tim
06/04/2020, 5:09 PM.map { someOp(); it }
the approach?streetsofboston
06/04/2020, 5:11 PMforEach
or also
type of functionality executing a pure side-effect (no expected results, just an action)
Usually, the method fold
is used for that.HieiJ
06/04/2020, 5:12 PMflatTap
that let you compose if your someOp() returns an effectHieiJ
06/04/2020, 5:14 PMpakoito
06/04/2020, 5:15 PMflatTap
to apply an effect and ignore the resulttim
06/04/2020, 5:15 PMEther.right(listOf(1,2,3))()
.map {
it * 10
} // [10, 20, 30]
.TAP/TEE {
saveToDatbase(it)
}
.map {
it * 10
} // [100, 200, 300]
pakoito
06/04/2020, 5:15 PMmap
shouldn’t be used for side-effects conceptuallytim
06/04/2020, 5:16 PMpakoito
06/04/2020, 5:16 PMtim
06/04/2020, 5:16 PMpakoito
06/04/2020, 5:16 PMpakoito
06/04/2020, 5:16 PMpakoito
06/04/2020, 5:16 PMtim
06/04/2020, 5:16 PMpakoito
06/04/2020, 5:17 PM.flatTap { IO { saveToDb(it) } }
pakoito
06/04/2020, 5:17 PMpakoito
06/04/2020, 5:17 PMpakoito
06/04/2020, 5:17 PMpakoito
06/04/2020, 5:17 PMpakoito
06/04/2020, 5:18 PMEither.catch
or something similarpakoito
06/04/2020, 5:18 PMtim
06/04/2020, 5:18 PMpakoito
06/04/2020, 5:18 PMtim
06/04/2020, 5:18 PMpakoito
06/04/2020, 5:19 PMEither.fx {
val a = !someApiForEither()
!log(a)
a + 1
}
tim
06/04/2020, 5:20 PMpakoito
06/04/2020, 5:22 PMpakoito
06/04/2020, 5:22 PMtim
06/04/2020, 5:22 PMtim
06/04/2020, 5:23 PMpakoito
06/04/2020, 5:23 PMtim
06/04/2020, 5:49 PMtim
06/04/2020, 5:55 PMval result: Either<String, Int> = randomEither(1)
.map {
it * 10
}
.flatTap {
println(it)
}
.map {
it * 10
}
println(result)
fun randomEither(value: Int): Either<String, Int> {
return if(Random.nextBoolean()) {
"fail".left()
} else {
value.right()
}
}
But the println is giving this error:tim
06/04/2020, 5:55 PMpakoito
06/04/2020, 6:20 PMBob Glamm
06/04/2020, 6:20 PMpakoito
06/04/2020, 6:20 PMtim
06/04/2020, 6:20 PMUnit
a side effect?pakoito
06/04/2020, 6:20 PMBob Glamm
06/04/2020, 6:21 PMtim
06/04/2020, 6:21 PMBob Glamm
06/04/2020, 6:21 PMtim
06/04/2020, 6:22 PMBob Glamm
06/04/2020, 6:22 PMpakoito
06/04/2020, 6:22 PMBob Glamm
06/04/2020, 6:22 PMpakoito
06/04/2020, 6:22 PMtim
06/04/2020, 6:22 PMBob Glamm
06/04/2020, 6:23 PMBob Glamm
06/04/2020, 6:24 PMpakoito
06/04/2020, 6:24 PMEither.fx { sideEffect() }
in 0.10 is kind of a trap because effect
is eager, so you are side-effectingBob Glamm
06/04/2020, 6:24 PMpakoito
06/04/2020, 6:24 PMEither.catch{ sideEffect(); 1 }
can only be called from a suspend function, so it’s always lazypakoito
06/04/2020, 6:24 PMpakoito
06/04/2020, 6:24 PMBob Glamm
06/04/2020, 6:25 PMBob Glamm
06/04/2020, 6:25 PMtim
06/04/2020, 6:30 PMEither.right(it)
for example ... so why wouldn't i just use map { somePureFunction(); it }
tim
06/04/2020, 6:31 PMBob Glamm
06/04/2020, 6:31 PM.flatTap()
is to primarily support side-effectsBob Glamm
06/04/2020, 6:32 PMtim
06/04/2020, 6:32 PMBob Glamm
06/04/2020, 6:32 PMA
cannot support side-effects, but M<A>
canBob Glamm
06/04/2020, 6:32 PM.flatTap
takes an A -> M<Unit>
Bob Glamm
06/04/2020, 6:33 PMEither
Bob Glamm
06/04/2020, 6:34 PMEither<L, ...>
, because Either
is only monadic if L
is fixed.tim
06/04/2020, 6:35 PM"a-string".right()
inside the flatTap ... then a subsequent map wouldn't be operating on "a-string" it would be operating on whatever the R value was before the flatTap call?tim
06/04/2020, 6:36 PMBob Glamm
06/04/2020, 6:36 PMdef processClaim(c: Claim): IO<ClaimCheck> = TODO()
processClaim(claim)
.flatTap(claimCheck -> IO(<http://LOG.info|LOG.info>("Claim check: $claimCheck")))
.flatMap(... more IO operations here operating on the claim check...)
Bob Glamm
06/04/2020, 6:36 PMBob Glamm
06/04/2020, 6:37 PMtim
06/04/2020, 6:37 PMtim
06/04/2020, 6:37 PMtim
06/04/2020, 6:38 PM