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

  Рет қаралды 15,274

JamesKool

JamesKool

Күн бұрын

example code and readings: github.com/imjamesku/monad-vi...
0:00 intro
0:47 Revisit array map
1:20 flatMap
2:52 Represent effects with types
5:24 What is a monad?
6:44 How does Promises relate to Monads?
7:40 Monads vs. Functors
7:59 do notation
9:08 Promise example
9:21 Array example
10:39 Maybe example
11:42 Writer example
14:39 Recap
15:23 Additional reading
Icons: www.flaticon.com
illustrations:www.irasutoya.com/
B-roll:envato elements

Пікірлер: 42
@sorvex9
@sorvex9 6 ай бұрын
A monad is simply a monoid in the cateogry of endofunctors. Didn't you know that?
@SimGunther
@SimGunther 8 күн бұрын
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"
@Razerdawn
@Razerdawn 7 ай бұрын
James你很適合當英文老師,我很久沒有睡那麼熟了,謝謝你
@Filaxsan
@Filaxsan 3 ай бұрын
Great video! I wish I could understand Chinese to watch all the other videos! All the best brother 💪
@SolathPrime
@SolathPrime 2 ай бұрын
π[T] -> (T->π[U]) -> π[U] Where ("T", "U") are generics, and "π" is the repository that's what a Monad is
@Nikage23
@Nikage23 3 ай бұрын
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?
@yashdelhiwala
@yashdelhiwala 6 ай бұрын
Hey, do you have an English version of your Currying video you referenced?
@user-nr3bt4bz4f
@user-nr3bt4bz4f 7 ай бұрын
下一個單元應該要到 applicative 了 從 Array to Option 或 Array to Either
@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 7 ай бұрын
還有把 A -> Array 解釋成「有多種可能結果」的運算過程也不正確 因為 Array 具有順序, 但「可能性」不應該有順序之分 A -> MultiSet 比較適合這樣的解釋 (multiset: 可包含多個相同元素的集合) 還有之前在某個 conference youtube video 上看到一種 monad: Distribution 某個機率分佈下的 B 值 A -> Distribution 才是真正「有多種可能結果」的運算過程
@james-kool
@james-kool 7 ай бұрын
我覺得不用過度著重在side effect這個字上面。用effect來稱呼這些運算算是滿普遍的習慣用法,不太需要糾結在字面上的意思。我自己的解讀是就把它當成某種效果
@james-kool
@james-kool 7 ай бұрын
​​​​@@have-bear我覺得不需要糾結在「什麼monad才是真正的可能性」。這不是重點,也不是這次討論的東西,下這個定義也沒什麼幫助。只要你找到或定義一個monad可以符合你當下需要做的事情、能達成你要的功能就好了。說到底,他就是一個結構而已,沒有intrinsic的意義,意義都是人類賦予的。 舉個例子,「我使用Array當做多種可能結果來解釋monad」的過程可以類比成,我們在討論、嘗試理解「為什麼鳥可以飛」。而重點放在「Array有順序所以不該解釋成可能性」就有點像在討論過程中開始把重點放在「不對啊,鴕鳥不會飛耶」。 在影片中這種應用,把array理解為「可能性」或是nondeterminism,可以解釋最終產生的效果、可以幫助理解,那就足夠了。
@have-bear
@have-bear 7 ай бұрын
@@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 一個統一的直觀圖像才會使得人們認為它很抽象
@user-tx4wj7qk4t
@user-tx4wj7qk4t 2 ай бұрын
​@@james-koolthe word side effect is very important. Also not explaining applicative makes this video hard for people to really grasp
@newsofthenerd
@newsofthenerd 15 күн бұрын
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 15 күн бұрын
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 8 күн бұрын
​@@newsofthenerd I hope that my latest comment on this vid helps you more than James :)
@user-cu9vx2zr9n
@user-cu9vx2zr9n 7 ай бұрын
為了FP我已經學了一個月Haskell,雖然已經學會了Monad的用法,但還是感覺對這個概念沒有完全理解
@user-yt4ik2vr9b
@user-yt4ik2vr9b 7 ай бұрын
正想說怎麼這麼久沒講 functional programming,這不就來了嗎
@pmcgee003
@pmcgee003 8 күн бұрын
Original paper would be Moggi, before Wadler ...
@kungfooman
@kungfooman 9 күн бұрын
It's like saying an axe is more powerful than a hammer lol... flatMap and map are just two tools for different purposes.
@LordErnie
@LordErnie 6 ай бұрын
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.
@user-tx4wj7qk4t
@user-tx4wj7qk4t 2 ай бұрын
What are you trying to say
@user-gn8lz7yl2x
@user-gn8lz7yl2x 7 ай бұрын
好,看完兩輪後,結論 "看沒" 我是從物件導向開始的,有點被物件所干擾, 我覺得這東西有點像是連鎖反應,有點像是工廠類遊戲(異星工廠) 就是所有 "東西" 都是簡化到最簡單的"東西" 經過一個 "反應(函式)" 變成一個 "新東西" "新東西" 又 經過 "新反應" 又變成一個 "新新東西" 一直下去,直到出現我們要的"東西"這樣 感覺像是把數學的連續運算概念實現化的感覺
@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.
@user-tx4wj7qk4t
@user-tx4wj7qk4t 2 ай бұрын
Monad adds the ability to flatten on top of Applicatives
@drew-lee
@drew-lee 7 ай бұрын
未來有可能有中文發音版本嗎...
@james-kool
@james-kool 7 ай бұрын
有中文字幕喔~
@steamedeggeggegg
@steamedeggeggegg 7 ай бұрын
等下 怎麼變英文了lmao
@Oktokolo
@Oktokolo 6 ай бұрын
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.
@klaus-udokloppstedt6257
@klaus-udokloppstedt6257 5 ай бұрын
and yet another video that fails to explain what a monad is (to me). 😟
@TJ-hs1qm
@TJ-hs1qm 5 ай бұрын
those have helped me, in this order 1. kzbin.info/www/bejne/m2fXiH16atZrq9k 2. kzbin.info/www/bejne/f2mvk56ciLiFa5o 3. kzbin.info/www/bejne/mIDTe4R6fL6igdk 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.
@Sahuagin
@Sahuagin Ай бұрын
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 3 ай бұрын
too much hidden behaviour
@user-tx4wj7qk4t
@user-tx4wj7qk4t 2 ай бұрын
Nothing is hidden lol. There's entire fields of math behind this
The Absolute Best Intro to Monads For Software Engineers
15:12
Studying With Alex
Рет қаралды 572 М.
The Death of Monads? Direct Style Algebraic Effects
17:13
Impure Pics
Рет қаралды 15 М.
Eccentric clown jack #short #angel #clown
00:33
Super Beauty team
Рет қаралды 28 МЛН
1❤️
00:20
すしらーめん《りく》
Рет қаралды 33 МЛН
1🥺🎉 #thankyou
00:29
はじめしゃちょー(hajime)
Рет қаралды 79 МЛН
когда достали одноклассники!
00:49
БРУНО
Рет қаралды 3,9 МЛН
MET Talks | AI and Humanity | MEANT TV
29:31
MEANT TV
Рет қаралды 304
Dear Functional Bros
16:50
CodeAesthetic
Рет қаралды 451 М.
Functional Programming & Haskell - Computerphile
9:19
Computerphile
Рет қаралды 657 М.
Okay but WTF is a MONAD?????? #SoME2
18:18
TheSTEMGamer
Рет қаралды 64 М.
What the Heck Are Monads?!
21:08
ArjanCodes
Рет қаралды 67 М.
What is IO monad?
36:32
Tsoding
Рет қаралды 66 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 593 М.
Brian Beckman: Don't fear the Monad
1:07:10
jasonofthel33t
Рет қаралды 396 М.
iPhone 12 socket cleaning #fixit
0:30
Tamar DB (mt)
Рет қаралды 18 МЛН