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

how to get empty groups, lazily

I want to group objects by a boolean value, and I need to always get two groups (one for true, one for false), no matter if there are any elements in them. The usual approach using GroupBy does not work, as it will only generate nonempty groups.…
Kjara
  • 2,504
  • 15
  • 42
2
votes
1 answer

Is there a way to construct lazy sequences in Python?

There is a Django view that loads Member objects from the database with a certain filter. Now I need to change this logic to present a specific Member first, and let the rest follow in their natural order. The most straightforward way is to execute…
Dan Abramov
  • 264,556
  • 84
  • 409
  • 511
2
votes
3 answers

how to build a chunked lazy-seq that blocks?

I'd like to use chunked cons or some other way to create a lazy-seq that blocks. Given a source: (defn -source- [] (repeatedly (fn [] (future (Thread/sleep 100) [1 2])))) (take 2 (-source-)) ;; => ( ) I'd like to have a function…
zcaudate
  • 13,998
  • 7
  • 64
  • 124
2
votes
4 answers

SICP Chapter 3.5.2 infinite streams integers definition

I am reading SICP and am having difficulty understanding one example provided for infinite streams: https://mitpress.mit.edu/sites/default/files/sicp/full-text/book/book-Z-H-24.html#%_sec_3.5.2 We can do more interesting things by manipulating…
Gregory Kuhn
  • 1,627
  • 2
  • 22
  • 34
2
votes
2 answers

SML Lazy sort of int list using streams

The question 1 Streams and lazy evaluation (40 points) We know that comparison sorting requires at least O(n log n) comparisons where were are sorting n elements. Let’s say we only need the first f(n) elements from the sorted list, for some…
Vasyagamer
  • 21
  • 2
2
votes
2 answers

Clojure evaluate lazy sequence

Hi I need to return string from stringify function, but currently I'm getting lazy seq. Can't wrap my head how to evaluate lazy seq. So what I need is to have chain call like this: (println "Formated: " (fmt (stringify m))) (defn stringify [m] …
Constantine
  • 1,802
  • 3
  • 23
  • 37
2
votes
5 answers

how to implement takeUntil with Scala lazy collections

I have an expensive function which I want to run as few times as possible with the following requirement: I have several input values to try If the function returns a value below a given threshold, I don't want to try other inputs if no result is…
Raphael Roth
  • 26,751
  • 15
  • 88
  • 145
2
votes
1 answer

Generic Sequence over natural numbers

I am trying to create a generic sequence, that would behave the following: val generic_sequence= fn : (int -> int) -> int seq that is, It should receive as an input a function: foo: int -> int and create a sequence that activates foo on all…
genericname
  • 167
  • 3
  • 11
2
votes
1 answer

How does sorting work in lazy sequences?

Assuming that I'm working with a lazy sequence and sort of an infinite sequence, then I try to write something like (pseudo code): Sequence([1,2,3,...]) .sortDescending() .take(10); In this scenario, I'm sorting first and then taking 10…
Afshin Mehrabani
  • 33,262
  • 29
  • 136
  • 201
2
votes
1 answer

Clojure: Extract maps with specific values from Lazy Sequence

I have a Clojure Lazy Sequence: { {:keyOne 123, :keyTwo "TestVal"} {:keyOne 456, :keyTwo "Value2"} {:keyOne 789, :keyTwo "TestVal"} } I want to get the maps which have a specific value for a given key, e.g. I want all maps which have…
Jdv
  • 962
  • 10
  • 34
2
votes
1 answer

Totality and searching for elements in Streams

I want a find function for Streams of size-bounded types which is analogous to the find functions for Lists and Vects. total find : MaxBound a => (a -> Bool) -> Stream a -> Maybe a The challenge is it to make it: be total consume no more than…
2
votes
1 answer

Writing generic takeWhile with an offset in Scala

I have a use case for takeWhile, but where I want to keep a fixed number of items after predicate is true. I'm also trying to write it to be as generic as possible for the type of collection. So something like: def takeWhileWithOffset[A, Iter[_] <:…
acjay
  • 34,571
  • 6
  • 57
  • 100
2
votes
2 answers

Scala function call without "." (dot) vs using "." (dot)

Can someone help me understand what is going on here. I have this definition for generating primes: def primes: Stream[Long] = { 2 #:: 3 #:: 5 #:: 7 #::Stream.iterate(11L)(_ + 2).filter { n => primes takeWhile (p => p*p <= n) forall (n %…
smac89
  • 39,374
  • 15
  • 132
  • 179
2
votes
1 answer

Analog of python groupby in haskell

In python there is groupby function. It's type can be expressed in haskell like this groupby :: a->b->[a]->[(b, [a])] Because it need data to be sorted we can think of it's running time as O(n*log(n)). I was probably not the only one dissatisfied…
user1685095
  • 5,787
  • 9
  • 51
  • 100
2
votes
1 answer

Merge (group-by) huge sequences lazily in clojure

EXAMPLE: We have two time-series lazy sequences of map created by reading csv. The two lazy-sequences start at different days: INPUT lazy-seq1 ({:date "20110515" :val1 123} {:date "20110516" :val1 143} {:date "20110517" :val1 1153} ...) …
naoki fujita
  • 689
  • 1
  • 9
  • 13