13 ms·
This article made it all click for me: https://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html https://adit.io/posts/2013-04-17-fu
by gaganyaan 5y ago
This article made it all click for me:
https://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html https://adit.io/posts/2013-04-17-functors,_applicatives,_and...
Not sure why, but treating everything as boxes makes all of the fancy FP abstractions on top make sense to me.
I think for C/assembly, the higher levels of abstractions aren't something you'll care about, because they're too costly. However, and apologies for the Rust code in a C++ thread, but I wrote this today and I think it demonstrates either the value or the horror of monads et al.
let channel = app
.channel
.map(|c| Channel::from_str(&c))
.transpose()?
.unwrap_or(Channel::Stable);
In plain words, I have a box that might contain a String (app.channel, which is `Option<String>` in Rust, or roughly `char *` in C). If it does have a String, I want to parse the String into a `Channel` enum and return an error if that fails. If it doesn't contain one, I want to use a default enum value of `Channel::Stable`.
The equivalent Rust code written in a more C-like style would be more verbose, something like this:
let channel = if app.channel.is_some() {
let parsed = Channel::from_str(&app.channel.unwrap());
if parsed.is_ok() {
parsed.unwrap()
} else {
return Err(parsed);
}
} else {
Channel::Stable
}