I was looking to learn about the Kleisli data type...
# arrow
t
I was looking to learn about the Kleisli data type and was reading Arrow's docs on it (http://arrow-kt.io/docs/datatypes/kleisli/). I do not understand what benefit does the data type provide in the first example. It seems that the method can be rewritten without the Kleisli and it will return the same result:
Copy code
fun String.safeToInt(): Option<Int> {
    return if (this.toCharArray().all { it.isDigit() }) Some(this.toInt()) else None
}
Am I missing something here?
or just straight up removing Kleisli, but leaving two functions as in the example:
Copy code
val optionInt = { str: String ->
    if (str.toCharArray().all { it.isDigit() }) Some(str.toInt()) else None
}

fun String.safeToInt(): Option<Int> {
    return optionInt(this)
}
p
That's correct, Kleisli is the reification of a parameter on a function. It can be replaced by such parameter or an extension function
the advantages is that you can define functions like map and flatmap for it, and pass it around as a value
also, abstract over it 😄
The other advantage is that Kleisli allow composition over wrapped elements
Imagine your ApiClient comes conditionally from a Future, or a Try, or an Observable
Kleisli knows how to unwrap & compose those types
t
right, the mechanics of the type still evade me though 😄 So I am having difficulty grasping the benefit of the datatype. It seems the further
flatMap()
example does not compile
though this compiles:
Copy code
optionIntKleisli.flatMap {
            optionDoubleKleisli
        }.run { "1" }
will have to look into it later
p
in the docs?
Lemme flag it, we'll take a look over the weekend.
t
yes, the example in the docs did not seem to compile
thanks for the explanation attempts though
p
@tginiotis if you can follow Scala, try this talk. Somewhere in the middle it starts using Kleisli in interesting ways

https://www.youtube.com/watch?v=urdtmx4h5LEâ–¾

t
great, thanks!