Monads in D
deadalnix via Digitalmars-d
digitalmars-d at puremagic.com
Sat Jun 11 11:27:19 PDT 2016
On Saturday, 11 June 2016 at 14:26:20 UTC, Atila Neves wrote:
> I give you a facsimile of a Haskell do block:
>
> with(Maybe!int) {
> return_(5).bind!(a => return_(a +
> 1)).shouldEqual(just(6));
> nothing.bind!(a => return_(a + 1)).shouldEqual(nothing);
> return_(8).bind!(a => nothing).bind!(a => return_(a +
> 1)).shouldEqual(nothing);
> }
>
> with(Maybe!string) {
> return_("foo").bind!(a => return_(a ~
> "bar")).shouldEqual(just("foobar"));
> nothing.bind!(a => return_(a ~
> "bar")).shouldEqual(nothing);
> return_("foo").bind!(a => return_(a ~ "bar")).bind!(a
> => nothing).shouldEqual(nothing);
> }
>
>
> Why? Because I could, I don't plan on using this for anything
> serious. I think "with" is my favourite D feature right now. I
> also wrote the Writer and State monads (not that D needs them):
>
> https://github.com/atilaneves/felix
>
> I tried overloading `>>>` for bind (closest overloadable
> operator to `>>=`) but it went horribly wrong. I always get
> problems when I try to pass lambdas as runtime values instead
> of template parameters.
>
>
> Atila
Honestly, the whole bind/return is just a giant NIH. >>> and >>=
are its symptoms. There is a reason why nobody understands jack
shit about monad even after 10s of tutorial when they aren't even
hard in any way: because haskell and alike have made all that is
in their power to obfuscate what is a monad.
I could go on, but this guy already did it way better that I can:
https://www.infoq.com/presentations/functional-pros-cons
The part about monad starts 25mins in.
More information about the Digitalmars-d
mailing list