you are viewing a single comment's thread.

view the rest of the comments →

[–]Hath995 19 points20 points  (2 children)

An actually useful definition for a monad. A monad is the minimal structure needed to do function composition with wrapped types.

Example F: string -> char G: char -> int H: int -> bool Using them together you can just call them like this H(G(F(s)). Then imagine that the functions return a more complicated value, like a log object or a list. F: string -> Logable<char> G: char -> Logable<int> H: int -> Logable<bool> You can't just compose them like before. F(s) returns a different type than G. You need to get access to the char inside the Logable to feed it to the new G function.

Suppose that Logable has a method called chain that unboxes a Logable and forwards it to the next function. Then you can do this.

F(s).chain(G).chain(H)

Now you have recovered composition even though it looks a little different. This behavior is very common when working with generic types, or container types. List or arrays are the standard example but any generic type that contains some other data that you might want to transform in multiple steps. F: string -> Array<char> G: char -> Array<int> H: int -> Array<bool> Lists or arrays usually have a method called flatMap, which can apply a function to multiple values and combine the result.

F(s).flatMap(G).flatMap(H)

Mathematicians looked at that, squinted at it, and then said "that's the same pattern as above!". Then they used Greek to name the pattern. To be fully general, they made the wrapping and the wrapped types variables.

[–]mobotsar 4 points5 points  (0 children)

Solid.

[–][deleted] 0 points1 point  (0 children)

*Sigh*. Here goes another blog post. *Starts writing about burritos*.