Hello, I got a funny one today. I had an IO with a...
# arrow
y
Hello, I got a funny one today. I had an IO with a list of items inside, and I needed to apply an IO operation to each element in the list. The issue is solved in two ways, but I'm baffled as why the second-to-last and last options are different. I'm evidently missing something and I would LOVE to get it out of my head.
I'm seeing now that it's actually wrapped in a
flatMap
, and not a
map
so that's probably the piece I'm missing. I still don't fully get it.
s
You’re dealing with 2 HKTs here which both have a
fix
method.
sequence
for
List<IO<A>>
returns
IO<Kind<ForListK, A>>
What you’re doing in the first snippet is call
fix
on
IO
before you return it to
flatMap
which is no-op.
So what you want to do instead is call
fix
on the nested
HKT
there instead using
map
as well.
Copy code
val appliedFixSkins: IO<ListK<BananaSkin>> = getBananas().flatMap { bananas ->
  bananas.map { banana ->
    eat(banana)
  }.sequence(IO.applicative()).map { it.fix() }
}
Luckily this can all disappear completely with Arrow Meta! 🙂
❤️ 4
y
Oh, thanks a lot, man. I was fixing the IO instead of the Kind.
s
You’re welcome! 👍
👍 1
a
I bumped into the exact same case last night, had to
.map { it.fix() }
, which looks and reads awkward. Can't wait for Arrow Meta. 👍
2