Jannis
12/19/2019, 6:19 PMgenerateSequence(0) { it + 1 }.take(100000).fold(emptySequence<Int>()) { acc, v ->
acc + v
}.first() => stackoverflows
Also this works fine:
generateSequence(0) { it + 1 }.take(100000).fold(emptySequence<Int>()) { acc, v ->
sequenceOf(v) + acc
}.first() => works fineKroppeb
12/19/2019, 6:27 PMKroppeb
12/19/2019, 6:27 PMJannis
12/19/2019, 6:36 PMfold but that is not the case:
generateSequence(0) { it + 1 }.take(100000).fold(0) { acc, v ->
acc + v
}.also(::println) => 704982704
This works fine.
The problem is that fun Sequence<A>.plus(a: A): Sequence<A> blows the stack when the resulting sequence is forced (at least with the example above)
What are you trying to do exactly?Nothing in particular, just investigating a stackoverflow someone had with arrow a while back which turned out to be a more general problem
Kroppeb
12/19/2019, 6:44 PMhasNext() function on the iterator, will have to check the hasNext() of the previous addition, which whill also call `hasNext()`etc, etc.Kroppeb
12/19/2019, 6:44 PMJannis
12/19/2019, 7:09 PMfun Sequence<A>.plus(a: Sequence<A>): Sequence<A> ?
Also this sounds like something that can be avoided, not sure how thoKroppeb
12/19/2019, 7:09 PMfold(0) { acc, v ->acc + v}?Jannis
12/19/2019, 7:10 PMgenerateSequence(0) { it + 1 }.take(100000).fold(emptySequence<Int>()) { acc, v ->
sequenceOf(v) + acc
}.first() => works fineKroppeb
12/19/2019, 7:11 PMKroppeb
12/19/2019, 7:12 PMlast() will cause a stackoverflowJannis
12/19/2019, 7:12 PM.last() doesn't instantly crash with a soe either, it just takes agesKroppeb
12/19/2019, 7:12 PMsoe?Jannis
12/19/2019, 7:12 PMKroppeb
12/19/2019, 7:13 PMKroppeb
12/19/2019, 7:13 PMJannis
12/19/2019, 7:13 PMKroppeb
12/19/2019, 7:16 PMJannis
12/19/2019, 7:21 PMJannis
12/19/2019, 8:16 PMSequence offers a similar method like traverse which I highly doubt as that's a very arrow specific methodKroppeb
12/19/2019, 8:19 PMI am pretty sure we need the fold thereWell the first case you is a nop that crashes if that crashes if the input is too big.
Kroppeb
12/19/2019, 8:19 PMJannis
12/19/2019, 8:21 PMJannis
12/19/2019, 8:21 PMJannis
12/19/2019, 8:22 PMApplicativesKroppeb
12/19/2019, 8:24 PMJannis
12/19/2019, 8:25 PMJannis
12/19/2019, 8:25 PMJannis
12/19/2019, 8:26 PMEither<E, A> you can short circuit the when you encounter a Left(e: E) . Different datatypes result in very different behaviour. It is a very useful combinator to haveKroppeb
12/19/2019, 8:27 PMJannis
12/19/2019, 8:29 PMJannis
12/19/2019, 8:31 PM