lazy-sequences

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 number: (defn good [] (lazy-seq (cons 42 (good)))) user> (take 5 (good)) ...

how to unit test for laziness

I have a function that is supposed to take a lazy seq and return an unrealized lazy seq. Now I want to write a unit test (in test-is btw) to make sure that the result is an unrealized lazy sequence. ...

Iterator blocks in Clojure?

I am using clojure.contrib.sql to fetch some records from an SQLite database. (defn read-all-foo [] (with-connection *db* (with-query-results res ["select * from foo"] (into [] res)))) Now, I don't really want to realize the whole sequence before returning from the function (i.e. I want to keep it lazy), but if I return ...

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 wrote to test correctness was: (defn chain-length [num] (loop [len 1 n num] (con...

Clojure Lazy Sequences that are Vectors

I have noticed that lazy sequences in Clojure seem to be represented internally as linked lists (Or at least they are being treated as a sequence with only sequential access to elements). Even after being cached into memory, access time over the lazy-seq with nth is O(n), not constant time as with vectors. ;; ...created my-lazy-seq here...

Are there sequence-operator implementations in .NET 4.0?

With that I mean similar to the Linq join, group, distinct, etc. only working on sequences of values, not collections. The difference between a sequence and a collection is that a sequence might be infinite in length, whereas a collection is finite. Let me give you an example: var c1 = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; var c2 =...

How Are Lazy Sequences Implemented in Clojure?

I like Clojure. One thing that bothers me about the language is that I don't know how lazy sequences are implemented, or how they work. I know that lazy sequences only evaluate the items in the sequence that are asked for, how does it do this? What makes lazy sequences so efficient that they don't consume much stack? How come you can ...

How do I write a predicate that checks if a value exists in an infinite seq?

I had an idea for a higher-order function today that I'm not sure how to write. I have several sparse, lazy infinite sequences, and I want to create an abstraction that lets me check to see if a given number is in any of these lazy sequences. To improve performance, I wanted to push the values of the sparse sequence into a hashmap (or se...

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 randint def devtrue(): while True: yield True answers=[False for _ in range(randint(1...

How to create a lazy-seq generating, anonymous recursive function in Clojure?

Edit: I discovered a partial answer to my own question in the process of writing this, but I think it can easily be improved upon so I will post it anyway. Maybe there's a better solution out there? I am looking for an easy way to define recursive functions in a let form without resorting to letfn. This is probably an unreasonable reque...

How do I avoid Clojure's chunking behavior for lazy seqs that I want to short circuit?

I have a long, lazy sequence that I want to reduce and test lazily. As soon as two sequential elements are not = (or some other predicate) to each other, I want to stop consuming the list, which is expensive to produce. Yes, this sounds like take-while, but read further. I wanted to write something simple and elegant like this (pretendi...

Clojure/Java: Most effective method for minimizing bandwidth consumption when performing complex operations on a stream of Amazon S3 data

Hi All, I'm performing streaming reads of an object using BufferedReader. I need to do two things with this object: Pass it to a SuperCSV csv reader Obtain the raw lines and keep them in a (Clojure) lazy sequence Currently, I am having to use two different BufferedReaders: one as an argument to a SuperCSV CSV reader class and one t...