| ▲ | louthy 2 days ago | |
I think you're describing part of the bind function, which is part of the definition of the monad interface:
But the full definition is:
In C#, it would look like this:
Assuming a future version of C# that supports higher-kinds that is.In my language-ext library, I achieve a higher-kinded monad trait [1], like so:
Which is what the original comment is about. Most people in C# are not creating monads when they implement Bind or SelectMany for their type. They are simply making 'do-notation' work (LINQ). The monad abstraction isn't there until you define the Monad trait that allows the writing of any function constrained to said trait.For example, a `When` function that runs the `then` monad when the `predicate` monad returns `true` for its bound value:
This will work for any monad, `Option`, `List`, `Reader`, ... or anything that defines the trait.So types like `Option` are just pure data-types. They become monads when the Monad trait is implemented for them. The monad trait can be implemented for data-types and function-types. Reader, for example, has the Monad trait implemented for the function: Func<E, A> btw, monads also inherit behaviour from applicative and functor. The ability to lift pure values into the monad (a -> m a) is vital to making monads useful. This is `select` in LINQ, `pure` in Haskell's Applicative, and `Pure` in language-ext's Applicative [2]. [1] https://github.com/louthy/language-ext/blob/main/LanguageExt... [2] https://github.com/louthy/language-ext/blob/main/LanguageExt... | ||