News for hackers. Stuff that matters.
Re:Don't Use Booleans by the_gipsy on Tuesday July 09, 2024 @06:01AM (#40914253)
An Option<Enum> has the advantage that you con chain it with other Option/Result operations like map, and_then, etc. I think this would be called being "monadic".

The advantage of Enum with Missing variant is that it's more expressive, and that you don't have to nest access in the Some matching.

Generally I prefer Option<Enum>, and often you can match directly with Some(Variant) to avoid nested matches.

Re:Don't Use Booleans by simondw on Tuesday July 09, 2024 @06:27AM (#40914436)
> map, and_then, etc. I think this would be called being "monadic".

Strictly speaking, I think providing "map" just makes it functorial. Monadic would need a flatmap. (In addition to the other functor and monad requirements, of course.)

Re:Don't Use Booleans by the_gipsy on Tuesday July 09, 2024 @08:09AM (#40915139)
So what would you call something like "it implements some common operations", like these?

For example, the Option and Result type both have functions like "map", they do the same thing just on different types. They're not quite generic in that sense, but on a high level they seem so.

Another example are reactive libraries. Everything is pegged into some common operations like map, take, and so on.

Re:Don't Use Booleans by hardwaresofton on Tuesday July 09, 2024 @07:03AM (#40914646)
You're right, and I'd add on that monads only need pure and bind (>>=), das it!

    pure :: Monad m => a -> M a

    (>>=) :: Monad m => M a -> (a -> M b) -> M b
pure is sometimes called return and >>= is sometimes called bind and flat map.

I was unsure that bind was the same as flatmap but it looks like it is.

Re:Don't Use Booleans by tubthumper8 on Tuesday July 09, 2024 @11:18AM (#40917112)
The "and_then" that they mentioned is the flatmap