#readwise # Kleisli Composition ![rw-book-cover](https://blog.ploeh.dk/assets/themes/ploeh/images/favicons/favicon.png) ## Metadata - Author: [[Mark Seemann]] - Full Title: Kleisli Composition - URL: https://blog.ploeh.dk/2022/04/04/kleisli-composition/ ## Highlights - Imagine some monad - any monad. It could be *Maybe* (AKA *Option*), *Either* (AKA *Result*), *Task*, *State*, etcetera. In [Haskell](https://www.haskell.org) notation we denote it `m`. ... If we generalise this notation, we would write a monadic function like this: `a -> m b` ... it seems intuitive that you can also compose monadic functions: `(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c` The [`>=>`](https://hackage.haskell.org/package/base/docs/Control-Monad.html#v:-62--61--62-) operator is known as the *fish* operator ... This is known as *Kleisli composition*. ... ```csharp public static Func<T, Monad<T2>> Compose<T, T1, T2>( this Func<T, Monad<T1>> action1, Func<T1, Monad<T2>> action2) { return x => action1(x).SelectMany(action2); } ``` - Notice that Kleisli composition works for any monad. You don't need any new abilities in order to compose a monad in this way. The composition exclusively relies on `SelectMany` (monadic *bind*). ([View Highlight](https://read.readwise.io/read/01gwp0na3ahf1ad5wz2f7a04rz)) - Kleisli composition lets you compose two (or more) compatible monadic functions into a single monadic function. ([View Highlight](https://read.readwise.io/read/01gwp0q0yg6d6h49jxx5c9fcvj))