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
0 answers

How is range a lazy sequence but not a generator in Python?

I had heard that range was a generator, then later I realized that it is not via this helpful post. While this post thoroughly explains how the __contains__ method works in constant time, I was not able to find any source code reference on how the…
mysl
  • 1,003
  • 2
  • 9
  • 19
3
votes
1 answer

What's lazy about views on Scala's mutable collections?

I think this question is essentially about what laziness is in the context of mutability. In Programming in Scala (or the docs), they give examples of how to use views on immutable and mutable collections. In that section, they state [transformer…
joel
  • 6,359
  • 2
  • 30
  • 55
3
votes
2 answers

What scope should calls to lazy-seq have?

I'm writing a lazy implementation of the Recamán's Sequence, and ran into some confusion regarding where calls to lazy-seq should happen. This first version I came up with this morning was: (defn lazy-recamans-sequence [] (let [f (fn rec [n seen…
Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
3
votes
1 answer

Sequence of incorrect length generated by function

Why is the following function returning a sequence of incorrect length when the repl variable is set to false? open MathNet.Numerics.Distributions open MathNet.Numerics.LinearAlgebra let sample (data : seq) (size : int) (repl : bool) = …
3
votes
2 answers

Create Lazy Sequency by Concatenating Collections

Create a lazy sequence by concatenating collections. Consider the following function: (defn req [] (Thread/sleep 1000) (repeat 4 (rand-int 10))) The sleep is added since the function will finally be a http request, thus it should emulate a…
Anton Harald
  • 5,772
  • 4
  • 27
  • 61
3
votes
2 answers

Proving equality on coinductive lazy lists in Coq

I am experimenting with Coq Coinductive types. I use the lazy list type form the Coq'Art book (sect. 13.1.4): Set Implicit Arguments. CoInductive LList (A:Set) : Set := | LNil : LList A | LCons : A -> LList A -> LList A. Implicit Arguments LNil…
Cryptostasis
  • 1,166
  • 6
  • 15
3
votes
2 answers

Does standard C++11 guarantee that temporary object passed to a function will have been created before function call?

Does standard C++11 guarantee that all 3 temporary objects have been created before the beginning performe the function? Even if temporary object passed as: object rvalue-reference passed only member of temporary…
Alex
  • 12,578
  • 15
  • 99
  • 195
3
votes
2 answers

How to access LazySeq values

I am playing with wit/duckling library. It has been written in clojure and I have no previous experience in clojure. By following its documentation I got value of a variable as ({:dim :time, :body "20 minutes from now", :value {:type "value",…
Saurabh Jain
  • 1,227
  • 1
  • 16
  • 28
3
votes
2 answers

Why does reducing this lazy sequence slow down this Clojure program 20x?

I have a Clojure program that returns the sum of a lazy sequence of even Fibonacci numbers below n: (defn sum-of-even-fibonaccis-below-1 [n] (defn fib [a b] (lazy-seq (cons a (fib b (+ b a))))) (reduce + (take-while (partial >= n) (take-nth 3…
alt
  • 13,357
  • 19
  • 80
  • 120
3
votes
1 answer

Kotlin sequence function unresolved reference

Kotlin 1.0.0 IDEA 2016.1 I have found a couple of references to the new sequence function used to create a sequence (no longer called stream). The JetBrains blog gives the following examples: val elements = sequence(1, { x -> x + 1}) val elements…
melston
  • 2,198
  • 22
  • 39
3
votes
4 answers

Yielding until all needed values are yielded, is there way to make slice to become lazy

Is there way to stop yielding when generator did not finish values and all needed results have been read? I mean that generator is giving values without ever doing StopIteration. For example, this never stops: (REVISED) from random import…
3
votes
2 answers

Generating an infinite set of numbers

I'm trying to make a prime number generator which should be able to return a sequence of prime numbers up to the nth number. Now I figured there should be a more elegant way to do this with sequences besides my current solution which feels a bit…
Overly Excessive
  • 2,095
  • 16
  • 31
3
votes
1 answer

Filter a Lazy Sequence in Parallel

I have a problem in which I am searching for numbers with certain properties in a very large search space (possibly infinite, but definitely too large for the whole space to fit in memory). So I want a lazy sequence, which I filter. My naive…
galdre
  • 2,319
  • 17
  • 31
3
votes
1 answer

Lazy fibonacci in Ruby

I can write a lazy fibonacci in Clojure like this: (def fib (lazy-cat [1 1] (map +' fib (rest fib)))) and I'm trying (unsuccessfully) to write it in Ruby like this: fib = Enumerator.new do |yielder| yielder << 1 << 1 fib.zip(fib.drop(1)).map do…
bmaddy
  • 876
  • 9
  • 16
3
votes
3 answers

recursion in implementation of "partition" function

I was randomly reading through Clojure source code and I saw that partition function was defined in terms of recursion without using "recur": (defn partition ... ... ([n step coll] (lazy-seq (when-let [s (seq coll)] (let [p…
Jay Somedon
  • 1,068
  • 11
  • 27