Free Monad in Scala

Поділитися
Вставка
  • Опубліковано 25 тра 2022
  • Written form: blog.rockthejvm.com/free-monad/
    In this video, we'll talk about the Free Monad, an abstraction that allows us to write better, more modularized, more easily maintainable and more testable Scala code.
    We'll discuss about "normal" monads, the structure of the Free monad, an example of situation where the Free monad is particularly useful, and we're going to implement a simple version of the Free monad from scratch.
    Free monads are part of the cats-free library (and others). If you want to learn more about Cats, we teach this at Rock the JVM:
    Cats: rockthejvm.com/p/cats
    Follow Rock the JVM on:
    LinkedIn: / rockthejvm
    Twitter: / rockthejvm
    Blog: rockthejvm.com/blog
    -------------------------------------------------------------------------
    Home: rockthejvm.com
    -------------------------------------------------------------------------
  • Наука та технологія

КОМЕНТАРІ • 23

  • @akemegbebu7807
    @akemegbebu7807 2 роки тому +11

    Just discovered this channel... A real jewel.

  • @temoncher
    @temoncher 2 роки тому +4

    Only wrote about 200 lines of Scala in my life, but this channel is brilliant at explaining tough FP concepts. Great work!

  • @dieduch
    @dieduch 2 роки тому +2

    Fantastic video, great work explaining free monads!

  • @indianbullet
    @indianbullet 2 роки тому +3

    This was great, thanks! I would love to see a comparison of free monads vs tagless final. It seems like they both claim to achieve a similar goal, but it's not clear to me what the advantages of one approach over the other are.

  • @DavidePavan
    @DavidePavan Рік тому +1

    Amazing!

  • @davidcrosson
    @davidcrosson Рік тому +1

    thank you very much, I've learned a lot of thing, in particular the ~> trick !

  • @fractionstaughttherightway5164

    I’ll ask one question more. The create/get/create/delete sequence in myLittleProgram has already been executed by the time foldMap is called on myLittleProgram, so a Flatmap’s f must reference lambdas different from those implicit in the myLittleProgram for comprehension. Obviously, this sequence can be executed only once. So what ARE the Flatmap f’s that are called by FoldMap? I’m totally confused!

    • @rockthejvm
      @rockthejvm  Рік тому

      The CRUD actions are not executed, but merely _described_ by those Free instances. What you get is a "big" data structure, nothing more. When you call foldMap, the natural transformation's functions kick in (those from dbOps2IO).

  • @thegioveZ
    @thegioveZ 2 роки тому

    Very nice video! Tangential question: what theme are you using? :)

  • @eliasavelino6729
    @eliasavelino6729 Рік тому

    This is so satisfying to watch even though I understand maybe 15% of it, but is this like the Repository Pattern in OOP? Interface the API, and swap with different implementations

    • @rockthejvm
      @rockthejvm  Рік тому

      Most design patterns are different versions of programming to an interface, then swap implementations.

  • @Voltra_
    @Voltra_ 2 роки тому +1

    So free monads, given that "programs" are just monadic descriptions of the execution flow, can be used to independently execute and log stuff. Interesting.

  • @leorandomnickname
    @leorandomnickname 2 роки тому

    so FreeMonads allow you to use natural transformations to change the interpretation, but I still dont get why you cant do that with normal Monads, I guess the fact that the FreeMonad kind of fixes the wrapped type A as a type parameter has to do with it. Thanks for this video, really cool.

  • @user-lg5bx8jl3z
    @user-lg5bx8jl3z Рік тому

    hi,Daniel. I want to buy your course on your website, but I find that all of the videos do not have subtitles. I am from a country not using English as mother tongue, so it maybe not that easy for me to understand each word you are saying. Is there any way that I can add subtitles to the courses like on youtube? Thanks.

    • @rockthejvm
      @rockthejvm  Рік тому

      Sadly the Rock the JVM website is not as advanced as UA-cam to add automated captions :(

    • @user-lg5bx8jl3z
      @user-lg5bx8jl3z Рік тому

      @@rockthejvm OK thanks for your reply. I'd like to buy a bundle on the Black Friday 2022, will be there any promotion? Or maybe I will buy a monthly membership to have a try first. Thank you anyway.

  • @fractionstaughttherightway5164

    Very elegant code! Some questions: It looks like the execution of myLittleProgram results in a Flatmap(Suspend(ms), f) Free monad for each line, but I don’t see how they are tied together in a single Free monad for the subsequent foldMap to interpret. Further, a Flatmap’s f would encompass all the computations after it in myLittleProgram, so it would seem that a Flatmap’s f would encompass some of the computations of the previous Flatmap f’s, resulting in a repeat of these computations when myLittleProgram is foldMapped. Finally, the get() in myLittleProgram would not produce a result “name”when it’s flatmapped because myLittleProgam has no database to work with when the for comprehension is executed. How does this work?
    Too many questions? They might not even make sense. A Free monad Part 2 that showed the data structures created during execution would do a lot to explain all that’s going on. Thanks!

    • @rockthejvm
      @rockthejvm  Рік тому

      unable to untangle the question :(

  • @Neskeip
    @Neskeip 2 роки тому

    In the for comprehension (15:10), Don't you should return "pure( () ) " instead of ()? I mean, I think that you are getting advantage about Unit type

    • @rockthejvm
      @rockthejvm  2 роки тому

      We're using IO.create, and in the block we need to return a value, not another monad.

  • @mouadmuslim
    @mouadmuslim 2 роки тому +2

    kind of advance subject for me, i'll get back later !