this post was submitted on 14 Aug 2023
1413 points (98.0% liked)
Programmer Humor
19589 readers
771 users here now
Welcome to Programmer Humor!
This is a place where you can post jokes, memes, humor, etc. related to programming!
For sharing awful code theres also Programming Horror.
Rules
- Keep content in english
- No advertisements
- Posts must be related to programming or programmer topics
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
Nope. Monads enable you to redefine how statements work.
Let's say you have a program and use an Error[T] data type which can either be Ok {Value: T} or Error:
Each statement has the following form:
You first evaluate the "expr" part and bind/store the result in variable a, and evaluate the "rest" of the program.
You could represent the same thing using an anonymous function you evaluate right away:
In a normal statement you just pass the result of "expr" to the function directly. The monad allows you to redefine that part.
You instead write:
Here "bind" redefines how the result of expr is passed to the anonymous function.
If you implement bind as:
Then you get normal statements.
If you implement bind as:
You get statements with error handling.
So in an above example if the result of foo() is Error, the result of the statement is Error and the rest of the program is not evaluated. Otherwise, if the result of foo() is Ok {Value = 3}, you pass 3 to the rest of the program and you get a final result Ok {Value = 4}.
So the whole idea is that you hide the if Error part by redefining how the statements are interpreted.