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
5
votes
2 answers

How to forget head(GC'd) for lazy-sequences in Clojure?

Let's say I have a huge lazy seq and I want to iterate it so I can process on the data that I get during the iteration. The thing is I want to lose head(GC'd) of lazy seq(that processed) so I can work on seqs that have millions of data without…
Ertuğrul Çetin
  • 5,131
  • 5
  • 37
  • 76
5
votes
1 answer

Can someone explain this lazy Fibonacci solution?

This is the code: fibs = 0 : 1 : zipWith (+) fibs (drop 1 fibs) When evaluated, fibs is an infinite list of Fibonacci numbers. What I don't understand is how the list is concatenated. zipWith returns a list, so zipping fibs would yield this: 0 :…
dopatraman
  • 13,416
  • 29
  • 90
  • 154
5
votes
2 answers

(println (iterate inc 0)): why does this even start printing?

When I run (println (iterate inc 0)) in my repl, I will get something like this: user=> (println (iterate inc 0)) (0 1 2 3 4 5 6 7 8 9 10 11 12 13 .................... My expectation when I run the code is that repl shows nothing and just stuck…
kimh
  • 719
  • 2
  • 9
  • 15
5
votes
1 answer

Stack overflows and recursive sequence expressions F#

I have a sequence expression like so: let fibSeq = let rec fibSeq' a b = seq { yield a yield! fibSeq' b (a + b) } fibSeq' 1 1 Now even for large numbers, this will not generate a stack overflow. I'm wondering why, it…
Overly Excessive
  • 2,095
  • 16
  • 31
5
votes
2 answers

Terminology for example of codata in Clojure

Imagine the following function to give an infinite lazy sequence of fibonacci in Clojure: (def fib-seq (concat [0 1] ((fn rfib [a b] (lazy-cons (+ a b) (rfib b (+ a b)))) 0 1))) user> (take 20 fib-seq) (0 1 1 2 3 5 8 13 21 34 55 89…
hawkeye
  • 34,745
  • 30
  • 150
  • 304
5
votes
1 answer

Clojure: Creating a lazy sequence of byte chunks from an input stream

Given an input stream I would like to create a lazy sequence of the data in the form of byte-arrays (chunks). Here's my try: (defn- read-or-nil [stream] (let [buf (byte-array 2)] (when-not (= -1 (.read stream buf)) buf))) (defn…
Mediocre Gopher
  • 2,274
  • 1
  • 22
  • 39
5
votes
3 answers

Realization timing of lazy sequence

(defn square [x] (do (println (str "Processing: " x)) (* x x))) (println (map square '(1 2 3 4 5))) Why is the output (Processing: 1 Processing: 2 1 Processing: 3 4 Processing: 4 9 Processing: 5 16 25) not (Processing: 1 1…
alice
  • 2,547
  • 4
  • 24
  • 30
5
votes
2 answers

Can I read n files lazily as a single IO operation in Haskell?

How can I read multiple files as a single ByteString lazily with constant memory? readFiles :: [FilePath] -> IO ByteString I currently have the following implementation but from what I have seen from profiling as well as my understanding I will…
Ben Doerr
  • 1,655
  • 1
  • 13
  • 23
5
votes
1 answer

How can I create a lazy-seq vector

Running this works as expected: (defn long-seq [n] (lazy-seq (cons (list n {:somekey (* n 2)}) (long-seq (+ n 1))))) (take 3 (long-seq 3)) ; => ((3 {:somekey 6}) (4 {:somekey 8}) (5 {:somekey 10})) However I would like…
snowape
  • 1,274
  • 10
  • 23
5
votes
3 answers

Clever streams-based python program doesn't run into infinite recursion

I was playing around with clever ways to create a python generator for sequence A003602 This appears to work, but I can't figure out why. It seems to me like it should hit infinite recursion. Is python doing some lazy evaluation somewhere that I…
dspyz
  • 5,280
  • 2
  • 25
  • 63
4
votes
2 answers

See if part of data is lazy in clojure

Is there a function in clojure that checks whether data contains some lazy part? Background: I'm building a small server in clojure. Each connection has a state, an input-stream and an output-stream The server reads a byte from an input-stream, and…
Claude
  • 8,806
  • 4
  • 41
  • 56
4
votes
4 answers

Implementing an unusual sequence as an infinite list in Haskell

I have two elements as the beginning of the list [1, 2] This unusual sequence is that it replicates the digits in the number of digits of a certain type that follows the three elements. For example, after 1 and 2, we would have another 2, followed…
4
votes
2 answers

Assoc or update Clojure lists and lazy sequences

If I have a vector (def v [1 2 3]), I can replace the first element with (assoc v 0 666), obtaining [666 2 3] But if I try to do the same after mapping over the vector: (def v (map inc [1 2 3])) (assoc v 0 666) the following exception is…
mrucci
  • 4,342
  • 3
  • 33
  • 35
4
votes
1 answer

How to produce a lazy sequence by portion in clojure?

I have a database server and I fetch data from it. Sometimes data have millions rows and more so I use laziness for downloading. I use Server Side Cursors from clojure.jdbc library https://funcool.github.io/clojure.jdbc/latest/#cursor-queries to…
Verbery
  • 157
  • 7
4
votes
2 answers

Clojure/Java: Most effective method for minimizing bandwidth consumption when performing complex operations on a stream of Amazon S3 data

I'm performing streaming reads of an object using BufferedReader. I need to do two things with this object: Pass it to a SuperCSV csv reader Obtain the raw lines and keep them in a (Clojure) lazy sequence Currently, I am having to use two…
jkndrkn
  • 4,012
  • 4
  • 36
  • 41