thana
03/26/2020, 12:55 PMSequence
not Iterable
?Michael de Kaste
03/26/2020, 1:01 PMMichael de Kaste
03/26/2020, 1:02 PMMichael de Kaste
03/26/2020, 1:03 PMthana
03/26/2020, 1:04 PMSequence
provides all the methods, an Iterable
must provide, toothana
03/26/2020, 1:05 PMitetator
)Michael de Kaste
03/26/2020, 1:05 PMstreetsofboston
03/26/2020, 1:10 PMhasNext()
methods on that Iterator will cause the backing Sequence to realize at least its next element (ie its generator/builder will yield
) to be able to look-ahead.
This can have side-effects, as where the hasNext()
of a ‘plain’ Iterable’s Iterator does not have side effects, since it can easily look ahead.thana
03/26/2020, 1:13 PMiterator()
?streetsofboston
03/26/2020, 1:14 PMthana
03/26/2020, 1:15 PMstreetsofboston
03/26/2020, 1:16 PMCollection.asSequence()
sequences, the hasNext()
is fine. It may get a bit problematic if you build your own ( =sequence { … }
) .Michael de Kaste
03/26/2020, 1:16 PMMichael de Kaste
03/26/2020, 1:17 PMset
a list
is the same questionthana
03/26/2020, 1:17 PMthana
03/26/2020, 1:17 PMstreetsofboston
03/26/2020, 1:17 PMdiesieben07
03/26/2020, 1:18 PMIterable
and Sequence
also behave completely differently.diesieben07
03/26/2020, 1:18 PMIterable
=> eager
Sequence
=> lazystreetsofboston
03/26/2020, 1:18 PMthana
03/26/2020, 1:18 PMdiesieben07
03/26/2020, 1:18 PMthana
03/26/2020, 1:19 PMstreetsofboston
03/26/2020, 1:19 PMthana
03/26/2020, 1:24 PMIterable
does not even mention how the Iterator
returned by iterator()
hast to behavethana
03/26/2020, 1:25 PMIterator
does not mention hasNext
may not have side effectsstreetsofboston
03/26/2020, 1:26 PMstreetsofboston
03/26/2020, 1:26 PMhasNext()
on an iterator from a Sequence may cause its sequence-builder to resume from a suspending yield()
… which may or may not cause a side effect depending on your builder; the hasNext() call may effectively generate the next element/item.Kroppeb
03/26/2020, 1:56 PMno the bave the same according to the interfaceWell, they do not behave the same. Both have to give an iterator as thats how they mesh with for loops.
Kroppeb
03/26/2020, 1:59 PMilya.gorbunov
03/26/2020, 3:06 PMSequence
and Iterable
are structurally the same interfaces. The difference is how extension operations are implemented upon them.thana
03/27/2020, 8:09 AMthana
03/27/2020, 8:10 AMdiesieben07
03/27/2020, 8:44 AMIterable
is eager, Sequence
is lazy. That is not similar behavior.thana
03/27/2020, 8:59 AMthana
03/27/2020, 9:00 AMdiesieben07
03/27/2020, 9:02 AMdiesieben07
03/27/2020, 9:03 AMSequence
contract says: This operates lazily.
Iterable
contract doesn't say that.thana
03/27/2020, 9:03 AMSequence
provide the method iterator()
thana
03/27/2020, 9:04 AMdiesieben07
03/27/2020, 9:04 AMdiesieben07
03/27/2020, 9:04 AMIterable
(the interface).thana
03/27/2020, 9:04 AMthana
03/27/2020, 9:07 AMList
and Set
shouldn't share the same itnerface because they differ in implementationdiesieben07
03/27/2020, 9:07 AMCollection
, whose contract they follow perfectly fine.thana
03/27/2020, 9:07 AMdiesieben07
03/27/2020, 9:08 AMthana
03/27/2020, 9:08 AMSet
containing it only once they other is a`List` containing it multiple timesthana
03/27/2020, 9:09 AMdiesieben07
03/27/2020, 9:09 AMCollection
does not prohibit either of thosethana
03/27/2020, 9:10 AMIterable
doesn't say that evaluation must be eagerdiesieben07
03/27/2020, 9:11 AMdiesieben07
03/27/2020, 9:11 AMthana
03/27/2020, 9:15 AMdiesieben07
03/27/2020, 9:15 AMdiesieben07
03/27/2020, 9:16 AMthana
03/27/2020, 9:16 AMIterable
because everything else of the implementation says it's itereable. And if i were sure it's really different i would NOT provide an iterator()
tht returns the good old java Iterator
diesieben07
03/27/2020, 9:18 AMIterable
you now have two filter
extensions on Sequence
, the eager Iterable
version (that returns a List) and the lazy Sequence
version that returns a Sequence
. Thats not very user friendly.diesieben07
03/27/2020, 9:19 AMoperator fun iterator(): Iterator
"thana
03/27/2020, 9:21 AMIterable
but cannot implement it because else we would get trouble regardig extensionsthana
03/27/2020, 9:21 AMthana
03/27/2020, 9:21 AMKroppeb
03/27/2020, 9:24 AMIterable.map()
function on a Sequence
it could crash the JVM as a Sequence could generate an infinite amount of elements, and you only have a finite amount of RAM.Kroppeb
03/27/2020, 9:25 AMthana
03/27/2020, 9:26 AMStream
. Also again, the interface does not say anything about there must be a finite amount of elements in the Iterable
of ven an amount of finite elements that fit into the heap 😉Kroppeb
03/27/2020, 9:32 AMStream
is actually very similar to a Kotlin Sequence
. And it also doesn't extend Iterable
thana
03/27/2020, 9:49 AM