A solution is a lazy version of `ap` (it's actuall...
# arrow
j
A solution is a lazy version of
ap
(it's actually solution to many many more problems 🙈 ) with that we would instantly return from
traverse
and execute/bind the
IO
as we go
So basically
fun Kind<F, A>.lazyAp(ff: () -> Kind<F, A -> B>): Kind<F, B>
and we are set. That solves this particular problem, it solves the problem in alternative which causes the default implementation for
some
and
many
to overflow and changes the semantics of
traverse
to actually short-circuit and not run the fold all the way and short on an operation by operation level. This is the second time this week someone fell over this issue, so I think we should really consider adding it. @pakoito @raulraja I know I proposed this a while ago, and the problems were that this only extends to
ap
and it's yet another method a datatype has to implement. Also this can be done via compiler plugin. But all these aside, a solution via a compiler plugin is at least a few weeks if not months out, so I think adding this and changing
traverse
+
some
+
many
now and later replacing it with a compiler plugin solution is a better idea.
👍 2
Also, even with a lazy ap. The resulting sequence is unusable as it will still stackoverflow, but at least the program will run the effects. The stackoverflow issue an entire different beast, because it happens when you evaluate a sequence that you construct by using
Sequence.plus
. This sucks
r
The changes you propose sounds good to me. With compiler plugins we can even bring solutions to stack safety without allocations down the road.
👍 3
j
I'll write something up tomorrow, it's a small change. The only remaining problem is that
fun Sequence<A>.plus(a: A): Sequence<A>
is not stacksafe once you force a long sequence. Don't know how to solve that
r
Can we override
+
for Sequence locally or not use plus but a stack safe version of it based on Eval or AndThen?
or is this happening internally outside our control?
Is the underlying problem that sequence is not stack safe?
j
One sec
r
If so then we should do our own version of it and provide
unsafeToSequence()
j
https://kotlinlang.slack.com/archives/C0922A726/p1576779566044700 Asked that in general and got a good explanation why this fails
Short story
the resulting iterator calls hasNext recursively and thus overflows
r
Then it’s something they could fix in the std lib replacing that for a loop
j
I was thinking this could be avoided... A sdt lib change seems best tho
r
it probably does not even need to change the public api and most likely they are not aware of it so this may be a legit bug if Sequence is meant to be stack safe on composition.