pakoito
03/16/2020, 11:12 AMmapN
to execute in order. If order matters, then use flatMap
Gopal S Akshintala
03/16/2020, 11:15 AMpakoito
03/16/2020, 11:16 AMpakoito
03/16/2020, 11:16 AM./gradlew runAnk
for the whole repopakoito
03/16/2020, 11:16 AMpakoito
03/16/2020, 11:17 AMGopal S Akshintala
03/16/2020, 11:17 AMJannis
03/16/2020, 12:59 PMGopal S Akshintala
03/16/2020, 2:15 PMraulraja
03/16/2020, 6:35 PMraulraja
03/16/2020, 6:36 PMraulraja
03/16/2020, 6:37 PMraulraja
03/16/2020, 6:37 PMGopal S Akshintala
03/17/2020, 9:37 AMmapN
remains the same as before. May I know which version has the fix available (I was previously on 0.10.5-SNAPSHOT). ThanksJannis
03/17/2020, 11:15 AMproduct
from Apply
. https://github.com/arrow-kt/arrow-core/blob/master/arrow-core-data/src/main/kotlin/arrow/typeclasses/Apply.kt#L300
As you can see it first applies the other
argument to this
. Since tupledN
and mapN
use that it messes up order. When I checked order last time I must have missed them... Will do a quick pr later todayGopal S Akshintala
03/17/2020, 11:16 AMGopal S Akshintala
03/18/2020, 10:11 AMmapN
is fixed to act from left to right, will it also take care of Short-circuting/fail-fast (means if u found a left, rest of them should not be evaluated). Currently, mapN
evaluates all the expressions, like below
@Test
fun `Left * Right`() {
val product = Either.applicative<String>().run {
mapN(
"left".left(),
getRight()
) {}
}.fix()
product.fold({ assertEquals("left", it) }, {})
}
private fun getRight(): Either<String, String> {
println("This should not be printed") // vvv But printed
return "right".right()
}
Gopal S Akshintala
03/18/2020, 10:57 AMApplicativeError
as well
@Test
fun `Left * Right`() {
val product = Either.applicativeError<String>().run {
mapN(
raiseAnError(),
getRight()
) {}
}.fix()
product.fold({ assertEquals("error", it) }, {})
}
private fun EitherApplicativeError<String>.raiseAnError(): Either<String, String> {
println("raising an error")
return this.raiseError("error")
}
private fun getRight(): Either<String, String> {
println("This should not be printed")
return "right".right()
}
pakoito
03/18/2020, 1:15 PMpakoito
03/18/2020, 1:16 PMmapN
pakoito
03/18/2020, 1:16 PMGopal S Akshintala
03/18/2020, 1:18 PMmapN
? It always asks for result isn’tpakoito
03/18/2020, 1:18 PMGopal S Akshintala
03/18/2020, 1:24 PMApplicativeError
looking for a failfast/shortcircuit, where I don’t want other methods to be called, if one of the method does raiseError
Gopal S Akshintala
03/18/2020, 1:24 PMGopal S Akshintala
03/18/2020, 1:29 PMApplicativeError
and Async
in this code @pakoitopakoito
03/18/2020, 1:32 PMmapN(later { }, later { })
: https://t.ly/kv8BJpakoito
03/18/2020, 1:32 PMKind<F, Kind<G, A>>
so you’ll need to flatMap that outer Kind on the callerpakoito
03/18/2020, 1:32 PMGopal S Akshintala
03/18/2020, 1:34 PMGopal S Akshintala
03/18/2020, 1:34 PMGopal S Akshintala
03/18/2020, 2:53 PM@Test
fun `IO ApplicativeError`() {
val product: Either<String, Unit> = IO.async<Nothing>().run {
Either.applicativeError<String>().run {
mapN(
later { raiseSomeError() },
later { justString() }
) {}
}
}.fix().unsafeRunSyncEither()
product.fold({ assertEquals("error", it) }, {})
}
private fun <S> ApplicativeError<S, String>.raiseSomeError(): Kind<S, String> {
println("raising an error")
return raiseError("error")
}
private fun <S> ApplicativeError<S, String>.justString(): Kind<S, String> {
println("This should not be printed")
return just("just")
}
pakoito
03/18/2020, 2:53 PMpakoito
03/18/2020, 2:53 PMpakoito
03/18/2020, 2:53 PMpakoito
03/18/2020, 2:54 PM{}
of the mapN
pakoito
03/18/2020, 2:54 PMpakoito
03/18/2020, 2:54 PMGopal S Akshintala
03/18/2020, 2:54 PMmapN
needs both their result to do productGopal S Akshintala
03/18/2020, 2:54 PMpakoito
03/18/2020, 2:54 PMpakoito
03/18/2020, 2:55 PMpakoito
03/18/2020, 2:57 PMval product: Either<String, Unit> =
IO.applicativeError().mapN(
IO.defer { raiseSomeError() },
IO.defer { justString() }
) {}
}.unsafeRunSyncEither()
Gopal S Akshintala
03/18/2020, 3:23 PMGopal S Akshintala
03/18/2020, 3:23 PM@Test
fun `IO ApplicativeError 2`() {
val product =
IO.applicativeError<Nothing>().mapN(
IO.defer { raiseSomeError() },
IO.defer { justString() }
) {}.fix().unsafeRunSyncEither()
product.fold({ assertEquals("error", it) }, {})
}
private fun <S> ApplicativeError<S, Throwable>.raiseSomeError(): Kind2<S, Throwable, Unit> {
println("raising an error")
return raiseError(RuntimeException("error"))
}
private fun <S> ApplicativeError<S, String>.justString(): Kind<S, String> {
println("This should not be printed")
return just("just")
}
pakoito
03/18/2020, 3:23 PMpakoito
03/18/2020, 3:24 PMasync()
part that allows calling into the other two 😄Gopal S Akshintala
03/18/2020, 3:25 PMpakoito
03/18/2020, 3:27 PMasync()
is the typeclass, with ApplicativeError should be enoughpakoito
03/18/2020, 3:27 PMpakoito
03/18/2020, 3:27 PMGopal S Akshintala
03/18/2020, 3:28 PMpakoito
03/18/2020, 3:28 PMpakoito
03/18/2020, 3:28 PMpakoito
03/18/2020, 3:29 PMApplicativeError<S, Throwable>
the error is fixed to Throwable
too, so do applicativeError<Throwable>()Gopal S Akshintala
03/18/2020, 3:30 PMGopal S Akshintala
03/18/2020, 3:30 PMpakoito
03/18/2020, 3:33 PMApplicativeError<S, String>
, applicativeError<String>()
Gopal S Akshintala
03/18/2020, 4:44 PM