You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I really really love your write-up. I just have a small question.
Since a for-comprehension is a series of nested flatMaps where the computation is sequenced, and the result of a previous computation is used to get the next computation. Shouldn't the Run loop here be:
Short answer:
You're right, for-comp translates the flatMaps the way you wrote. But Cats Effect stacks the IOs in the run loop the way I wrote.🙂 It evaluates into the same thing.
Long answer:
Scala compiler translates the for-comp exactly how you wrote. But note that this format requires us to know the whole expression:
With IO, we don't know the rest of the chain. So we need to build what we have right now, and later when we have the subsequent IO, we will nest the first one inside it.
val io1 = FlatMap(
someIO,
someFunction
)
... and then later ...
val io2 = FlatMap(
io1,
someFunction
)
It comes down to (A flatMap B) flatMap C vs A flatMap (B flatMap C). Both expressions evaluate into the same thing. Here's some actual code:
// the "run loop way"
val inner = Option("whats up?").flatMap(_ => Option("reading"))
val outer = inner.flatMap(_ => Option("ah, input is up!"))
// the "for comp way"
lazy val first = Option("whats up?").flatMap(_ => second)
val second = Option("reading").flatMap(_ => Option("ah, input is up!"))
assert(outer == second)
See how in the second case I even had to use lazy val for the first, because it depends on the value of second. Compiler can do that because it sees the whole for-comp expression and just turns it into a chain of flatMaps, with one final map for the yield part. Run loop doesn't know the whole expression, and it's building it iteratively (when you give me the next IO, I will take what I have so far and shove it inside).
Hi, I really really love your write-up. I just have a small question.
Since a for-comprehension is a series of nested
flatMap
s where the computation is sequenced, and the result of a previous computation is used to get the next computation. Shouldn't the Run loop here be:where the function
k
is what leads to the nextFlatMap
?Using a toy interpreter, funny enough, I can't seem to see any difference between the two.
The text was updated successfully, but these errors were encountered: