What the Heck Are Monads?!

Поділитися
Вставка
  • Опубліковано 4 січ 2025

КОМЕНТАРІ •

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

    👷 Join the FREE Code Diagnosis Workshop to help you review code more effectively using my 3-Factor Diagnosis Framework: www.arjancodes.com/diagnosis

  • @Pandajuice22
    @Pandajuice22 Рік тому +55

    Whew, thank you for uploading this. I have a wedding to go to tonight and wasn't sure what to talk about with my fellow attendees. Now I do!

  • @gustavomezzovilla7248
    @gustavomezzovilla7248 Рік тому +193

    7:05 small correction: associativity means that x+(y+z) = (x+y)+z. What you've describe is commutativity (or the abelian property).

    • @ArachnidAbby
      @ArachnidAbby Рік тому +4

      You can also have right vs left associativity. Like doing 4+7+3 will execute left to right (left associative). 3^5^6 will be right associative, so it will execute like this: 3^(5^6)

    • @mtcomscxstart
      @mtcomscxstart Рік тому +11

      @@ArachnidAbby Right and left associativity is a thing on the notation level, rather than a mathematical concept

    • @edgeeffect
      @edgeeffect Рік тому +12

      There's always got to be ONE functional programmer that's got to pop up and say "well actually..." and then fill the blackboard with equations whilst the rest of us quietly back towards the door. ;)

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

      @@mtcomscxstart ah makes sense. I just remember having to deal with it while working on my compiler

    • @gustavomezzovilla7248
      @gustavomezzovilla7248 Рік тому +9

      @@edgeeffect i wish i was a functional programmer. I am a mathematician trying to be a better programmer at my shitty data hore job hahahahaha Knowing the difference between associativity and commutativity is the most i can contribute in this domain besides yelling "Do yOu kNoW abOUt YonEdas LeMma?" Hahahahah

  • @blakasmurf
    @blakasmurf Рік тому +141

    I got kicked in the Monads once, boy was that painful!

  • @kode4food
    @kode4food Рік тому +21

    Finally someone who actually came close to explaining monads in a way that normal people can understand! I've only been waiting over a decade since Brian Beckman made the 'definitive' explanation that ended up confusing everybody.

  • @sigmundwong2489
    @sigmundwong2489 Рік тому +2

    In general, when thinking about programming languages in terms of category theory, functions in the PL are considered morphisms, and types are the objects. Functors map one category into another category. Therefore, unless you're somehow departing the language altogether, you're only ever going to touch endofunctors. To be more precise a functor, is any type that is: 1) defined in terms of another type, 2) preserves functions between types (this is what "map" gives you), and 3) preserves identity functions. The initializer is NOT required for it to be a functor. That is, any functor F does not automatically give you a function T -> F for any type T. The initializer is better thought of as a natural transformation from the identity functor to the functor in question.

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

    This is a fantastic video, I have tried time and time again to understand monads, but this was the first video that really clicked. I like how you broke down the "monoid in the category of endofunctors" so thoroughly, that was a huge help.

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

    haha, this one was magical and I didn't see it coming! Following along I was like "feels like this weird concept which I haven't fully understood when i was trying to get into Rust" - turns out it was and I finally got it now! so I've watched a python video about a thing I shouldn't do in python, learnt about rust and ended up a better programmer than i was before. That's why you're once of a kind on youtube! have my thumps up :)

  • @filippopisello
    @filippopisello Рік тому +9

    Happy to see some more advanced content!
    After being lost for about 18 minutes, it suddenly clicked how I could have used something like this instead of regular error handling on a project a couple of months back.
    Cool to be reminded that there are alternative approaches out there!
    Cool to be reminded in mind that there are more ways to do stuff!

    • @SimGunther
      @SimGunther 6 місяців тому +1

      If we don't care where the error occurred and the debugging is simple, than I could see monads replacing explicit error handling.
      At 17:00, there's a nice example of handing errors at the end of the track instead of handling errors in the middle of each bind.

  • @dankprole7884
    @dankprole7884 Рік тому +5

    Any chance of a video on Generics? I have used them a bit in the past but i don't understand why you made the Monad depend on T at 8:49. I've never seen a good video on this topic.

    • @traal
      @traal Рік тому +3

      That would be nice, especially with the new 3.12 generic type hinting syntax.

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

    @ArjanCodes Very heady topic presented in an extraordinarily approachable way. I really liked seeing you turn maybe into a decorator. That gives me lots of ideas for my own home-grown functions. Thanks for another great tutorial.

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

      I'm very glad the video inspired you, Matt!

  • @WalterVos
    @WalterVos Рік тому +8

    Very nice to have you discussing an advanced topic again! Are you currently working in languages where monads do make sense?

  • @vidaroni
    @vidaroni Рік тому +6

    Dang, missed opportunity to title the video "What the func are monads?!"

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

    Thanks for showing this. Best video on monads by far and great to see better pattern matching usecases. Lately I’ve been writing tools that return tuples (eg (ok:bool, val: int, reason:str) returned from tryParseInt ). This is a great use case for pattern matching logic in the caller.

  • @johnathanrhoades7751
    @johnathanrhoades7751 6 місяців тому +1

    Video 3 on monads. Still working on the comprehension! Very well explained.

    • @ArjanCodes
      @ArjanCodes  6 місяців тому

      Glad it was helpful!

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

    Thank so much Arjan! I tried to understand Monads each time I got hyped on functional programming, but only this time I think I understood the idea of a monad and usecases. And actually it seems that I have already been using something like that for error handling with ts-result library that brings in this railway approach that you mention in the video

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

    good and informative video, i have a small nitpick, Maybe isn't really the typical exception handling mechanism, its more the null or none handling mechanism, and to this extent the Result in rust would be the try except equivilant and Option would be Maybe (synonymous)
    monadic handling can be very useful even in languages its not implemented, it would come down to developer preference and style, i myself prefer monadic handling

  • @mrc1500
    @mrc1500 Рік тому +31

    Thanks, Arjan. As a Scala refugee, this was very useful to me. If someone in the Golang community can come up with an implementation, it'll be funny to see what they called it.

  • @mtcomscxstart
    @mtcomscxstart Рік тому +2

    Something isn't quite right with the example at 4:15. What do you exactly mean when you say it's not an endofunctor? I mean, in a programming languages we usually talk about only one category - category of types of the language. Which means all functors are endofunctors just by nature. And it's not even a functor, at least because map isn't polymorphic as it should, it only works with functions from str to str. Correct me if I'm wrong, maybe I just misunderstood.

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

      +1. I also think the definition of EndoFunctor was not technically correct. The rest of the video is still very good though.

    • @mtcomscxstart
      @mtcomscxstart Рік тому +4

      @@MdImrulHassan I wouldn't agree on the rest of the video. Many details in the video aren't correct. I understand that there was a good intention to make this video easy to comprehend, but it sacrificed correctness so much that can in fact worsen your understanding instead. It's a really difficult task to make a "monad tutorial" easy to understand. But at least we should appreciate the efforts.

    • @AndreiGeorgescu-j9p
      @AndreiGeorgescu-j9p 27 днів тому

      ​@@mtcomscxstartI took one of this guys courses. He has an extremely surface level understanding of everything. It's like what you would think of a soy devs final form. But hey at least he's talking about important things like this

  • @ryanfu7555
    @ryanfu7555 7 місяців тому +1

    Thanks a lot!! Really cool and clear explanation.
    Just have a question.
    Can we do early return when there is an exception in the bind chain?
    like in this example
    Maybe(input_str).bind(parse_int).bind(is_positive).bind(lambda n: Maybe(double(n)))
    Thanks!

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

    Thanks!

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

    Very nice explanation about Functors and Monads. Seems like you have broken the curse of the Monad!

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

      Thank you ahaha! Let's hope so :)

  • @ManuelSchulte007
    @ManuelSchulte007 8 місяців тому

    Hi Arjan, thanks for this video. I'm actually considering the railroad oriented programming approach in a data project. With a colleague we are about to migrate some processes running in an ETL tool that is actually already working in a functional way and which already has a way of chaining operations along happy/sorry paths. Besides functional programming makes parallelization easier, and in data processing this is important. So yes, in conjunction with the itertools, generator expressions and multiprocessing, I think we will give monads a chance under Python 😉

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

    I've watched like 5 videos on this really esoteric topic, and this is one that finally made sense to my programmer brain by typing up code examples rather than like Venn diagrams or whatever else functional thinking people do.

  • @Yankzy
    @Yankzy Рік тому +3

    Awesome you back on advanced stuff! You should do metaprogramming topics

  • @kacabram
    @kacabram Рік тому +2

    Thanks a bunch for your video, very informative. Regarding the usability of monad - I've spent quiet a lot of time getting rid of code that relies extensively on results library (specifically, it's error handling way). At some point of codebase growth it became virtually impossible to tell, what behavior and return types you should expect from methods (and code overall), therefore, we decided to move to Python's built-in exception handling and typing mechanisms

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

    Thank you, an actual explanation of what the attributes of monad mean in practice

  • @5thElem
    @5thElem Рік тому +11

    Thanks for picking up this important topic! Those new to functional programming might find the explanations involving monads, functors, and monoids within category theory, where their origins lie, to be intimidating. In my view, it is more crucial to first present the practical elegance and simplicity of these concepts, as demonstrated in your video.
    Just a small hint: i think you are sometimes confusing associativity with commutativity.
    commutativity: (a + b) == (b + a) => order of operands does not affect the result
    associativity: (f ・ (g ・ h))(x) == ((f ・ g) ・ h)(x) => grouping of operands does not affect the result

    • @ruslansergeev4061
      @ruslansergeev4061 6 місяців тому

      Seems like after all Arjan tests the associativity but describes it incorrectly.

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

    I'm glad I completely agree with your conclusion. If it's in the language, sure, why not? But I won't step out of my way to use that when it's not the norm. It just makes reading the code more confusing if and when someone else has to dive in.

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

    we need more videos like this!

  • @romulocollopy4974
    @romulocollopy4974 2 місяці тому

    I had this discussion more than once in companies. Not about implementing a whole monad ecosystem in python, but implementing the Maybe modad. The argument revolved a lot about the "Billion Dollar Mistake" that has functions of any kind possibly returning Null. But, in Python, None is the sole instance of the NoneType type. That means that if you are using type annotations and your function can return None, it needs to be explicitly stated. The Maybe/Response monad can provide convenience -- like the NullObject pattern. But in python it's counter intuitive and requires unnatural boilerplate. I am diving more in this subject to apply it in Haskell

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

    Thanks Arjan, I tried to understand what monads were some time ago, but every video I started was just too complex for my understanding. You've explained it in a way that was very easy to understand!

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

      I'm happy to hear it helped you, Andrea!

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

    Great video, very good explanation!
    One thing, is that in Rust the equivalent of Maybe is actually the Option enum, the Result enum is another sum type that is specialized to represent an error.

  • @TheScott10012
    @TheScott10012 Рік тому +4

    So, would a dataframe be a monad? Since you can chain functions on a dataframe together and they return the DF

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

      You mean to ask whether it is an endofunctor?
      If you limit it to only having a map function that acts on the data in the dataframe, then I guess so... (The chaining of other functions/methods would be a bad example, I guess, because they process the value directly in some way, they don't map a user-supplied function necessarily.)
      But that doesn't make it a monad yet, does it? That also requires an associative binary operation with an identity element. What would that be?
      (Btw, I'm not an expert; just following the logic from the video.)

    • @peeweemunster
      @peeweemunster 10 місяців тому

      No, data frames in themselves are not monads, but some operations on data frames are monoids, e.g. data frame addition. Moreover addition is a commutative monoid, and the nice thing about commutative monoids is that you can use them in a reduce operation, e.g. in Apache Spark, to easily parallelize addition.

    • @AndreiGeorgescu-j9p
      @AndreiGeorgescu-j9p 27 днів тому

      ​@@peeweemunsterfunctions cannot be monoids, only types can. If two data frames can be combined together and return another dataframe then they are monoids under said operation. A dataframe of a certain type would also be at least a functor because all type level functions are Functors in an adt system

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

    21:07 where is this dunder match documented? I can't find it

  • @menaa5978
    @menaa5978 10 місяців тому

    Is the method `map` in the class `Functor` the same as `>>=` in Haskell?

  • @oreos_overdose
    @oreos_overdose Рік тому +5

    Not necessarily a criticism, more of an observation but this seems leaps and bounds ahead in complexity with respect to most of the subjects of your other videos.

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

      Maybe u think that way because you are not used to this way of coding. The examples itself are not that complex

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

      Yes, finally, we're so back

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

    Now I will be able to answer what a monad is when a relative asks me in a family funeral.

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

    Excellent post, thank you.

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

      Thank you for the kindness, Greg!

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

    thank you, that was very good explanation, it could be usefull if you have lot of possible types and validations, so you don't have to write special cases for everything, but define it in generic level

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

    I'm just learning about monads and functors in python. Cool stuff.
    Might be wrong but looking at the example you gave with the `add_one` method been mapped to the Functor(5) which returns Functor(5 + 1). It think a functor is a programming implementation of functions in math.
    I feel that given that a functor returns its value when called, we can say:
    Functor(5) = f(5) -> 5
    Say '->' means returns
    Hence; Functor(x) = f(x) -> x
    If add_one(x) -> x + 1
    Then;
    f(x).map(add_one) -> f(x + 1)
    If we let;
    g(x) = f(x).map(add_one) = f(x + 1)
    g(x) = f(x + 1) -> x + 1
    But f(x) -> x,
    Therefore g(x) -> f(x) + 1
    Mehh. I'm not actually sure if this is right but if it is, then i think understand better using this analogy.

  • @IvanToshkov
    @IvanToshkov Рік тому +2

    I think for a 20-ish minute video you're doing a good job in explaining what monads are. However there are a few things that are missing, unclear or slightly wrong IMO:
    * After describing monads at 11:13 I find the explanation of how they can capture side effects very unclear and hand-wavy (both literally and figuratively). I don't think it's obvious how from the structure of monads it follows that they can capture side effects. Adding an example of such monad would be helpful.
    * The Maybe monad is meant to be used as a replacement for None or null values. Using it to represent errors is akin to using None to represent errors: it could work in some simple cases, but is not the general solution.
    * In rust, the Maybe monad is called Opion. The Result monad is somewhat similarly looking to Maybe/Option, but instead of the None case, it has the error case, where it contains the error that happened. So it's a better mapping for exceptions.
    I agree that monads are most useful when they are part of the language from the get-go. In ML languages like Haskell, Ocaml and Rust you don't have the concept of null. Instead the standard library and basically the rest of the code is using the Maybe/Option monad. There are no exceptions either, so they are using the Either/Result monad. The compilers also check if the code handles all cases, which makes the use of monads there much better. Their usefulness in languages like Python or Java is limited. Even so, it's good to expand one's knowledge.

    • @AnthonyBecker9
      @AnthonyBecker9 8 місяців тому

      Thanks for adding some useful info and pointing out where info is lacking-I also found the point about side effects went by 10x too fast-If that’s the point, I’d really like to understand that better :D
      I can kinda understand though-based on my understanding that monads are used to create things like… string.strip().lowercase()
      In these cases, bind() is hidden with syntactic sugar. Explaining this would also make this video more valuable.

  • @kmac499
    @kmac499 2 місяці тому

    best description i've seen so far, But.. how many maybe's do you have to create in your code. One for null values, one for oversized strings, one for car number plate formsts, one for sanity checked order values..?

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

    I've been curious about funcotrs/monoids for so long and finally a video explaining this in practical terms! I also love that you explain why it wouldn't be useful yet in python.
    Personally, I see this as doing a bunch of unnecessary computation. If i divide by zero, there is no reason why I should continue down the Maybe(None) rail, why not just short circuit actually handle the error. On the other had I do see how this allows you to move error handelling to a high level and not worrry about it when there are other things going on

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

      The thing that I was immediately missing was any information about where and why in my chain of Maybe Monads the error occurred that led to a None-Monad. My guess is that you might be able to have a monad value that encapsulates the kind of error that occurred, but at that point, that seems like the same thing as throwing a specific error in Python.

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

    thanks for the vid, just a small error:
    6:35 talking about the binary operation of a monoid
    "it should be associative so the order in which you do the binary operation shouldn't matter..."
    That's not associativity but conmutativity "a·b" = "b·a" and the binary operation of a monoid does not need to be conmutative.
    What you meant to say was "the way in which you group operation doesn't matter" that is:
    " (a·b)·c " = "a · (b ·c) "
    I guess you wanted to say this. Just to clarify.

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

    I've known monads from using functional reactive programming paradigms like RxJava KotlinFlow and Swift Combine. They're very useful on multithreaded the front systems like mobile apps where you want to safely react to user input and do thread safe concurrent operations with standardised error handling. Functional reactive is a combination of monads and the observer pattern

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

    You know, its been a long time since we had a code roast, and that kind of videos is my favorite, i would be happy for another episode, maybe even something that uses openai api or django...

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

    Nice video !! I took haskell class at collage but I never imagine I could bring this to python.

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

      Thank you! I'm glad you enjoyed the content.

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

    Looks like something that you can find in code that you inherit from developer who suddenly left the company and noone is familiar with. Great content. Thanks.

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

    I love this kind of videos! Do you have any plan to create a video or a serie talking about rust? 🤔🤔

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

      Alas, by devoting his life to the way of the serpent he has forfeit his right to preach the good word. If only the high priests of Rust were more benign in their judgement...

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

    How about adding an `exception` field to Maybe to save whatever got raised. It could allow better user feedback, and also better debugging because the programmer could see the actual exception.

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

    This is great!!! Thank you so much for making this

  • @igordemetriusalencar5861
    @igordemetriusalencar5861 Рік тому +2

    Great video!! This explanation would be absolutely top-notch with a comparison against a functional language (Elixir?) or in a language with functional capabilities (R?, Julia?).

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

      Would totally love more Julia content from @ArjanCodes!

  • @lmbraga
    @lmbraga 10 місяців тому

    I really wish this video existed in 22 years ago when I had to sit through my first computer science class.

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

    Best explanation of monads I've seen.

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

      Glad you enjoyed the content!

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

    Love the t-shirt, where'd you get it?

  • @giullianosep
    @giullianosep 4 дні тому

    Monads are a hard concept to understand, but trying to learn monads before understanding monoids, functors and applicatives is like starting a game and trying to fight the last boss first.

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

    Great channel, subbed. Reddit sent me here. I am still working through a bunch of kaggle courses to learn python all the way up to AI coding. Even me having a barely basic understanding of the syntax, this was extremely easy to follow. 😊

  • @codingbloke
    @codingbloke Рік тому +3

    This is a big step forward in describing monads. I feel closer to understanding them but still not there yet. Perhaps need more examples, we kind a drifted into talking about maybe and railroad etc. Other examples of monad usage would be needed to help differentiate.

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

      It's description could be simplified much more: A monad is an object with a value that can be run through a pipeline(a chain of functions) where in each of the function applications in the chain it does some additional proccessing like checking for None or waiting for an operation in Async to finish. It's useful for automating repetitive task, by appying this additional operation each time a function is called. Monads that are actually used in mainstream languages are Result, Option, Async and technically also List. Bind is sometimes referred to as and_then or flatmap in some languages. So in essence: A chain in where each step it does something extra in addition to applying the function.

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

    Great vid! First class functions - a good thing. Functional programming - meh not so much. And I write a lot of Rust...
    For real-world applications functional programming will give you poor performance, or great performance right up until COOOOOOOOWtf? - clunky error handling and readability problems past a certain scale. For raw data processing it's fine. Feels like unix pipes. For anything bigger, anything complicated nah. AAA game? Image editor? Web browser? Not a prayer. I've never seen a functional body of code that didn't come with a huge accompanying document trying to explain the big picture because the abstractions start to fall apart as things get bigger. Error handling is either ignored or becomes a nightmare that relies on a bunch of macros. Haskell is 30+ years old and still not widely used. Fun ideas, but 99.999% of all code is some form of imperative - even most real world Rust is often heavily imperative, unless you're playing code golf. Keep up the good work!

  • @hansschenker
    @hansschenker 3 місяці тому

    please explain the Monad using Typescript!

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

    very nice, thank you, complex topics but I think I´ve learned something!

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

      I'm happy to hear! Glad you enjoyed the video :)

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

    Is it not possible to add monads as a concept into python using metaclasses?

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

    Have to see this a few more times… 🤓

  • @rhbvkleef
    @rhbvkleef 11 місяців тому

    Small correction on your Maybe monad. Bind always returns `Maybe[U]`. The return type of `Maybe[T] | Maybe[U]` is incorrect.

  • @greatfate
    @greatfate 9 місяців тому

    A monad is just a monoid in the category of endofunctors, what's the problem?

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

    The problem with explaining monads is that all monads are different, and sometimes completely different from each other. A list and an Async task does not have much in common except that they are monads. It's sort of like a spaceship and a bicycle are both transportation devices, but that's where the similarities end.

  • @kilianklaiber6367
    @kilianklaiber6367 9 місяців тому

    I understand classes, Objekts and functions/methods in Python. So was interested what a functor is. The definition of the map Returns an Objekt or instance of the class. Thus the definition reminds me of recursive functions.

    • @kilianklaiber6367
      @kilianklaiber6367 9 місяців тому

      But honestly, I didn't understand all the Rest of your Video. No Definition of func(self.value) sent me into compile error Status.

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

    Monads!
    Love the videos, Arjan

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

      Glad you're enjoying the content! :)

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

    I understood... at least half of that. I can definitely see the use case for them, at the very least, though I admit some of the construction logic is a bit confusing to me(though I've never really understood generics so that might be part of it).

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

    Good Stuff....... Thank you..

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

    Fantastic video

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

    What do you mean by same shape?

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

    спасибо, Эржан

  • @mr.wheelers5504
    @mr.wheelers5504 Рік тому

    I think Monads have a usecase when dealing with computationally expensive processes, e.g. fitting multivariate functions. It would avoid spending a lot of time on values that you know would throw an error in later parts of the algorithm.

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

    I'm a mathematician who works with monads on a daily basis and yet I've never managed to figure out why programmers, of all people, care about monads.
    I have to admit, I still have to squint to see the connection to the arcane programming terminology :p
    A category is just a notion of stuff and stuff that goes from stuff to stuff.
    Functors are stuffs that go between categories, so it has to be able to do something both to the stuff and to the stuff that goes from stuff to stuff, in some consistent way.
    Endofunctors go from a category to itself. In particular that means you can do it multiple times to the same stuff.
    A monoid is a stuff that has a way to "multiply" different copies of itself. For endofunctors that multiplication is composition, and the "way" is to compute the composition to an instance of just a single use.

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

    Typescript is also not a functional programming language. Even though with fp-ts you can do it very comfortably. It provides you all the monads you need - and I find the "exception handling" in fp superior to the "object oriented"

  • @АртемФедоров-ю7б

    why it's a functional pattern if it contains a type and an instance of this type which comes from OOP, I would say it's a mix of OOP+functional.

  • @Cyber_Commando_00
    @Cyber_Commando_00 10 місяців тому

    You are correct that operating on monads shouldn't require any boilerplate. In fact, it requires neither that nor close integration. A monad is not a runtime representation or syntax feature, but an abstract category: a relation formed by morphisms defined over structures, with coherence properties. You never arbitrarily define a composite class or modify the language to exploit the monoid formed by integer addition, the functor formed by a list, or the monad formed by async actions. Property based testing, perhaps a runtime protocol, would make more sense.
    Generally speaking, the separation of behaviors from representations is advised in the functional style, so rather than considering a monad as a tightly coupled object to be converted to, consider it as operations which may be defined over potentially existing objects, if they haven't already. It would be fairly trivial to define the bind operator as a regular function decorated with singledispatch, for instance. In fact, type classes in Haskell are implemented similarly with an implicit parameter gathered by the type system.
    To make this concrete, consider the list monad:
    let unit(x) = [x]
    bind(xs, f) = [y for x in xs for y in f(x)]
    where fail = []
    Extensibility without modification is a powerful principle of software architecture closely related to the Expression Problem. Otherwise, excellent content, as always!

  • @-j.s-
    @-j.s- Рік тому

    Great use of decorators!

  • @mardukbp
    @mardukbp Рік тому +6

    It's easier to understand functors and monads using diagrams. A functor is a mapping between categories. A category is a collection of objects with arrows between the objects that can be composed. An endofunctor F in the category of Python types maps the type a to the type F[a] and a function from a to b to a function from F[a] to F[b]. The simplest functor in Python is the list constructor []. List comprehension is a clear example of mapping a function between types to a function between lists. In the category whose objects are functors, a monad is a triple, consisting of a functor and two natural transformations (that is the name of the mapping between two arrows from functor to functor). One acts as the identity under composition and the other composes two arrows. That is the monoidal structure. A list is also a monad, with double list comprehension acting as the composition. Since the result is a list and not a list of lists this operation is called flatMap in some functional languages. Scala and Haskell allow using comprehensions with any monad. Since this allows sequencing operations, it is commonly called a glorified semicolon. Monads allow reifying computations as data structures. Which means that instead of calling an API immediately one obtains instead a description of the API call. This is the reason why it is said that in pure functional programming there are no side effects. All functions return data. In Haskell only the main function can execute side effects. They are called side effects because in mathematics there is no function for sending emails. The idea of functional programming is to make writing software akin to writing a book in Word instead of with a typewriter. It is not for academics. It is what engineering always does: it gives us more powerful and sophisticated tools that allow us to go farther than our ancestors. The Joy of Abstraction is a quite accessible introduction to category theory. Also check out CQL and Ballerina. Programming with diagrams is not old-fashioned, it is only getting started. Thanks for making this video Arjan! 🎉

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

      Edit: just saw the bit about “there is not function for sending emails” hahahaa

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

    Where is your shirt from? :)

  • @unperrier
    @unperrier Рік тому +10

    Monads fill in the shortcomings of functional programming languages. They allow to sequence function calls while attaching a context, which is something that exists natively in python, so monads should stay in the FP world as python has better native ways to do the same things.
    But they're nice, the maths behind it is a bit harsh just to understand that it's merely a context passed down the chain of recursive calls to emulate a chain of sequential calls.

    • @AndreiGeorgescu-j9p
      @AndreiGeorgescu-j9p 27 днів тому

      That's not what a monad is, that's what IO is which is one of countless useful monads. You don't comprehend the idea and then decide to gatekeep

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

    Thanks. I am studying FP and Thinking how to apply some concepts in Python. You rocks.

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

      I'm happy to hear it was helpful, Lucas!

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

    Thanks for the explanation. I agree. I love the predictability of modads, but at this time using them in most python projects is *overkill*.

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

    Amazing!

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

    Finally a good explanation.
    I agree with your assessment: it doesn't seem to integrate well with existing "pythonic" code.

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

    What I'm missing in this video is an explanation of higher-order functions. I'm aware you explained it before, but that was a while ago.
    I already know the concepts in the video, but if I didn't I would wish I knew what a higher-order function was.

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

    in most languages it does not make sense to directly implement monads as a single entity. it can be beneficial only in languages that have function-based dispatching (not class-based) and that can reliably inline higher order functions.
    errors as values are totally superior to exceptions outside of scripting. i wish there was a way to switch between those on per-session/per-build basis (theoretically, this is possible).

  • @Fonsecaj89
    @Fonsecaj89 Рік тому +5

    I once try to understand monads years ago and found that Leibniz wrote a humongous book about that theory called Modanology

    • @edgeeffect
      @edgeeffect Рік тому +2

      Best way to understand monads is to forget all those great mathematical tomes and just write a bit of JavaScript using promises then you'll just start to wonder what all the fuss was about.

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

      Leibnitz's "Monadology" has nothing to do with monads in modern sense

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

      And the book is rather short btw

    • @AndreiGeorgescu-j9p
      @AndreiGeorgescu-j9p 27 днів тому

      ​@@edgeeffectthat is not a monad

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

    I believe the concept of the monad can be very useful. I noticed a lot of people using the exception framework to communicate business validation errors in a DDD architecture. To me this has always been kind of weird, because it is expected behaviour, and not an exception. So switching to another control path is kind of off.
    I am using Result bags for this, a custom solution to communicate errors, warnings, etc. Monads could be nice for this as well as it doesn't break the main control flow and leaves the exception framework for what it is meant for.

    • @r.c8756
      @r.c8756 Рік тому

      I get what you mean but you’re a little bit wrong in the way that the reason why exceptions are called exceptions and not errors is that as their name indicate they’re not just meant to deal with errors, even though in the real world they’re mostly used to do that. One can argue that using exceptions to deal with exceptionnal behavior is by definition using the tool for its precise purpose. I happen to work with the DDD philosophy (DDD is not really a specific architecture) and for our project we created a DomainException (which is then subclassed for more specific purposes) in order to differenciate technical error handling and domain validation. This also makes it easier to setup a middleware in the webapi to bubble these appropriately towards the UI.
      There may be ways to do that in a more elegant fashion, however it has the virtue to keep things simple and so hold up the criminally underrated KISS principle.

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

    Gods, this will be so useful for my work.

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

      Glad to hear it was helpful, Victoria!

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

    What makes an object a Functor?
    Map
    What makes an object a Monoid?
    Reduce
    What makes a Functor a Monad?
    FlatMap/Bind and because they are wrappers.

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

    cool shirt, where do i get one?

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

    Oh these are GREAT

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

    16:58 is this python or rust!?😅

  • @cparks1000000
    @cparks1000000 6 місяців тому

    Monoid is a set with a binary operation. A function is not a monoid. (There is also a related category-theoretic definition of a monoid.)
    A functor is not a wrapper around a value. It is a map between categories.
    These concepts don't translate directly into code, and I didn't think it's beneficial to try to force the translation.

  • @smanzoli
    @smanzoli 4 місяці тому

    The good thing about Monads is that NOONE, not even a single developer in the world, even ones that ONLY use funcional programming, needs to know what it is.

  • @kid-vf4lu
    @kid-vf4lu Рік тому

    Lovely!

  • @EW-mb1ih
    @EW-mb1ih Рік тому

    Nice videos! Do you plan to make videos about Rust (it would be great)?