lazy-sequences

Lazy sequence generation in Rust

守給你的承諾、 提交于 2019-11-29 02:59:26
How can I create what other languages call a lazy sequence or a "generator" function? In Python, I can use yield as in the following example (from Python's docs) to lazily generate a sequence that is iterable in a way that does not use the memory of an intermediary list: # a generator that yields items instead of returning a list def firstn(n): num = 0 while num < n: yield num num += 1 sum_of_first_n = sum(firstn(1000000)) How can I do something similar in Rust? Lucian Rust 1.0 does not have generator functions, so you'd have to do it manually with explicit iterators . First, rewrite your

In Clojure, are lazy seqs always chunked?

╄→гoц情女王★ 提交于 2019-11-29 01:27:44
I was under the impression that the lazy seqs were always chunked. => (take 1 (map #(do (print \.) %) (range))) (................................0) As expected 32 dots are printed because the lazy seq returned by range is chunked into 32 element chunks. However, when instead of range I try this with my own function get-rss-feeds , the lazy seq is no longer chunked: => (take 1 (map #(do (print \.) %) (get-rss-feeds r))) (."http://wholehealthsource.blogspot.com/feeds/posts/default") Only one dot is printed, so I guess the lazy-seq returned by get-rss-feeds is not chunked. Indeed: => (chunked-seq

Lazy foreach on a Spark RDD

帅比萌擦擦* 提交于 2019-11-28 09:36:47
问题 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? 回答1: I could

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

送分小仙女□ 提交于 2019-11-28 07:15:24
问题 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

Clojure printing lazy sequence

南笙酒味 提交于 2019-11-27 16:01:28
I'm trying to print out my binary tree but Clojure is giving me a hard time printing out the sequences properly. So, I have a list of nodes '(1 2 3) for example. In each iteration I want to print out the node with a number of spaces before and after each element. (defn spaces [n] (apply str (repeat n " "))) Great, this seems to work. So, suppose I have a list of nodes '(:a :b :c) I want to print out on one line, with as said, the spaces. (println (map #(str (spaces before) % (spaces (dec before))) nodes)) I have a list of items. Using the map I get a list of string objects. Great, so I can

In Clojure, are lazy seqs always chunked?

烈酒焚心 提交于 2019-11-27 15:57:53
问题 I was under the impression that the lazy seqs were always chunked. => (take 1 (map #(do (print \.) %) (range))) (................................0) As expected 32 dots are printed because the lazy seq returned by range is chunked into 32 element chunks. However, when instead of range I try this with my own function get-rss-feeds , the lazy seq is no longer chunked: => (take 1 (map #(do (print \.) %) (get-rss-feeds r))) (."http://wholehealthsource.blogspot.com/feeds/posts/default") Only one

How to create lazy combinations

左心房为你撑大大i 提交于 2019-11-27 08:28:06
问题 My question is very simple, how do I make this code lazy: /* input: [ [1, 2], [3, 4], [5, 6] ] output: [ [1, 3, 5], [1, 3, 6], [1, 4, 5], [1, 4, 6], [2, 3, 5], [2, 3, 6], [2, 4, 5], [2, 4, 6], ] */ func combinations<T>(options: [[T]]) -> [[T]] { guard let head = options.first else { return [].map({ [$0] }) } if options.count == 1 { return head.map({ [$0] }) } let tailCombinations = combinations(options: Array(options.dropFirst())) return head.flatMap({ option in return tailCombinations.map({

How Are Lazy Sequences Implemented in Clojure?

自闭症网瘾萝莉.ら 提交于 2019-11-27 06:38:01
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 wrap recursive calls in a lazy sequence and no longer get a stack over flow for large computations? What resources do lazy sequences consume to do what it does? In what scenarios are lazy sequences inefficient? In what scenarios are lazy sequences most efficient?

Lazy sequence generation in Rust

隐身守侯 提交于 2019-11-27 04:32:33
问题 How can I create what other languages call a lazy sequence or a "generator" function? In Python, I can use yield as in the following example (from Python's docs) to lazily generate a sequence that is iterable in a way that does not use the memory of an intermediary list: # a generator that yields items instead of returning a list def firstn(n): num = 0 while num < n: yield num num += 1 sum_of_first_n = sum(firstn(1000000)) How can I do something similar in Rust? 回答1: Rust 1.0 does not have

Kotlin's Iterable and Sequence look exactly same. Why are two types required?

安稳与你 提交于 2019-11-26 18:38:15
Both of these interfaces define only one method public operator fun iterator(): Iterator<T> Documentation says Sequence is meant to be lazy. But isn't Iterable lazy too (unless backed by a Collection )? The key difference lies in the semantics and the implementation of the stdlib extension functions for Iterable<T> and Sequence<T> . For Sequence<T> , the extension functions perform lazily where possible, similarly to Java Streams intermediate operations. For example, Sequence<T>.map { ... } returns another Sequence<R> and does not actually process the items until a terminal operation like