This is a simple example of use of a mysterious beast called a Kleisli arrow.
I will be using scalaz for my incantations as it comes with the necessary potions.
What is the Problem?
You have functions that take a simple type and return higher kinded types like Options or Lists, and you need to compose those functions. You might think you need to get the first result, pattern match on the it, apply it to the second function, repeat. Sounds rather cumbersome.
Kleisli FTW
The intuition is this:
a Kleisli is function composition for Monads.
Hmm, lets try again:
if you have functions that return kinds of things, like Lists, Options, etc, then you can use a Kleisli to compose those functions.
The Example
I was asked by bhericher below about composing functions returning different types. Here is a solution but I am not sure if its the best way:
Further reading
- Kleisli Arrow example with Files at Cake Solutions
- Scalaz Example
- Kleili Arrow at the Haskell Wiki