A frightfully inefficient way to add elements to the beginning of a corecursive list.
A frightfully inefficient way to add elements to the beginning of
a corecursive list. It is correct, and reasonable enough for
one-or-two-time use on a particular list, but be warned that
iterative cons is necessarily a very inefficient way to build up
a CorecursiveList
.
You can see why by taking a look at its choice of state. cons
adds a Maybe
to the state of the tail list, so if you cons
four elements onto someCList
, its state will be
Maybe[Maybe[Maybe[Maybe[someCList.S]]]]
, and so on. This is
utterly unsustainable for iterative consing.
Instead, try apply
, or another more wholemeal-style
combinator. You might also cons up a different structure, then
transform that to a CorecursiveList
wholesale and append
or
plus
it on; that's reasonably efficient, too.
Any LinearSeq
converts to a CorecursiveList
efficiently.
Any LinearSeq
converts to a CorecursiveList
efficiently. No
natural transformation from CorecursiveList
to List
exists,
because representation of infinite lists is not guaranteed. Use
streamIso
for such cases instead.
Any IndexedSeq
converts to a CorecursiveList
efficiently.
Any IndexedSeq
converts to a CorecursiveList
efficiently. No
natural transformation from CorecursiveList
to Vector
exists, because representation of infinite lists is not
guaranteed. Use streamIso
for such cases instead.