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
6
votes
3 answers

Clojure head retention in doseq, run! loops

Clojure beginner/intermediate here, I have a large XML file (~ 240M), which I need to process lazily item by item for ETL purposes. There is some run-processing function, which does a lot of stuff with side-effects, db interactions, writing to…
Twice_Twice
  • 527
  • 4
  • 16
6
votes
1 answer

Does standard C++11 guarantee that temporary object passed to a function will have been destroyed after the end of the function?

As known, that standard C++11 guarantees that temporary object passed to a function will have been created before function call: Does standard C++11 guarantee that temporary object passed to a function will have been created before function…
Alex
  • 12,578
  • 15
  • 99
  • 195
6
votes
4 answers

Iterator blocks in Clojure?

I am using clojure.contrib.sql to fetch some records from an SQLite database. (defn read-all-foo [] (with-connection *db* (with-query-results res ["select * from foo"] (into [] res)))) Now, I don't really want to realize the whole…
Alex B
  • 82,554
  • 44
  • 203
  • 280
6
votes
1 answer

Idiomatic way to get first element of a lazy seq in clojure

When processing each element in a seq I normally use first and rest. However these will cause a lazy-seq to lose its "laziness" by calling seq on the argument. My solution has been to use (first (take 1 coll)) and (drop 1 coll) in their place when…
robertjlooby
  • 7,160
  • 2
  • 33
  • 45
6
votes
3 answers

Streams in Scheme - define integers through stream map in scheme

How can I define integers through stream-map in Scheme: (define integers (stream-cons 1 (stream-map *something* *something*))
nanachan
  • 1,051
  • 1
  • 15
  • 26
6
votes
1 answer

Understanding the execution of a lazy fibonacci implementation in Clojure

I'm trying to understand the execution of the following code: (def fibs (concat (lazy-seq [0 1]) (lazy-seq (map + fibs (rest fibs))))) This is what I would expect the execution to look like [0 1 : (map + [0 1] [1]) => 1 [0 1 1 : (map + [0 1 1]…
ebaxt
  • 8,287
  • 1
  • 34
  • 36
6
votes
1 answer

Lazyness and stackoverflow

I wrote the following: (fn r [f xs] (lazy-seq (if (empty? xs) '() (cons (f (first xs)) (r f (rest xs)))))) to solve 4clojure.com's problem #118: http://www.4clojure.com/problem/118 which asks to reimplement map without using map etc.…
Cedric Martin
  • 5,945
  • 4
  • 34
  • 66
5
votes
2 answers

Is this possible to create an operator that evaluates argument left-to-right in OCaml

When you define an operator such as let (++) a b = a :: b When you do let v = foo a ++ bar b bar is evaluated before foo. A workaround is to use let expression, i.e. let e1 = foo a in let e2 = bar b in e1 ++ e2 However, sometimes it'd be handy…
Butanium
  • 726
  • 5
  • 19
5
votes
1 answer

Does Haskell discards intermediary results during lazy evaluation?

If I define the Fibonacci sequence recursively: fibo_lazy_list = 0 : 1 : zipWith (+) fibo_lazy_list (tail fibo_lazy_list) Then ask for the first element above a given value, say: print $ find (>100) fibo_lazy_list I understand that Haskell…
vkubicki
  • 1,104
  • 1
  • 11
  • 26
5
votes
3 answers

Space complexity of streams in Scheme

I am reading Structure and Interpretation of Computer Programs (SICP) and would like to make sure that my thinking is correct. Consider the following simple stream using the recursive definition: (define (integers-starting-from n) (cons-stream n…
5
votes
1 answer

Equivalent of Scala View in JavaScript

In Scala, view allows preventing creating an entirely new collection. e.g. In Scala, what does "view" do? Is there something similar in JavaScript? My use case: x = inputValue.split(",").map(x => x.trim()).filter(f1).map(f2) As you can see in the…
5
votes
1 answer

compactMap on sequence() not lazy?

Every once in a while I have to walk up the responder chain to reach an instance of a known class. (Just accept this for purposes of the question.) I've been doing this with a while loop, but it occurred to me that it would be cooler to use…
matt
  • 515,959
  • 87
  • 875
  • 1,141
5
votes
3 answers

Creating a compound iterator in F#

I'm implementing a checkers-like game, and I need a sequence that enumerates all legal moves for a given configuration. I've got the following function, directly translated from C#: seq { for y1 = 0 to BOARDSIZE-1 do for x1 = 0 to…
user627943
  • 125
  • 4
5
votes
1 answer

Clojure Lazy Sequences: Equivalents in Kotlin

Clojure provides means for lazy evaluation of values in (infinite) sequences. With this, values will only be computed when they get actually consumed. An example of an infinite sequence of one repeated element: (take 3 (repeat "Hello…
s1m0nw1
  • 76,759
  • 17
  • 167
  • 196
5
votes
1 answer

Ruby's `lazy` less performant than allocating huge lists?

I'm trying to process a large list of numbers: require 'benchmark' N = 999999 Benchmark.bm 10 do |bm| bm.report 'Eager:' do (0..N).select(&:even?).map{|x| x * x}.reduce(&:+) end bm.report 'Lazy:' do (0..N).lazy.select(&:even?).map{|x|…
Idan Arye
  • 12,402
  • 5
  • 49
  • 68