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

Clojure clojure.lang.LazySeq errors

I seem to have nasty problem with clojure I/O (or typesystem). The point is that this function, which I expect to consume collection of collections of strings and numbers or strings, and returns dictionary of strings associated with numbers,…
Jakub Bartczuk
  • 2,317
  • 1
  • 20
  • 27
3
votes
1 answer

Lazy evaluation / Stream / FRP code for node.js fs.readdir Async recursive directory search

I try to implement a list structure reading FileSystem Directory tree using node.js DIR/file structure: DIR1 DIR2R file1 file2 file3 … -> list structure: ("DIR1" ("DIR2" "file1" "file2" "file3" …)) DIR() //looks…
3
votes
1 answer

Can I process an unrealized lazy-seq step by step

I have a lazy-seq where each item takes some time to calculate: (defn gen-lazy-seq [size] (for [i (range size)] (do (Thread/sleep 1000) (rand-int 10)))) Is it possible to evaluate this sequence step by step and print the results.…
snowape
  • 1,274
  • 10
  • 23
3
votes
3 answers

Alternate two values

I have the code (define alternate (letrec ([f (lambda (x) (cons x (lambda () (f (+ x 1)))))]) (lambda () (f 1)))) The result is 1,2,3.. How i could change it to take 1,2,1,2,1,2.. I tried cons inside the f but didn't work. Any ideas?
Rea
  • 67
  • 5
3
votes
2 answers

Non-linear slowdown creating a lazy seq in Clojure

I implemented a function that returns the n-grams of a given input collection as a lazy seq. (defn gen-ngrams [n coll] (if (>= (count coll) n) (lazy-seq (cons (take n coll) (gen-ngrams n (rest coll)))))) When I call this function with…
jgre
  • 787
  • 5
  • 11
3
votes
1 answer

adding metadata to a lazy sequence

When I try to add metadata to an infinite lazy sequence in Clojure, I get a stack overflow, and if I take off the metadata, then it works just fine. Why does adding the with-meta macro break the lazy seq? First create an infinite seq of a very nice…
Arthur Ulfeldt
  • 90,827
  • 27
  • 201
  • 284
2
votes
1 answer

How to prevent Clojure exception: clojure.lang.LazySeq cannot be cast to clojure.lang.IFn

I am trying to pass the (lazy) sequence returned from a map operation to another map operation, so that I can look up elements in the first sequence. The code is parsing some football fixtures from a text file (in row/column format), cleaning it up,…
Andrew Whitehouse
  • 2,738
  • 3
  • 31
  • 35
2
votes
1 answer

Does this Haskell code reuse previous computation?

I have a piece of Haskell code that computes regular numbers, i.e. positive integers whose only prime factors can be 2, 3 or 5. The algorithm is straightforward and follows what's suggested in the same Wikipedia article. regularSeq ::…
2
votes
3 answers

What would be the simplest analogue in OCaml, of Python's enumerate function?

In Python enumerate works as follows: a_list = ['a', 'b', 'c'] for i, x in enumerate(a_list): print(i, x) The output will be: 0 a 1 b 2 c Thus, enumerate actually returns a generator (pretty much a lazy sequence) of pairs of the form (i,…
Mateo
  • 1,494
  • 1
  • 18
  • 27
2
votes
1 answer

How to define a infinite Lazy List using a given function or property in Scala?

I recently was seeing an advanced Scala course in Rock the JVM and, in one lesson, Daniel purposed to create a set using propertys (functions going from A to Boolean), the implementation of this Set can be found here. For example, he was able to…
2
votes
1 answer

Newbie Problem Understanding Clojure Lazy Sequences

I've just started learning Clojure and I'm puzzled by how lazy sequences work. In particular, I don't understand why these 2 expressions produce different results in the repl: ;; infinite range works OK (user=> (take 3 (map #(/(- % 5))…
BillyBadBoy
  • 552
  • 3
  • 18
2
votes
2 answers

Trying to split string in Clojure running into lazy seq problem

I am working on a problem to read in a file with lines like: A abcdefg B bcdefgh But I keep getting errors about Lazy Sequence not compatible with Java Charseq .. I tried: (def notlazy (doall lyne2)) Then thought I verified: (realized?…
Mark McWiggins
  • 621
  • 6
  • 20
2
votes
1 answer

Trouble with streams in Scheme

I'm trying to write a stream that takes as arguments an infinite stream S, and two integers m and n, and returns the stream whose elements are elements of S that are multiples of either m or n. Unfortunately, my stream only works until I find the…
Ben
  • 141
  • 2
  • 12
2
votes
1 answer

This prime generating function using generateSequence in Kotlin is not easy to understand. :(

val primes = generateSequence(2 to generateSequence(3) {it + 2}) { val currSeq = it.second.iterator() val nextPrime = currSeq.next() nextPrime to currSeq.asSequence().filter { it % nextPrime != 0} }.map…
Zynastor
  • 71
  • 8
2
votes
1 answer

Generate strings in lexicographical order in Python

How can I write a Python generator that lazily generates all strings composed of lowercase English letters of up to a certain length1? I have written my own solution (posted below as an answer), but I would like to see if there are any more…
Anakhand
  • 2,838
  • 1
  • 22
  • 50