I often write code like this:
abstract class Foo [T[+_]: Monad]
{
def getStr (): T[String]
}
object Example
{
def f [T[+_]: Monad] (bar: String): ReaderT[T, Foo[T], String] = {
Kleisli { foo =>
for {
str <- foo.getStr ()
} yield str + bar
}
}
}
I know what the code above does, however I do not fully understand the inner workings of the Kleisli
part.
Looking at the source I can see that the Kleisli
type represents a function that takes type and returns another type wrapped in a higher order type:
/**
* Represents a function `A => M[B]`.
*/
How can that be used to explain the example I've posted above?
What else can Kleisli
be used for?