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.

257 questions
88
votes
3 answers

Kotlin's Iterable and Sequence look exactly same. Why are two types required?

Both of these interfaces define only one method public operator fun iterator(): Iterator Documentation says Sequence is meant to be lazy. But isn't Iterable lazy too (unless backed by a Collection)?
Venkata Raju
  • 3,788
  • 1
  • 24
  • 30
70
votes
4 answers

Understanding a recursively defined list (fibs in terms of zipWith)

I'm learning Haskell, and came across the following code: fibs = 0 : 1 : zipWith (+) fibs (tail fibs) which I'm having a bit of trouble parsing, in terms of how it works. It's very neat, I understand that nothing more is needed, but I'd like to…
Frank
  • 4,055
  • 7
  • 37
  • 54
41
votes
4 answers

Lazy sequence generation in Rust

How can I create what other languages call a lazy sequence or a "generator" function? In Python, I can use yield as in the following example (from Python's docs) to lazily generate a sequence that is iterable in a way that does not use the memory of…
NathanD
  • 7,611
  • 7
  • 28
  • 26
39
votes
2 answers

Recursive function causing a stack overflow

I am trying to write a simple sieve function to calculate prime numbers in clojure. I've seen this question about writing an efficient sieve function, but I am not to that point yet. Right now I am just trying to write a very simple (and slow)…
dbyrne
  • 52,081
  • 13
  • 82
  • 102
29
votes
3 answers

How Are Lazy Sequences Implemented in Clojure?

I like Clojure. One thing that bothers me about the language is that I don't know how lazy sequences are implemented, or how they work. I know that lazy sequences only evaluate the items in the sequence that are asked for. How does it do this? What…
mudge
  • 6,757
  • 11
  • 43
  • 46
25
votes
6 answers

Lazy lists in Prolog?

Is it possible to have lazy lists in Prolog? Something like the following: ones([1 | Y]) :- ones(Y). Although this obviously doesn't work as it's written.
Peteris
  • 2,640
  • 3
  • 20
  • 33
25
votes
6 answers

What are some compelling use cases of infinite data structures?

Some languages (Haskell, Clojure, Scheme, etc.) have lazy evaluation. One of the "selling points" of lazy evaluation is infinite data structures. What is so great about that? What are some examples of cases where being able to deal with infinite…
Anas Elghafari
  • 912
  • 1
  • 8
  • 20
17
votes
4 answers

Lazy sequences in R

In Clojure, it's easy to create infinite sequences using the lazy sequence constructor. For example, (def N (iterate inc 0)) returns a data object N which is equivalent to the infinite sequence (0 1 2 3 ...) Evaluating the value N results in an…
Tom LaGatta
  • 545
  • 2
  • 12
17
votes
11 answers

how to do Seq.takeWhile + one item in F#

I would like to write a function which filters a sequence using a predicate but the result should also INCLUDE the first item for which the predicate returns false. The logic would be something like this, if there was a break keyword in F# let…
vidi
  • 1,722
  • 13
  • 28
17
votes
3 answers

In Clojure, are lazy seqs always chunked?

I was under the impression that the lazy seqs were always chunked. => (take 1 (map #(do (print \.) %) (range))) (................................0) As expected 32 dots are printed because the lazy seq returned by range is chunked into 32 element…
Geo G
  • 335
  • 2
  • 11
16
votes
3 answers

Do something infinitely many times with an index

In more ruby way of doing project euler #2 , part of the code is while((v = fib(i)) < 4_000_000) s+=v if v%2==0 i+=1 end Is there a way to change i += 1 into a more functional programming style construct? The best I can think of…
Andrew Grimm
  • 70,470
  • 47
  • 186
  • 310
15
votes
3 answers

How do I avoid Clojure's chunking behavior for lazy seqs that I want to short circuit?

I have a long, lazy sequence that I want to reduce and test lazily. As soon as two sequential elements are not = (or some other predicate) to each other, I want to stop consuming the list, which is expensive to produce. Yes, this sounds like…
ivar
  • 1,464
  • 12
  • 20
15
votes
1 answer

Clojure printing lazy sequence

I'm trying to print out my binary tree but Clojure is giving me a hard time printing out the sequences properly. So, I have a list of nodes '(1 2 3) for example. In each iteration I want to print out the node with a number of spaces before and after…
Christophe De Troyer
  • 2,660
  • 2
  • 26
  • 41
14
votes
1 answer

Why is Seq.init slower than a sequence expression with 'for'?

The following code showed that generating a sequence using a sequence expression containing for was approximately five times faster than generating the same sequence using Seq.init. open System let rand count = let rnd = Random() // if this…
Soldalma
  • 4,183
  • 2
  • 18
  • 34
13
votes
2 answers

How to generate a lazy division?

I want to generate the sequence of 1, 1/2, 1/3, 1/4 ... * using functional programming approach in raku, in my head it's should be look like: (1,{1/$_} ...*)[0..5] but the the output is: 1,1,1,1,1 The idea is simple, but seems enough powerful for…
metagib
  • 193
  • 5
1
2 3
17 18