Because `List.map` from the kotlin stdlib is an in...
# arrow
j
Because
List.map
from the kotlin stdlib is an inline function and thus allows suspend functions inside of it if the scope from which it is called allows it.
Option.map
and pretty much all other arrow functions are not inline and thus don't allow it. Not entirely sure what the design decision is, but that is the reason this does not work ^^
☝️ 2
g
Thanks @Jannis, how can I get around this in an idiomatic way?
j
traverse
^^ It's always the answer 😅 Something like
getListOfStrings().traverse(M) { getIsSomethingTrueInHigherKind(it).map { booleanInput(it) } }
should work for both option and list
g
I hope
M
should be replaced by something like
Option.monad()
, right?
j
Depends,
M
is the applicative instance from whatever
getIsSomethingTrueInHigherKind
returns.
traverse
has the signature
fun <F, G, A, B> Kind<F, A>.traverse(AP: Applicative<G>, f: (A) -> Kind<G, B>): Kind<G, Kind<F, B>>
which is usually easier to understand as a generalization of
fun <F, G, A> Kind<F, Kind<G, A>>.sequence(AP: Applicative<G>): Kind<G, Kind<F, A>>
. @Imran/Malic has written a very good doc entry for the
Traverse
typeclass, I'd highly recommend reading it
💯 1
g
Thanks @Jannis 🙂
for now I tried this, not working:
Copy code
private fun optionMapper() = fx.monad {
        getOptionString().traverse(Option.monad()) { booleanInput(!getIsSomethingTrueInHigherKind(it)).toOption() }
    }
will read and try to correct
j
Yes, that
Option.monad
looks wrong
also the
!
is not needed
g
btw my `
Copy code
getIsSomethingTrueInHigherKind
returns a Kind<F, Boolean>
j
private fun optionMapper() = getOptionString().traverse(this /* refering to the monad instance from the class */) { getIsSomethingTrueInHigherKind(it).map { booleanInput(it) } }
should actually work
g
Awesome! thanks alot 🙂
j
traverse
is like a super power, threading applicative effects through any structure is a very useful concept to know and understand. Not in it's abstract ways, although that helps as well, but simply knowing it exists and how to use it helps a ton ^^
👍 3