Jannis
02/15/2020, 12:17 PMList.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 ^^Gopal S Akshintala
02/15/2020, 12:18 PMJannis
02/15/2020, 12:21 PMtraverse
^^ It's always the answer 😅
Something like getListOfStrings().traverse(M) { getIsSomethingTrueInHigherKind(it).map { booleanInput(it) } }
should work for both option and listGopal S Akshintala
02/15/2020, 12:25 PMM
should be replaced by something like Option.monad()
, right?Jannis
02/15/2020, 12:30 PMM
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 itJannis
02/15/2020, 12:31 PMGopal S Akshintala
02/15/2020, 12:31 PMGopal S Akshintala
02/15/2020, 12:32 PMprivate fun optionMapper() = fx.monad {
getOptionString().traverse(Option.monad()) { booleanInput(!getIsSomethingTrueInHigherKind(it)).toOption() }
}
Gopal S Akshintala
02/15/2020, 12:32 PMJannis
02/15/2020, 12:32 PMOption.monad
looks wrongJannis
02/15/2020, 12:32 PM!
is not neededGopal S Akshintala
02/15/2020, 12:33 PMgetIsSomethingTrueInHigherKind
returns a Kind<F, Boolean>Jannis
02/15/2020, 12:34 PMprivate fun optionMapper() = getOptionString().traverse(this /* refering to the monad instance from the class */) { getIsSomethingTrueInHigherKind(it).map { booleanInput(it) } }
should actually workGopal S Akshintala
02/15/2020, 12:35 PMJannis
02/15/2020, 12:37 PMtraverse
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 ^^