lazy-sequences

Why does my lazy filtered list in scheme consume so much memory?

雨燕双飞 提交于 2021-02-10 21:54:10
问题 I'm currently learning to use some slightly more advanced features of scheme, and I've hit a road-block with lazy lists. Basically, I'm trying to create an infinite, lazily generated list, and apply a lazy filter on it, and only take a single element. My hope was that this would consume very little memory: the filter looks at only one element at a time, and there's no need to store the previous entries. Here is my attempt at this: (define lazy-inf-seq (lambda (start next) (delay (cons start

compactMap on sequence() not lazy?

徘徊边缘 提交于 2020-02-02 11:18:06
问题 Every once in a while I have to walk up the responder chain to reach an instance of a known class. (Just accept this for purposes of the question.) I've been doing this with a while loop, but it occurred to me that it would be cooler to use sequence() , which can express the responder chain itself neatly like this: let chain = sequence(first: someView as UIResponder) {$0.next} That's brilliant because so far we haven't actually done any walking; the sequence is lazy and the anonymous function

Lazy Fibonacci Sequence in Ruby

梦想与她 提交于 2020-01-06 12:49:11
问题 Coming from Python if I wanted to create an iterative lazy Fibonacci sequence, I could do something like this: def fib(): a = 1 b = 2 yield a yield b while True: yield a + b tmp = a a = b b = tmp + b Grabbing next(fib) will give me the next element in the sequence by simply adding the previous two elements, so if I want to get the first 1000 Fibonacci elements, I can do that quickly: fib = fib() for i in range(0,1000): print(next(fib)) If I try to reproduce that in Ruby with an Enumerator, it

doseq over a simple lazy seq runs out of heap space

六月ゝ 毕业季﹏ 提交于 2020-01-02 17:16:54
问题 When stress-testing some Clojure code at work, I noticed it runs out of heap space when iterating over large data-sets. I eventually managed to trace the issues back to the combination of Clojure's doseq function, and implementation fo lazy sequences. This is the minimal code snippet that crashes Clojure by exhausting available heap space: (doseq [e (take 1000000000 (iterate inc 1))] (identity e)) The documentation for doseq clearly states that it doesn't retain the head of the lazy sequence,

doseq over a simple lazy seq runs out of heap space

感情迁移 提交于 2020-01-02 17:16:14
问题 When stress-testing some Clojure code at work, I noticed it runs out of heap space when iterating over large data-sets. I eventually managed to trace the issues back to the combination of Clojure's doseq function, and implementation fo lazy sequences. This is the minimal code snippet that crashes Clojure by exhausting available heap space: (doseq [e (take 1000000000 (iterate inc 1))] (identity e)) The documentation for doseq clearly states that it doesn't retain the head of the lazy sequence,

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

谁说我不能喝 提交于 2020-01-02 03:31:08
问题 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 a java.io.BufferedReader. However when I test this in the REPL, the type is listed as clojure.lang.Cons. See the code below: => (ns stack-question (:require [clojure.java.io :as io])) nil => (type (line-seq (io/reader "test-file.txt"))) clojure

Does standard C++11 guarantee that temporary object passed to a function will have been created before function call?

馋奶兔 提交于 2020-01-01 15:56:22
问题 Does standard C++11 guarantee that all 3 temporary objects have been created before the beginning performe the function? Even if temporary object passed as: object rvalue-reference passed only member of temporary object http://ideone.com/EV0hSP #include <iostream> using namespace std; struct T { T() { std::cout << "T created \n"; } int val = 0; ~T() { std::cout << "T destroyed \n"; } }; void function(T t_obj, T &&t, int &&val) { std::cout << "func-start \n"; std::cout << t_obj.val << ", " <<

How to define the partitions (factorizations w.r.t. concatenation) of a sequence as a lazy sequence of lazy sequences in Clojure

倾然丶 夕夏残阳落幕 提交于 2019-12-25 01:39:56
问题 I am new to Clojure and I want to define a function pt taking as arguments a number n and a sequence s and returning all the partitions of s in n parts, i.e. its factorizations with respect to n -concatenation. for example (pt 3 [0 1 2]) should produce: (([] [] [0 1 2]) ([] [0] [1 2]) ([] [0 1] [2]) ([] [0 1 2] []) ([0] [] [1 2]) ([0] [1] [2]) ([0] [1 2] []) ([0 1] [] [2]) ([0 1] [2] []) ([0 1 2] [] [])) with the order being unimportant. Specifically, I want the result to be a lazy sequence

Generic LazyCollection type

送分小仙女□ 提交于 2019-12-24 14:01:04
问题 I need a function that returns a lazy generator of various composed generator functions such as filter and map. For example, if I want to apply lazy.filter().map() the code looks like: // Simplified typealias MyComplexType = Int typealias MyComplexCollection = [MyComplexType] func selection() -> LazyMapCollection<LazyFilterCollection<MyComplexCollection>, Int> { let objects:MyComplexCollection = [1, 2, 3, 4, 5, 6] let result = objects.lazy.filter({$0 < 4}).map({$0 * 10}) return result } for

Why is this recursive map function only being applied to the last two elements in the list?

半城伤御伤魂 提交于 2019-12-24 01:18:33
问题 This is the problem given: What are the first 8 elements in the following list? mystery = 0 : 10 : (map(+1)mystery) The answer is [0,10,1,11,2,12,3,13...] But in my opinion the answer should be [0,10,1,11,1,11,2,12] . The following steps show why: 1) We are given ;list [0,10] so after applying the function the first time we have the list [ 0,10,1, 11] 2) Now we have a list [ 0,10,1,11] so after applying the function again the resulting list should be [0,10,1,11,1,11,2,12] Apparently that is