What F# sorcery is this (>=>)?

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):

  • Left identity: return >=> g ≡ g
  • Right identity: f >=> return ≡ f
  • Associativity: (f >=> g) >=> h ≡ f >=> (g >=> h)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)