Questions tagged [lazy-sequences]

Lazy sequences are sequences that are constructed as their members are accessed.

Lazy sequences are sequences that are constructed as their members are accessed.

For example, all haskell sequences are lazy because of the lazy semantics of the language; python's generators can also be considered lazy sequences.

273 questions
13
votes
2 answers

How to create a lazy-seq generating, anonymous recursive function in Clojure?

Edit: I discovered a partial answer to my own question in the process of writing this, but I think it can easily be improved upon so I will post it anyway. Maybe there's a better solution out there? I am looking for an easy way to define recursive…
ivar
  • 1,484
  • 12
  • 20
13
votes
2 answers

Clojure: Idiomatic way to call contains? on a lazy sequence

Is there an idiomatic way of determining if a LazySeq contains an element? As of Clojure 1.5 calling contains? throws an IllegalArgumentException: IllegalArgumentException contains? not supported on type: clojure.lang.LazySeq …
nansen
  • 2,912
  • 1
  • 20
  • 33
12
votes
3 answers

What is the difference between the Clojure function (nth [coll index]) and the composition (last (take index coll))

I'm trying to work through Stuart Halloway's book Programming Clojure. This whole functional stuff is very new to me. I understand how (defn fibo[] (map first (iterate (fn [[a b]] [b (+ a b)]) [0 1]))) generates the Fibonacci sequence…
Josh
  • 2,077
  • 1
  • 17
  • 21
12
votes
1 answer

Clojure Lazy Sequences that are Vectors

I have noticed that lazy sequences in Clojure seem to be represented internally as linked lists (Or at least they are being treated as a sequence with only sequential access to elements). Even after being cached into memory, access time over the…
ivar
  • 1,484
  • 12
  • 20
12
votes
1 answer

double stream feed to prevent unneeded memoization?

I'm new to Haskell and I'm trying to implement Euler's Sieve in stream processing style. When I checked the Haskell Wiki page about prime numbers, I found some mysterious optimization technique for streams. In 3.8 Linear merging of that…
11
votes
2 answers

Getting the last element of a lazy Seq in Raku

I want to get the last element of a lazy but finite Seq in Raku, e.g.: my $s = lazy gather for ^10 { take $_ }; The following don't work: say $s[* - 1]; say $s.tail; These ones work but don't seem too idiomatic: say (for $s<> { $_ }).tail; say…
Nikola Benes
  • 2,372
  • 1
  • 20
  • 33
11
votes
2 answers

mapcat breaking the lazyness

I have a function that produces lazy-sequences called a-function. If I run the code: (map a-function a-sequence-of-values) it returns a lazy sequence as expected. But when I run the code: (mapcat a-function a-sequence-of-values) it breaks the…
Carlos Nunes
  • 1,929
  • 2
  • 16
  • 20
11
votes
1 answer

Clojure: lazy magic

Almost 2 identical programs to generate infinite lazy seqs of randoms. The first doesn't crash. The second crash with OutOfMemoryError exception. Why? ;Return infinite lazy sequence of random numbers (defn inf-rand[] (lazy-seq (cons (rand)…
GabiMe
  • 18,105
  • 28
  • 76
  • 113
11
votes
2 answers

lazy version of mapM

Suppose, I'm getting large list of items while working with IO: as <- getLargeList Now, I'm trying to apply fn :: a -> IO b onto as: as <- getLargeList bs <- mapM fn as mapM has type mapM :: Monad m => (a -> m b) -> [a] -> m [b], and that's what I…
Dmitry Bespalov
  • 5,179
  • 3
  • 26
  • 33
10
votes
1 answer

How does lazyness of the slice index affects the slicing of an array/list? [RAKU]

When we slice an array with an index that exceeds the boundaries of the array we get as the result the undefined (Any) When we pass the same slice index as a lazy list then we get as result the existing values of the array/list (and NOT any more…
jakar
  • 1,701
  • 5
  • 14
9
votes
3 answers

how to cycle through list indefinitely and lazily in Raku?

This is mostly to geek out on how awesome Raku is. Question Are there built-in methods that will take a list and cycle through it indefinitely, producing, say, the lazy list a, b, c, a, b, c, ... out of (a, b, c)? Nothing in the documentation on…
grobber
  • 1,083
  • 1
  • 9
  • 20
9
votes
3 answers

Lazy partition-by

I have a source of items and want to separately process runs of them having the same value of a key function. In Python this would look like for key_val, part in itertools.groupby(src, key_fn): process(key_val, part) This solution is completely…
tempestadept
  • 835
  • 4
  • 15
9
votes
2 answers

how to unit test for laziness

I have a function that is supposed to take a lazy seq and return an unrealized lazy seq. Now I want to write a unit test (in test-is btw) to make sure that the result is an unrealized lazy sequence.
Arthur Ulfeldt
  • 90,827
  • 27
  • 201
  • 284
8
votes
4 answers

How to cycle a list infinitely and lazily in Kotlin?

I have a list of directions and want to find the next direction when I take a right or left turn. Here is the working code I have: enum class Turn { R, L } enum class Direction { N, E, S, W } val directionsInRightTurnOrder = listOf(Direction.N,…
or9ob
  • 2,313
  • 4
  • 25
  • 45
8
votes
3 answers

How can I make a ruby enumerator that does lazy iteration through two other enumerators?

Let's say I have two enumerators, enum1 and enum2 that must be lazily iterated through (because they have side effects). How do I construct a third enumerator enum3 where enum3.each{|x| x} would lazily return the equivalent of enum1 + enum2? In my…
Alex Altair
  • 3,246
  • 3
  • 21
  • 37
1
2
3
18 19