lazy-sequences

How can I make a ruby enumerator that does lazy iteration through two other enumerators?

ε祈祈猫儿з 提交于 2019-12-01 15:54:37
问题 Let's say I have two enumerators, enum1 and enum2 that must be lazily iterated through (because they have side effects). How do I construct a third enumerator enum3 where enum3.each{|x| x} would lazily return the equivalent of enum1 + enum2 ? In my real world use case, I'm streaming in two files, and need to stream out the concatenation. 回答1: This seems to work just how I want; enums.lazy.flat_map{|enum| enum.lazy } Here's the demonstration. Define these yielding methods with side-effects;

How to find length of lazy sequence without forcing realization?

眉间皱痕 提交于 2019-12-01 15:02:18
问题 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 without realizing its contents. My question is, How this is done and why it's so rare? Unfortunately, the book does not specify these things in this section. I personally think that it's very useful to know the length of a lazy sequence prior it's

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

假如想象 提交于 2019-12-01 08:32:06
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(100,100000))] answers[::randint(3,19)]=devtrue() print answers I found this code, but do not yet understand, how to apply it in this case: http://code.activestate.com/recipes/576585-lazy-recursive-generator-function/ You can call close() on the generator object. This way, a

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

可紊 提交于 2019-12-01 06:52:07
问题 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(100,100000))] answers[::randint(3,19)]=devtrue() print answers I found this code, but do not yet understand, how to apply it in this case: http://code.activestate.com/recipes

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

为君一笑 提交于 2019-12-01 02:07:49
问题 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 too compact: (defn qsort-inner [work] (lazy-seq (loop [loopwork work] (let [[ part & partz ] loopwork ] (if-let [[pivot & valuez] (seq part)] (let [ smaller? #(< % pivot) smz (filter smaller? valuez) lgz (remove smaller? valuez) nxxt (list* smz

Printing a tree lazily in Newick format

我怕爱的太早我们不能终老 提交于 2019-12-01 00:24:40
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 tree->newick [tree] (let [{:keys [id children to-parent]} tree dist (double to-parent)] ; to-parent may be a rational (if children (str "(" (tree->newick (first children)) "," (tree->newick (second children)) "):" dist) (str (name id) ":" dist)))) (def example {:id nil :to-parent 0.0 :children [{:id nil :to-parent 0.5 :children [{:id "A" :to-parent 0.3

mapcat breaking the lazyness

浪尽此生 提交于 2019-11-30 19:08:06
I have a function that produces lazy-sequences called a-function. If I run the code: (map a-function a-sequence-of-values) it returns a lazy sequence as expected. But when I run the code: (mapcat a-function a-sequence-of-values) it breaks the lazyness of my function. In fact it turns that code into (apply concat (map a-function a-sequence-of-values)) So it needs to realize all the values from the map before concatenating those values. What I need is a function that concatenates the result of a map function on demand without realizing all the map beforehand. I can hack a function for this:

Can someone explain this lazy Fibonacci solution?

匆匆过客 提交于 2019-11-30 17:32:29
问题 This is the code: fibs = 0 : 1 : zipWith (+) fibs (drop 1 fibs) When evaluated, fibs is an infinite list of Fibonacci numbers. What I don't understand is how the list is concatenated. zipWith returns a list, so zipping fibs would yield this: 0 : 1 : [1] : [1,2] : [1,2,3] Because 0 : 1 : zipWith (+) [0,1] [1] yields [1] and zipWith (+) [0,1,1] [1,1] yields [1,2] etc). However, when I run the code, I get the correct result. What am I not understanding here? 回答1: Your "Because" is not telling

Lazy foreach on a Spark RDD

吃可爱长大的小学妹 提交于 2019-11-29 16:06:35
I have a big RDD of Strings (obtained through a union of several sc.textFile(...)) . I now want to search for a given string in that RDD, and I want the search to stop when a "good enough" match has been found. I could retrofit foreach , or filter , or map for this purpose, but all of these will iterate through every element in that RDD, regardless of whether the match has been reached. Is there a way to short-circuit this process and avoid iterating through the whole RDD? zero323 I could retrofit foreach, or filter, or map for this purpose, but all of these will iterate through every element

Clojure: Idiomatic way to call contains? on a lazy sequence

∥☆過路亽.° 提交于 2019-11-29 13:38:34
Is there an idiomatic way of determining if a LazySeq contains an element? As of Clojure 1.5 calling contains? throws an IllegalArgumentException: IllegalArgumentException contains? not supported on type: clojure.lang.LazySeq clojure.lang.RT.contains (RT.java:724) Before 1.5, as far as I know, it always returned false. I know that calling contains? on a LazySeq may never return as it can be infinite. But what if I know it isn't and don't care if it is evaluated eagerly? What I came up with is: (defn lazy-contains? [col key] (not (empty? (filter #(= key %) col)))) But it doesn't feel quite