The below code snippet came from here and from the context I understand what it is doing via pattern matching, but how it is doing it and that operator(s) has me for a loop. MSDN was not helpful. If that is an operator, does it have a name? Sorry if there is some missing google fu on my part.
let (>=>) f1 f2 arg = match f1 arg with | Ok data -> f2 data | Error e -> Error e
UPDATE: Indeed it might be the case that the operator is overloaded, and thanks for the link to the other SO question, I guess the core of my question was what is that overloaded operator's semantics. Looking at the other links (>>=) seems to be the typical bind operator.
That's the Kleisli composition operator for monads. It allows you to compose functions with signatures like 'a -> M<'b>
and 'b -> M<'c'>
where M
is monadic: in your case the Result<'t>
from the linked article.
>=>
is really just a function composition, but >>
wouldn't work here since the return type of the first function isn't the argument of the second one - it is wrapped in a Result<'t>
and needs to be unwrapped, which is exactly what >=>
implementation does.
It could be defined in terms of >>=
as well:
let (>=>) f1 f2 arg = f1 arg >>= f2
It seems that Haskell's Control.Monad package uses this definition. The full type signature might also be helpful (taken from here):
-- | Left-to-right Kleisli composition of monads. (>=>) :: Monad m => (a -> m b) -> (b -> m c) -> (a -> m c) f >=> g = \x -> f x >>= g
Yet another fun fact is that Kleisli composition makes the three monad laws easier to express by only using functions (and in my opinion it makes them much clearer):
return >=> g ≡ g
f >=> return ≡ f
(f >=> g) >=> h ≡ f >=> (g >=> h)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With