Someone else and not an expert. But Maybe types are implemented with Monads, Maybe is a common monad.
Its how rust does error handling for example, you have to test a return value for “something or nothing” but you can pass the monadic value and handle the error later, in go you have to handle the error explicitly (nearly) all the time.
nick@campfyre.nickwebster.dev 1 year ago
Having a
Result
monad that could represent either the data from a successful operation or an error. This can be generalised to theEither
monad too.serenity@lemmy.world 1 year ago
oessessnex@programming.dev 1 year ago
Nope. Monads enable you to redefine how statements work.
Let’s say you have a program and use an Error 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:
(a => rest)(expr);
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:
bind((a => rest), expr)
Here “bind” redefines how the result of expr is passed to the anonymous function.
If you implement bind as:
B bind(Func f, A result_expr) { return f(result_expr); }
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.</b>
barsoap@lemm.ee 1 year ago
“Some generic class” with specific methods and laws, Monads are an algebraic structure and you want those laws included same as if you enable some type to use
+
you want to have a0
somewhere andx + 0 == x
to hold.In Rust,
Result
andOption
actually are monads. Let’s takeOption
as example:pure x
isJust(x)
a >>= b
isa.and_then(b)
Then we have:
Just(x).and_then(f)
=f(x)
x.and_then(Just)
=x
m.and_then(g).and_then(h)
=m.and_then(|x| g(x).and_then(h)
Why those laws? Because following them avoids surprises like
x + 0 /= x
.Rust’s type system isn’t powerful enough to have a Monad trait (lack of HKTs) hence why you can’t write code that works with any type that implements that kind of interface.
Result
names>>=
and_then
, just likeOption
does so the code reads the same but you’ll have to choose betweenOption
orResult
in the type signature, the code can’t be properly generic over it.m_f@midwest.social 1 year ago
This is the best explanation I’ve ever seen of monads: adit.io/…/2013-04-17-functors,_applicatives,_and_…
For some reason, you’ll find a lot of really bad explanations of monads, like “programmable semi-colons”. Ignore those, and check out the link.