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

Extracting the contents of a Clojure vector as a LazySeq

I work with the Java API of Clojure and I have a persistent vector which was created through the code: IPersistentVector vec = PersistentVector.create(); and later populated with values. I need to extract the contents of this vector as a LazySeq.…
user881285
  • 55
  • 1
2
votes
1 answer

Printing a tree lazily in Newick format

I wish to print a binary tree in Newick format, showing each node's distance to its parent. At the moment I haven't had an issue with the following code, which uses regular recursion, but a tree too deep may produce a stack overflow. (defn…
Bruno Kim
  • 2,300
  • 4
  • 17
  • 27
2
votes
1 answer

How to flatten and lazily concat a list of lists

I am playing around with lazy lists and cannot seem to figure this out. I think I can solve my problem by writing this as one big recursive function that does all that is necessary, but I would like to compose it of simpler functions. I will try to…
user744959
  • 751
  • 1
  • 6
  • 13
2
votes
1 answer

efficient sieve of Euler in stream processing style

Sieve of euler has a better asymptotic complexity than Sieve of Eratosthenes, and can be implemented simply in Imperative languages. I'm wondering wether there is any way to implement it elegantly with streams .I've checked the haskell wiki about…
FooBee
  • 778
  • 7
  • 23
2
votes
2 answers

Type variance error in Scala when doing a foldLeft over Traversable views

I am trying concatenate a series of Traversable views in Scala using a foldLeft operator and am hitting type variance errors that I don't understand. I can use reduce to concatenate a list of Traversable views like so. val xs =…
W.P. McNeill
  • 16,336
  • 12
  • 75
  • 111
2
votes
3 answers

Join multiple lazy sequences of strings in Clojure

I have several strings: (def a "some random string") (def b "this is a text") Now i want to concatenate parts of them to create a string "some text". Unfortunately both of the strings below didn't work. (clojure.string/join " " [(take 4 a)…
Mirzhan Irkegulov
  • 17,660
  • 12
  • 105
  • 166
2
votes
1 answer

doseq over a simple lazy seq runs out of heap space

When stress-testing some Clojure code at work, I noticed it runs out of heap space when iterating over large data-sets. I eventually managed to trace the issues back to the combination of Clojure's doseq function, and implementation fo lazy…
2
votes
3 answers

What's wrong with this clojure prime seq?

I can't figure out why this definition of a lazy primes sequence would cause non-termination. The stack-trace I get isn't very helpful (my one complaint about clojure is obtuse stack-traces). (declare naturals is-prime? primes) (defn naturals …
redcartel
  • 292
  • 1
  • 3
  • 15
1
vote
1 answer

clojure storing vs. using a sequence in expression

Helo, In an effort to learn clojure, I have taken an interest in clojure.core functions that act on sequences. Recently, I noticed some odd behaviour and would like an explaination of the difference between the folling expressions: What I'm trying…
wespiserA
  • 3,131
  • 5
  • 28
  • 36
1
vote
1 answer

What's the idiomatic way to make replayable sequences in F#?

I just started using this year's Advent of Code to learn F# and I immediately stepped on a rake by trying to reuse the IEnumerable from File.ReadLines. Here are all of the ways I see to solve this: // Read all lines immediately into array/list let…
Kenneth Allen
  • 347
  • 3
  • 7
1
vote
1 answer

Count runs lazily in Python translate from Haskell

I'm trying to write a generator function (or achieve the equivalent) which takes an iterable xs in Python and counts the "runs". (This is a problem in Thinking Functionally with Haskell by Bird, which I want to translate into Python using Python's…
Eric Auld
  • 1,156
  • 2
  • 14
  • 23
1
vote
1 answer

SICP 3.52 delayed cdr

Exercise 3.52, (define sum 0) (define (accum x) (set! sum (+ x sum)) sum) ;1: (define seq (stream-map accum (stream-enumerate-interval 1 20))) ;2: (define y (stream-filter even? seq)) ;3: (define z (stream-filter (lambda (x) (= (remainder x 5)…
1
vote
2 answers

Delayed "let" in SICP

SICP indicates cdr is opened up: In section 3.5.4 , i saw this block: (define (integral delayed-integrand initial-value dt) (define int (cons-stream initial-value (let ((integrand (force delayed-integrand))) …
1
vote
1 answer

Using the >>= and =<< operators to combine IO in Haskell

I am trying to run an "infinite" simulation, printing the results of each step. There's a function nextFrameR which takes an input Map and steps forward the simulation to return an output Map, and then there's a render_ function which takes an input…
GTF
  • 8,031
  • 5
  • 36
  • 59
1
vote
2 answers

Filtering and reducing lazy structures

I would like to filter a lazy structure and then reduce it using Swift language. func main() -> () { let result = (1...) .lazy .filter { $0 < 3 } .reduce(0, {$0 + $1}) return print( result …
F. Zer
  • 1,081
  • 7
  • 9