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
2
votes
1 answer

lazy-seq StackOverflowError

Why does this work (def fibs (cons 0 (cons 1 (lazy-seq (map + fibs (rest fibs)))))) (take 10 fibs) while this other (def fibs (lazy-seq (cons 0 (cons 1 (map + fibs (rest fibs)))))) (take 10 fibs) generate a StackOverflowError?
Matteo
  • 539
  • 3
  • 9
2
votes
0 answers

lazy seqs in clojure to be translated to idiomatic scheme

I would like to rewrite in scheme a script I made in clojure but I'm not sure how. I wrote this simple script in clojure. It reads some csv files, processes them a bit and writes a new csv files. It's based on a bunch of functions each accepting a…
user1632812
  • 431
  • 3
  • 16
2
votes
1 answer

Clojure's lazy-seq caching

In Practical Clojure, the authors mention that once a lazy seq value is calculated, it is cached. If we get a very large number of values from a lazy-seq, might we see an out-of-memory error? Or is there a mechanism to prevent that (e.g. older…
Tianxiang Xiong
  • 3,887
  • 9
  • 44
  • 63
2
votes
2 answers

How to generate one lazy sequence from a nested for loop in Clojure

This is a question about how to properly collect the results from a nested for loop in Clojure. Suppose you want to create a sequence of all vectors [i j] where 0<=j
Adi Levin
  • 5,165
  • 1
  • 17
  • 26
2
votes
1 answer

Scala error: "forward reference extends over definition of value" when code appears in a function

I'm trying to compile the following code, using Scala 2.11.7. object LucasSeq { val fibo: Stream[Int] = 0 #:: 1 #:: fibo.zip(fibo.tail).map { pair => pair._1 + pair._2 } def firstKind(p: Int, q: Int): Stream[Int] = { val lucas:…
200_success
  • 7,286
  • 1
  • 43
  • 74
2
votes
2 answers

Writing good Scala without co-routines (including a Python example using yield)

I'm currently learning Scala and looking for an elegant solution to a problem that is easily solved by the use of co-routines. Since co-routines are not enabled by default in Scala I assume them to be at least not widely accepted best practice and…
Robert Fey
  • 1,747
  • 22
  • 23
2
votes
1 answer

Post Processing to Lazy-Sequences (Clojure)

Say I'have a lazy sequence called numbers that's giving me an infinite sequence of numbers: 0, 1, 2, 3, 4, 5, 6... (def numbers (iterate inc 0)) I limit the infinity by passing it to the function take. e.g.: (take 3 numbers) ; > (0 1 2) I'm asking…
2
votes
2 answers

Realizing a Clojure lazy sequence (string) in the REPL

I'm trying to realize a lazy sequence (which should generate a single string) in the REPL, with no luck. The original code works fine: (def word_list ["alpha" "beta" "gamma" "beta" "alpha" "alpha" "beta" "beta" "beta"]) (def word_string (reduce str…
John C
  • 6,285
  • 12
  • 45
  • 69
2
votes
2 answers

Lazy foreach on a Spark RDD

I have a big RDD of Strings (obtained through a union of several sc.textFile(...)). I now want to search for a given string in that RDD, and I want the search to stop when a "good enough" match has been found. I could retrofit foreach, or filter,…
Pradyumna
  • 1,583
  • 4
  • 19
  • 34
2
votes
2 answers

Are there sequence-operator implementations in .NET 4.0?

With that I mean similar to the Linq join, group, distinct, etc. only working on sequences of values, not collections. The difference between a sequence and a collection is that a sequence might be infinite in length, whereas a collection is…
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
2
votes
0 answers

doseq with KafkaStream evaluates previous item

I want to pop messages from a KafkaStream and process them. As KafkaStream implements java.lang.Iterable I thought I could just put the stream into a doseq and it would consume the messages as they come in. However what happens is that a message…
shmish111
  • 3,697
  • 5
  • 30
  • 52
2
votes
2 answers

Elapsed time in lazy sequence evaluation

Given this code: (reduce my-fun my-lazy-seq) To measure the elapsed time of the entire operation: (time (reduce my-fun my-lazy-seq)) ;;Elapsed time: 1000.1234 msecs How do I measure the elapsed time of this loop at various stages before…
CJLam
  • 801
  • 3
  • 10
  • 15
2
votes
1 answer

Infinite generator speed in ruby - Method vs Enumerator vs Lazy

Warning: This has little practical value. I just want to know what's going on. I've come accross this line multiple times online: return to_enum __method__ unless block_given? I wanted to test it out and made a generator method with it (#1), then I…
user21033168
  • 444
  • 1
  • 4
  • 13
2
votes
2 answers

In clojure, what is the exact behaviour of identical?

I am very surprised by the behaviour of identical? in clojure. (def a (map identity [:a :b])) (identical? (rest a) (rest a)); false Any idea why identical? returns false?
viebel
  • 19,372
  • 10
  • 49
  • 83
2
votes
2 answers

Does using `lazy-seq` recursively take up stack frames?

I'm learning about lazy seqs at the moment, and I've noticed that they usually involve recursion without using recur. For example, here is the implementation of iterate: (defn iterate "Returns a lazy sequence of x, (f x), (f (f x)) etc. f must be…
Tom Dalling
  • 23,305
  • 6
  • 62
  • 80