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
1
vote
4 answers

The usage of lazy-sequences in clojure

I am wondering that lazy-seq returns a finite list or infinite list. There is an example, (defn integers [n] (cons n (lazy-seq (integers (inc n))))) when I run like (first integers 10) or (take 5 (integers 10)) the results are 10 and (10 11…
Xiufen Xu
  • 531
  • 1
  • 3
  • 19
1
vote
1 answer

What is the best way of reading a large JSON file in Erlang?

There is a large (does not fit in memory) .json file with the following content: [{ "doc_number": "xxx", "other": "data" }, { "doc_number": "yyy", "other": "data" }, { "doc_number": "zzz", "other": "data" }] I would like to…
Istvan
  • 7,500
  • 9
  • 59
  • 109
1
vote
2 answers

Clojure lazily read random line from file

I have a sample data set in a txt file. The data file is extremely large so loading it in memory is not an option. I need to be able to read the file lazily. Furthermore, I need the lines to be read in a random order. And there might be cases where…
Lordking
  • 1,413
  • 1
  • 13
  • 31
1
vote
1 answer

Generic LazyCollection type

I need a function that returns a lazy generator of various composed generator functions such as filter and map. For example, if I want to apply lazy.filter().map() the code looks like: // Simplified typealias MyComplexType = Int typealias…
Stiivi
  • 2,077
  • 2
  • 16
  • 27
1
vote
2 answers

Usage of Lambda.filter() in haxe typed iterables or equivalent functional construct

How can I make the haxe Lambda.filter work with a typed iterable? The next code refuses to compile because of a bad type: Iterator should be Iterable> { next : Void -> Int, hasNext : Void -> Bool } should be Iterable> Code…
1
vote
1 answer

sort-by on a lazy sequence of hash-maps in clojure

I need to take 20 results from a lazy sequence of millions of hash-maps but for the 20 to be based on sorting on various values within the hashmaps. For example: (def population [{:id 85187153851 :name "anna" :created #inst…
adamneilson
  • 758
  • 7
  • 9
1
vote
4 answers

scala Stream transformation and evaluation model

Consider a following list transformation: List(1,2,3,4) map (_ + 10) filter (_ % 2 == 0) map (_ * 3) It is evaluated in the following way: List(1, 2, 3, 4) map (_ + 10) filter (_ % 2 == 0) map (_ * 3) List(11, 12, 13, 14) filter (_ % 2 == 0) map (_…
Vadim Samokhin
  • 3,378
  • 4
  • 40
  • 68
1
vote
1 answer

scala Stream.takeWhile

I am implementing takeWhile method of trait Stream via foldRight. My foldRight is following: trait Stream[+A] { def foldRight[B](z: => B)(f: (A, => B) => B): B = uncons.map(t => { f(t._1, t._2.foldRight(z)(f)) }).getOrElse(z) } My…
Vadim Samokhin
  • 3,378
  • 4
  • 40
  • 68
1
vote
1 answer

Scala Stream function evaluation

I got a following code: trait Stream[+A] { def uncons: Option[(A, Stream[A])] def foldRight[B](z: => B)(f: (A, => B) => B): B = { uncons.map(t => { f(t._1, t._2.foldRight(z)(f)) }).getOrElse(z) } def exists(p: A => Boolean) = …
Vadim Samokhin
  • 3,378
  • 4
  • 40
  • 68
1
vote
2 answers

lazy-seq and stack overflow for infinite sequences

I am trying to show the importance of lazy-sequences or lazy-evaluation to the non-FP programmers. I have written this implementation of prime-generation to show the concept: (defn primes-gen [sieve] (if-not (empty? sieve) (let [prime (first…
Ashish Negi
  • 5,193
  • 8
  • 51
  • 95
1
vote
1 answer

Clojure: What is the underlying data structure of a lazy sequence?

As stated above, what is the underlying data structure of a lazy sequence ? Is it a list ? If it is, then what kind of list is it ? Where can I find references about this ?
mynameisJEFF
  • 4,073
  • 9
  • 50
  • 96
1
vote
2 answers

How to generate a lazy sequence in clojure using non-tail recursion?

Take integer partition problem for example, I can write the following code to output all partitions of a positive integer n: (defn foo ([n] (foo n n [])) ([n k buf] (if (zero? n) (println buf) (doseq [i (range 1 (inc (min n…
SaltyEgg
  • 1,498
  • 1
  • 15
  • 26
1
vote
2 answers

How to create a lazy-seq that repeats elements of another collection, in random order?

I would like to create a lazy sequence that repeats elements from another collection. It should generate one of each element before repeating. And the order of elements must be random. Here's what it should behave like: => (take 10 x) (B A C B A C A…
muhuk
  • 15,777
  • 9
  • 59
  • 98
1
vote
2 answers

Clojure: iterating on multiple (possibly infinite) sequences

I've got a couple of infinite sequences. I want to take one of each other per step. What's the idiomatic way of doing that? In other words, assume that there's a finite, realized sequence iss that contains lazy, infinite sequences. How to print out…
Mate Varga
  • 3,144
  • 2
  • 14
  • 17
1
vote
1 answer

(list) in lazy-seq causes infinite recursion but (cons) does not

In attempting to understand lazy-seq, I came up with this example: (defn zeroes [] (list 0 (lazy-seq (zeroes)))) (take 5 (zeroes)) ; too much recursion error This however triggers a too much recursion error. Replacing (list) with (cons) fixes…
Jegschemesch
  • 11,414
  • 4
  • 32
  • 37