What is a Monad? - The Last Monad Intro You'll Ever Need

Поделиться
HTML-код
  • Опубликовано: 21 дек 2024

Комментарии • 59

  • @Razerdawn
    @Razerdawn Год назад +20

    James你很適合當英文老師,我很久沒有睡那麼熟了,謝謝你

  • @sorvex9
    @sorvex9 Год назад +117

    A monad is simply a monoid in the cateogry of endofunctors. Didn't you know that?

  • @hansschenker
    @hansschenker 6 месяцев назад +2

    the steps to a monad are: functor -> applicative functor -> monad
    easy to follow but useful samples helping in understanding the funtioning of a monad!

  • @SimGunther
    @SimGunther 6 месяцев назад +5

    Monads aren't just "a useless wrapper to do a side effect in purely functional programming land".
    The best way I can describe monads is how I described it to another youtuber on their "purest coding style" vid - monads are tuples of the following 3 elements:
    1. The context that changes with each bind
    2. Injective functor that uses the value from the initial context and returns a new value
    3. Surjective functor aka the bind that takes the new value and creates a new context that will hold this new value
    FWIW the IO monad derives from the State monad. An IO monad can be defined as:
    1. File handle/other IO related sockets to change via the runtime along with extra values to use with the handle/socket
    2. Functor that supplies the line from the CLI
    3. Functor (most likely implicitly defined in the prelude) that takes the string from readLn and creates the context to say "if we were the runtime, we would've read X characters from the file, so we're at this point in the handle"

  • @SolathPrime
    @SolathPrime 8 месяцев назад +2

    π[T] -> (T->π[U]) -> π[U]
    Where ("T", "U") are generics, and "π" is the repository that's what a Monad is

  • @newsofthenerd
    @newsofthenerd 7 месяцев назад +8

    So I an program in a number of languages including haskell and I don't understand what that lower representation is. I mean I can look at it and kinda understand what it's says but it's basically explaining a concept by way of another concept I don't know what it is. Is that typescript or some c++ derivative. Why explaining something by using another foreign concept it just causes extra mental overhead and exactly what all those other failed teachers do.

    • @newsofthenerd
      @newsofthenerd 7 месяцев назад +2

      Ok never mind after watching further it's very clear you don't have a full understanding of monads yourself. It seems your a javascript scripted and are trying to understand how javascript has borrowed. But your explanations only halfway there and its frustrating because the into to your video is right where I am. I have watched and read all the explanations and I know I don't fully understand it to myself. But my understanding seems to be a couple steps past yours. You get so close in small places for 30 seconds almost get to monads and functors and then miss the bus. Then you skip what you should be explaining and just use monads incorrectly to describe monads getting the types wrong. But this probably has something to do with you attempt to explain javascript. Which is really and I mean really not a good language.

    • @SimGunther
      @SimGunther 6 месяцев назад

      ​@@newsofthenerd I hope that my latest comment on this vid helps you more than James :)

  • @have-bear
    @have-bear Год назад +7

    我不喜歡用 side effect 概括 monad 的概念
    我覺得這對一些 monad 來說有些牽強
    例如 A -> Array 作為 monad function 通常被解釋成有多種可能結果的運算過程
    「有多種可能結果」是什麼樣的 side effect?
    我想 side effect 這個詞或許來自 IO monad
    A -> IO 被當作帶有印出/讀取的運算過程
    其中「印出/讀取」作為 side effect 來說很合理
    因為它單純就是在運算過程中「順便」印出文字或讀取檔案
    但這個「順便」對於數學家來說非常刺眼, 因此它被形容為不純函數
    不純的部份因此被稱作 side effect
    但對於 A -> Maybe 和 A -> Array 這種會改變流程控制的 monad 來說
    用 side effect 描述就很奇怪, 它根本就不是「順便」造成的結果
    A -> Maybe 被解釋成「可能會失敗」的運算過程人們能接受是因為 C++ 萬惡的 throw/try/catch
    把 A -> Array 解釋成「有多種可能結果」的運算過程就完全是牽強的解釋
    更不用提 continuation...
    說到底 monad 關注的還是在他的數學結構
    簡單來說就是 a monoid in the category of endofunctors
    硬要給它一個程式上的直觀圖像本身就不合適

    • @have-bear
      @have-bear Год назад +2

      還有把 A -> Array 解釋成「有多種可能結果」的運算過程也不正確
      因為 Array 具有順序, 但「可能性」不應該有順序之分
      A -> MultiSet 比較適合這樣的解釋 (multiset: 可包含多個相同元素的集合)
      還有之前在某個 conference youtube video 上看到一種 monad: Distribution 某個機率分佈下的 B 值
      A -> Distribution 才是真正「有多種可能結果」的運算過程

    • @james-kool
      @james-kool  Год назад +1

      我覺得不用過度著重在side effect這個字上面。用effect來稱呼這些運算算是滿普遍的習慣用法,不太需要糾結在字面上的意思。我自己的解讀是就把它當成某種效果

    • @james-kool
      @james-kool  Год назад +2

      ​​​​@@have-bear我覺得不需要糾結在「什麼monad才是真正的可能性」。這不是重點,也不是這次討論的東西,下這個定義也沒什麼幫助。只要你找到或定義一個monad可以符合你當下需要做的事情、能達成你要的功能就好了。說到底,他就是一個結構而已,沒有intrinsic的意義,意義都是人類賦予的。
      舉個例子,「我使用Array當做多種可能結果來解釋monad」的過程可以類比成,我們在討論、嘗試理解「為什麼鳥可以飛」。而重點放在「Array有順序所以不該解釋成可能性」就有點像在討論過程中開始把重點放在「不對啊,鴕鳥不會飛耶」。
      在影片中這種應用,把array理解為「可能性」或是nondeterminism,可以解釋最終產生的效果、可以幫助理解,那就足夠了。

    • @have-bear
      @have-bear Год назад +6

      @@james-kool
      monad 作為一個數學結構, 當然不一定有程式上的直觀圖像
      但作為程式設計師, 直觀圖像是非常重要的
      我第一次學 monad 時範例就是 List monad
      然後教程把它解釋成「有多種可能結果」的運算過程
      剛學函數式程式語言的人看到這裡一定都一頭霧水
      把它當作是 flatMap 對程式設計師來說就足夠了
      畢竟 List 裝的不一定都代表「多種可能的結果」
      我想應該很少情況會把它當成「有多種可能結果」的運算過程來編程吧
      所以 flatMap 才不會稱作 mapWithMultiplePosibility
      我想程式設計師認為 monad 太抽象, 是因為出發點根本就錯了
      多數的教程都把 monad 解釋成 side effect
      然後再說這裡的 side effect 比字面上還強大
      比如它可以出錯(Maybe), 分支出不同結果(List), 甚至可以竄改後續的運算(Cont)
      side effect (或是 effect) 這一詞根本沒辦法囊括這些概念
      這麼解釋只會讓人更難理解 monad
      我認為程式設計師不用理解 monad 的細節, 因為 monad 不具有統一的概念
      它只是用來描述所有看起來像函式的數學結構而已
      試圖給所有 monad 一個統一的直觀圖像才會使得人們認為它很抽象

    • @AndreiGeorgescu-j9p
      @AndreiGeorgescu-j9p 9 месяцев назад

      ​@@james-koolthe word side effect is very important. Also not explaining applicative makes this video hard for people to really grasp

  • @Filaxsan
    @Filaxsan 10 месяцев назад +1

    Great video! I wish I could understand Chinese to watch all the other videos! All the best brother 💪

  • @Mikhanius
    @Mikhanius Год назад +5

    I don't understand the purpose of the flatmap section at all. Flatmap is not somehow more related or better for monads than map. It's just two functions combined - map and flatten. You could do everything flatmap can do with those two, and it would be just as monadic a pattern.

    • @AndreiGeorgescu-j9p
      @AndreiGeorgescu-j9p 9 месяцев назад +1

      Monad adds the ability to flatten on top of Applicatives

    • @GiveMeAnOKUsername
      @GiveMeAnOKUsername 4 месяца назад

      So map is redundant?

    • @Mikhanius
      @Mikhanius 4 месяца назад +1

      @@GiveMeAnOKUsername no, but flatmap is. You can always flatten an array, but you cannot un-flatten and recreate the lost structure. It's still useful to have flatmap as a shorthand to writing map().flatten() each time, but it's not an essential part to understanding monads.

  • @yashdelhiwala
    @yashdelhiwala Год назад +1

    Hey, do you have an English version of your Currying video you referenced?

  • @hooman1439
    @hooman1439 2 месяца назад

    My recommended section always has a monad video. I still don't understand what they do. And also, why are monads important?

  • @Nikage23
    @Nikage23 10 месяцев назад

    Nice video, bro. I know fp dart package supports uses do notation but I couldn't understand what language you using in your examples. How do you make your animation for video, btw?

  • @銅學-k7e
    @銅學-k7e Год назад

    正想說怎麼這麼久沒講 functional programming,這不就來了嗎

  • @Chitose-e3v
    @Chitose-e3v Год назад +1

    為了FP我已經學了一個月Haskell,雖然已經學會了Monad的用法,但還是感覺對這個概念沒有完全理解

  • @莫秉禾
    @莫秉禾 Год назад

    下一個單元應該要到 applicative 了
    從 Array to Option
    或 Array to Either

    • @ayii-it
      @ayii-it 5 месяцев назад

      哭哭,好像沒後續了

    • @莫秉禾
      @莫秉禾 5 месяцев назад

      @@ayii-it 看下面英文留言大概就知道為啥沒後續了

    • @ayii-it
      @ayii-it 5 месяцев назад

      @@莫秉禾 請問是因為被人家批評嗎?
      我覺得講的還不錯耶 QQ

  • @steamedeggeggegg
    @steamedeggeggegg Год назад +5

    等下 怎麼變英文了lmao

  • @heyyohihat
    @heyyohihat Год назад +1

    未來有可能有中文發音版本嗎...

  • @LordErnie
    @LordErnie Год назад +2

    The only problem I have with the usage of bind is that one doesn't call a function, but it does look like it. const = bind add2(init) is a bit weird. Functionally speaking bind would return a function that takes in something and returns that thing plus 2. I don't really like the syntaxis of assigning values to something whilst that value is an expression of the function its end value, instead a new function that just defines that x is add2(init). Bind does what exactly? It just unwraps it, applies the operation, and rewraps the result. The await keyword shows that it is waiting on an operation. Bind here doesn't serve its context here. To what are we binding? With what? A nice video non the less, but there are some holes in there that I can't seem to place.

  • @kungfooman
    @kungfooman 6 месяцев назад

    It's like saying an axe is more powerful than a hammer lol... flatMap and map are just two tools for different purposes.

  • @Oktokolo
    @Oktokolo Год назад +3

    It was hard to keep awake with teh monotonous AI-style voice (not sure if actual AI or just Spock though). But the content was certainly worth it.

  • @TimJSwan
    @TimJSwan 3 месяца назад +1

    Sadly, even though I went to the same University that people who make PayPal in RUclips went to, if I accidentally click off of this video on the RUclips app for Android, they don't have a functionality for undo so I have to watch ads and I have to manually find my position in the video after searching for the video in my watched history, but they'd never hire someone like me or listen to my advice. These are basic user interface rules which were defined since the 1980s and 90s but RUclips forces you to use their app even though they can't provide basic interface functionality for even a simple user.

    • @doctorsphoenix4681
      @doctorsphoenix4681 3 месяца назад

      They’re in it for the money, not the functionality. Just enough functionality to get them money🙃

  • @pmcgee003
    @pmcgee003 6 месяцев назад

    Original paper would be Moggi, before Wadler ...

  • @ゾカリクゾ
    @ゾカリクゾ 3 месяца назад

    sorry, I still don't get it lol. good thing is I want to learn Haskell anyways

  • @klaus-udokloppstedt6257
    @klaus-udokloppstedt6257 Год назад +7

    and yet another video that fails to explain what a monad is (to me). 😟

    • @TJ-hs1qm
      @TJ-hs1qm Год назад

      those have helped me, in this order
      1. ruclips.net/video/e6tWJD5q8uw/видео.html
      2. ruclips.net/video/I8LbkfSSR58/видео.html
      3. ruclips.net/video/bOpJQDGYoKw/видео.html
      after watching 1 I went back and forth between 2 and 3. But watching 1 is the real deal, especially when he explains when to use map and when flatMap.

    • @Ashalmawia
      @Ashalmawia 8 месяцев назад

      this was better than some. basically it's a generic type with 1 type parameter, that also includes the return and bind operations. these operations (supposedly) come in handy if you know how to use them. for me, I have tried this, but never really found a use (yet) for bind. I think that is the one thing I am missing (could be because there is no do syntax like he said as well).

  • @edwardmacnab354
    @edwardmacnab354 9 месяцев назад +2

    too much hidden behaviour

    • @AndreiGeorgescu-j9p
      @AndreiGeorgescu-j9p 9 месяцев назад

      Nothing is hidden lol. There's entire fields of math behind this

    • @JDogB-tc3lx
      @JDogB-tc3lx 6 месяцев назад

      @@AndreiGeorgescu-j9p 😂 bruh you must be a functional programmer, you completely missed his point

  • @Peter-r4h9q
    @Peter-r4h9q Год назад

    好,看完兩輪後,結論 "看沒"
    我是從物件導向開始的,有點被物件所干擾,
    我覺得這東西有點像是連鎖反應,有點像是工廠類遊戲(異星工廠)
    就是所有 "東西" 都是簡化到最簡單的"東西"
    經過一個 "反應(函式)" 變成一個 "新東西"
    "新東西" 又 經過 "新反應" 又變成一個 "新新東西"
    一直下去,直到出現我們要的"東西"這樣
    感覺像是把數學的連續運算概念實現化的感覺

  • @terrormapu
    @terrormapu 14 дней назад

    I am dumb enough for you to fail..

  • @watcher8582
    @watcher8582 21 день назад

    A monad is more than whatever aspect of it was later used in programming languages. You can't really learn monads just by sticking to programming notions. If you do that, you merely lean about monads in programming. That's like you'd wanted to learn what music is all about by only going to rap festivals. The mathematical aspects of monads go beyond the computable realm. Just saying.

  • @epistemocrat
    @epistemocrat 3 месяца назад

    the only thing this video explains is why nerds get bullied :D