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

Clojure lazy seq consumption from different positions in the body of a function

Clojure beginner here, not sure if the terminology in the question is even correct. I'm building a web scraper using the clj-webdriver taxi api. There are several sites that it needs to scrape data from. The following isn't actually code from the…
Y T
  • 63
  • 5
4
votes
1 answer

Does Clojure have "unfold"?

(defn unfold [step seed] (if-let [[val new-seed] (step seed)] (cons val (lazy-seq (unfold step new-seed))) nil)) Example usage: (defn fib-step [[x y]] [x [y (+ x y)]]) (take 10 (unfold fib-step [0 1])) ;=> (0 1 1 2 3 5 8 13 21 34) (defn…
Dan Burton
  • 53,238
  • 27
  • 117
  • 198
4
votes
2 answers

How do I write a predicate that checks if a value exists in an infinite seq?

I had an idea for a higher-order function today that I'm not sure how to write. I have several sparse, lazy infinite sequences, and I want to create an abstraction that lets me check to see if a given number is in any of these lazy sequences. To…
ivar
  • 1,484
  • 12
  • 20
4
votes
2 answers

idiomatic lazy atoms in clojure

I am playing a bit with atoms in clojure. I have an atom pointing at a lazy-seq. In another bit of code I want to update the value of the atom to the result of doing next on the sequence, but given that both swap! and reset! return the updated value…
emanjavacas
  • 109
  • 2
  • 7
4
votes
2 answers

clojure lazy-seq performance optimization

I'm fairly new to Clojure and I have some code that I am trying to optimise. I want to compute concurrance-counts. The main function is compute-space and the output is a nested map of the type {"w1" {"w11" 10, "w12" 31, ...} "w2" {"w21" 14, "w22"…
emanjavacas
  • 109
  • 2
  • 7
4
votes
1 answer

Flattening lazy list of lazy lists in Scheme via higher-order accumulation procedure

I'm trying to find an implementation which flattens a lazy list of lazy lists using interleave and lz-lst-accumulate which are procedures that I wrote. This is the code so far: (define lz-lst-accumulate (lambda (op initial lz) (if (empty? lz) …
kitsuneFox
  • 1,243
  • 3
  • 18
  • 31
4
votes
3 answers

Does Enumerable.ToDictionary only retrieve what it needs?

I'm using Enumerable.ToDictionary to create a Dictionary off of a linq call: return (from term in dataContext.Terms where term.Name.StartsWith(text) select term).ToDictionary(t => t.TermID, t => t.Name); Will that call fetch the…
Mike Pateras
  • 14,715
  • 30
  • 97
  • 137
4
votes
2 answers

Find the elements of a LazySeq that have been realized

I have a LazySeq of connections that are created when realized. If an exception occurs while attempting to create a connection, I'd like to iterate through all of the connections that have already been realized in the LazySeq and close them.…
Josh
  • 875
  • 1
  • 13
  • 25
4
votes
1 answer

confused in a macro definition

I want to implement the lazy stream in SICP section 3.5.1 Firstly, I defined this two functions (defmacro delay (form) `(lambda () ,form)) (defun force (form) (when form (funcall form))) When we called: (force (delay '(+ 1 2))) ;;=> (+ 1…
xiepan
  • 623
  • 4
  • 13
4
votes
1 answer

How to use LazySeq correctly

There are a dozen of confusion when I use lazySeq. Question: (def fib (lazy-seq (concat [0 1] (map + fib (rest fib))))) ;; It's Ok (take 10 fib) ;; Bomb Got the error message: StackOverflowError clojure.lang.RT.more (RT.java:589) And the…
Liao Pengyu
  • 591
  • 3
  • 12
4
votes
2 answers

Create lazy IO list from a non-IO list

I have a lazy list of filenames created by find. I'd like to be able to load the metadata of these files lazily too. That means, that if i take 10 elements from metadata, it should only search the metadata of these ten files. The fact is find…
4
votes
2 answers

stop and split generated sequence at repeats - clojure

I am trying to make a sequence that will only generate values until it finds the following conditions and return the listed results: case head = 0 - return {:origin [all generated except 0] :pattern 0} 1 - return {:origin nil :pattern…
user945754
4
votes
4 answers

When are the different elements of a lazy sequence realized in clojure?

I'm trying to understand when clojure's lazy sequences are lazy, and when the work happens, and how I can influence those things. user=> (def lz-seq (map #(do (println "fn call!") (identity %)) (range 4))) #'user/lz-seq user=> (let [[a b]…
collapsinghrung
  • 855
  • 1
  • 6
  • 7
3
votes
4 answers

How can I create a type in order to accommodate the return value of my Ocaml function?

I am trying to implement a lazy fibonacci generator in Ocaml as shown below: (* fib's helper *) let rec fibhlpr n = if n == 0 then 0 else if n == 1 then 1 else fibhlpr (n-1) + fibhlpr (n-2);; (* lazy fib? *) let rec fib n = ((fibhlpr n), fun() ->…
3
votes
2 answers

Creating a generator for primes in the style of SICP

In a future course, I'll be having a discipline that uses Python with an emphasis of using sequences and generators and that kind of stuff inn Python. I've been following an exercise list to exercise these parts. I'm stuck on an exercise that asks…
tigre200
  • 184
  • 1
  • 10