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
8
votes
4 answers

Infinite fibonacci sequence

I'm trying to imitate Haskell's famous infinite fibonacci list in F# using sequences. Why doesn't the following sequence evaluate as expected? How is it being evaluated? let rec fibs = lazy (Seq.append (Seq.ofList [0;1]) …
is7s
  • 3,500
  • 1
  • 20
  • 41
8
votes
2 answers

How to find length of lazy sequence without forcing realization?

I'm currently reading the O'reilly Clojure programming book which it's says the following in it's section about lazy sequences: It is possible (though very rare) for a lazy sequence to know its length, and therefore return it as the result of count…
izaban
  • 1,013
  • 7
  • 15
8
votes
2 answers

Get element from sequence in clojure

I understand that lists and vectors in Clojure can be used almost interchangeably in most situations. Here is a simple case that surprised me (nth [2 4] 0) ;=> 2 (nth '(2 4) 0) ;=> 2 (get [2 4] 0) ;=> 2 (get '(2 4) 0) ;=> nil -- wtf??? The…
Adam Schmideg
  • 10,590
  • 10
  • 53
  • 83
8
votes
2 answers

How to improve text processing performance in Clojure?

I'm writing a simple desktop search engine in Clojure as a way to learn more about the language. Until now, the performance during the text processing phase of my program is really bad. During the text processing I've to: Clean up unwanted…
luisgabriel
  • 2,483
  • 1
  • 15
  • 10
7
votes
5 answers

clojure rest and next related

I was following The Joy of Clojure and I am puzzled with these 2 statements (def very-lazy (-> (iterate #(do (print \.) (inc %)) 1) rest rest rest)) (def less-lazy (-> (iterate #(do (print \.) (inc %)) 1) next next next)) accordingly, the output…
thlim
  • 2,908
  • 3
  • 34
  • 57
7
votes
2 answers

How to slip gather-take in lazy manner into map?

I need to construct following flow: Accept list of file names Extract multiple lines from those files Process those lines However I have no idea how to properly inject gather-take into map: sub MAIN ( *@file-names ) { @file-names.map( { slip…
7
votes
1 answer

About Laziness [ RAKU ]

In Raku documentation it is stated that gather-take constructs are being lazy evaluated. In the following examples I have a hard time concluding about the laziness of the constructs: say 'Iterate to Infinity is : ', (1 ... Inf).WHAT; say 'gather is…
jakar
  • 1,701
  • 5
  • 14
7
votes
1 answer

When can I rely on Haskell to read a list lazily?

Why do I get an infinite loop (<>) runtime error here? file feedback.hs: plus1 :: [Int]->[Int] -- add 1 to input stream plus1 [] = [] plus1 (x:xs) = (x+1): plus1 xs to10 :: [Int] -> [Int] -- stop the input stream when it gets to 10 to10…
Daniel Patru
  • 1,968
  • 18
  • 15
7
votes
1 answer

What is the real answer to SICP 3.57

SICP Exercise 3.57: How many additions are performed when we compute the nth Fibonacci number using the definition of fibs based on the add-streams procedure? Show that the number of additions would be exponentially greater if we had implemented…
nate
  • 71
  • 4
7
votes
2 answers

How to reify Prolog's backtracking state to perform the same task as "lazy seq" from Clojure?

Here is a quicksort algorithm for numbers written in Clojure. It is basically the quicksort algorithm found in "The Joy of Clojure", 2nd edition, page 133. I modified it slightly for (hopefully) better readability, because the original felt a bit…
David Tonhofer
  • 14,559
  • 5
  • 55
  • 51
7
votes
1 answer

How to write an `intersperse` function in Perl 6

One thing I've been missing in Perl 6, is an intersperse function like Haskell has: The intersperse function takes an element and a list and `intersperses' that element between the elements of the list. E.g. this: intersperse , (, ,…
smls
  • 5,738
  • 24
  • 29
7
votes
3 answers

Lazy Sequences that "Look Ahead" for Project Euler Problem 14

I'm trying to solve Project Euler Problem 14 in a lazy way. Unfortunately, I may be trying to do the impossible: create a lazy sequence that is both lazy, yet also somehow 'looks ahead' for values it hasn't computed yet. The non-lazy version I…
ivar
  • 1,484
  • 12
  • 20
7
votes
1 answer

Immutable.js - lazy sequence

I recently heard about Facebook's Immutable.js library (https://github.com/facebook/immutable-js). I am confused about the following from their documentation: var oddSquares = Immutable.Sequence(1,2,3,4,5,6,7,8) .filter(x => x % 2).map(x => x *…
tldr
  • 11,924
  • 15
  • 75
  • 120
7
votes
1 answer

why is this looping function so slow compared to map?

I looked at maps source code which basically keeps creating lazy sequences. I would think that iterating over a collection and adding to a transient vector would be faster, but clearly it isn't. What don't I understand about clojures performance…
Core
  • 840
  • 11
  • 24
7
votes
1 answer

Why is line-seq returning clojure.lang.Cons instead of clojure.lang.LazySeq?

According to the ClojureDocs entry for line-seq (http://clojuredocs.org/clojure_core/clojure.core/line-seq) and the accepted answer for the Stack question (In Clojure 1.3, How to read and write a file), line-seq should return a lazy seq when passed…
user1922460
1 2
3
18 19