lazy-sequences

How to produce a lazy sequence by portion in clojure?

折月煮酒 提交于 2019-12-05 12:12:05
I have a database server and I fetch data from it. Sometimes data have millions rows and more so I use laziness for downloading. I use Server Side Cursors from clojure.jdbc library https://funcool.github.io/clojure.jdbc/latest/#cursor-queries to fetch data lazily. Now I have a problem. I need produce initial 500 elements from a lazy-sequence, then the program must wait for 10 minutes to get a signal which report to the program produce next 500 elements and so on until i receive all data from server. But if a report didn`t arrive for 10 minutes, the program must close a connection. I wrote

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

帅比萌擦擦* 提交于 2019-12-05 04:38:44
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 pivot lgz partz) ] (recur nxxt)) (if-let [[oldpivot & rightpartz] partz] (cons oldpivot (qsort-inner

Scala error: “forward reference extends over definition of value” when code appears in a function

。_饼干妹妹 提交于 2019-12-04 22:03:19
I'm trying to compile the following code, using Scala 2.11.7. object LucasSeq { val fibo: Stream[Int] = 0 #:: 1 #:: fibo.zip(fibo.tail).map { pair => pair._1 + pair._2 } def firstKind(p: Int, q: Int): Stream[Int] = { val lucas: Stream[Int] = 0 #:: 1 #:: lucas.zip(lucas.tail).map { pair => p * pair._2 - q * pair._1 } lucas } } fibo is based on the Fibonacci sequence example in Scala's Stream documentation , and it works. However, the firstKind function, which tries to generalize the sequence with parameters p and q (making Lucas sequences of the first kind ), has the following error: LucasSeq

Is there a Python library for handling complicated mathematical sets (constructed using mathematical set-builder notation)?

心不动则不痛 提交于 2019-12-04 20:38:00
I often work with multidimensional arrays whose array indices are generated from a complicated user-specified set. I'm looking for a library with classes for representing complicated sets with an arbitrary number of indices, and arbitrarily complicated predicates. Given a set description, the desired output would be a generator. This generator would in turn produce either dict s or tuple s which correspond to the multidimensional array indices. Does such a library exist? Example Suppose we had the following user-specified set (in set-builder notation ), which represents the indices of some

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

我只是一个虾纸丫 提交于 2019-12-04 14:01:05
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 << ", " << t.val << ", " << val << std::endl; std::cout << "func-end \n"; } int main() { function(T(), T(), T()

Clojure: lazy magic

爷,独闯天下 提交于 2019-12-04 01:49:16
Almost 2 identical programs to generate infinite lazy seqs of randoms. The first doesn't crash. The second crash with OutOfMemoryError exception. Why? ;Return infinite lazy sequence of random numbers (defn inf-rand[] (lazy-seq (cons (rand) (inf-rand)))) ;Never returns. Burns the CPU but won't crash and lives forever. (last (inf-rand)) But the following crash pretty quickly: ;Return infinite lazy sequence of random numbers (defn inf-rand[] (lazy-seq (cons (rand) (inf-rand)))) (def r1 (inf-rand)) ;Crash with "OutOfMemoryError" (last r1) I believe this is an example of "holding onto the head". By

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

一世执手 提交于 2019-12-03 16:31:53
问题 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 request, but the reason I am looking for this technique is because I have a mix of data and recursive functions that depend on each other in a way requires a lot

lazy version of mapM

早过忘川 提交于 2019-12-03 11:25:07
Suppose, I'm getting large list of items while working with IO: as <- getLargeList Now, I'm trying to apply fn :: a -> IO b onto as : as <- getLargeList bs <- mapM fn as mapM has type mapM :: Monad m => (a -> m b) -> [a] -> m [b] , and that's what I need in terms of type matching. But it builds all the chain in memory until return the result. I'm looking for analog of mapM , which will work lazily, so that I may use head of bs while tail is still building. Do not use unsafeInterleaveIO or any lazy IO for that matter. This is precisely the problem that iteratees were created to address:

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

岁酱吖の 提交于 2019-12-03 06:28:52
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 request, but the reason I am looking for this technique is because I have a mix of data and recursive functions that depend on each other in a way requires a lot of nested let and letfn statements. I wanted to write the recursive functions that generate lazy

How to find length of lazy sequence without forcing realization?

佐手、 提交于 2019-12-01 16:13:48
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 realization, for instance, in the same page is an example of a lazy sequence of files that are