lazy-evaluation

Lazy quantifier {,}? not working as I would expect

时光毁灭记忆、已成空白 提交于 2019-12-04 03:40:44
问题 I have an issue with lazy quantifiers. Or most likely I misunderstand how I am supposed to use them. Testing on Regex101 My test string is let's say: 123456789D123456789 .{1,5} matches 12345 .{1,5}? matches 1 I am OK with both matches. .{1,5}?D matches 56789D !! I would expect it to match 9D Thanks for clarifying this. 回答1: First and foremost, please do not think of greediness and laziness in regex as means of getting the longest/shortest match . "Greedy" and "lazy" terms only pertain to the

Why this Haskell code never terminates?

痞子三分冷 提交于 2019-12-04 03:31:06
问题 I recently wrote some Haskell code and it never terminates. After I carefully examined my code, the problem boiled down to the following code piece main :: IO () main = print $ let a = 10 in let a = a in a :: Int I guess this must have something to do with the laziness of Haskell since the same code terminates in OCaml. However, if I wrote the following code instead main :: IO () main = print $ let a = 10 in let b = a in b :: Int the code would have no problem terminating at all. I can't get

How could I make my own lazy iterator?

廉价感情. 提交于 2019-12-04 03:09:20
I'm making a C++11 class that produces a huge amount of data. That data currently comes from a database and it cannot fit entirely in memory. I would like to provide the user with an iterator that behaves like regular STL iterators, but that would be lazy. More precisely, I would be able to do something like that : for (auto& item : big_bunch_of_data) { do_stuff_with(item); } With item being retrieved from the database only at each iteration. If I'm right, this new syntax is sugar for for (stuff::iterator it = big_bunch_of_data.begin();it != big_bunch_of_data.end();it++) { do_stuff_with(*it);

Why does Scala evaluate the argument for a call-by-name parameter if the method is infix and right-associative?

我只是一个虾纸丫 提交于 2019-12-04 02:57:34
问题 As I understood call-by-name parameters of a method, the corresponding argument expression will not be evaluated when passing it to the method, but only when (and if) the value of the parameter is used in the method body. In the following example, however, this is only true in the first two method calls, but not in the third one, although it should be a merely syntactical variation of the second case!? Why is the argument expression evaluated in the third method call? (I tested this code

Lazily transpose a list in Python

浪子不回头ぞ 提交于 2019-12-04 02:34:25
So, I have an iterable of 3-tuples, generated lazily. I'm trying to figure out how to turn this into 3 iterables, consisting of the first, second, and third elements of the tuples, respectively. However, I wish this to be done lazily. So, for example, I wish [(1, 2, 3), (4, 5, 6), (7, 8, 9)] to be turned into [1, 4, 7] , [2, 5, 8] , [3, 6, 9] . (Except I want iterables not lists.) The standard zip(*data) idiom doesn't work, because the argument unpacking expands the entire iterable. (You can verify this by noting that zip(*((x, x+1, x+2) for x in itertools.count(step=3))) hangs.) The best I've

Haskell Lazy Evaluation and Reuse

青春壹個敷衍的年華 提交于 2019-12-04 02:30:15
I know that if I were to compute a list of squares in Haskell, I could do this: squares = [ x ** 2 | x <- [1 ..] ] Then when I call squares like this: print $ take 4 squares And it would print out [1.0, 4.0, 9.0, 16.0]. This gets evaluated as [ 1 ** 2, 2 ** 2, 3 ** 2, 4 ** 2 ]. Now since Haskell is functional and the result would be the same each time, if I were to call squares again somewhere else, would it re-evaluate the answers it's already computed? If I were to re-use squares after I had already called the previous line, would it re-calculate the first 4 values? print $ take 5 squares

Lazy transform in C++

爷,独闯天下 提交于 2019-12-04 02:26:57
I have the following Python snippet that I would like to reproduce using C++: from itertools import count, imap source = count(1) pipe1 = imap(lambda x: 2 * x, source) pipe2 = imap(lambda x: x + 1, pipe1) sink = imap(lambda x: 3 * x, pipe2) for i in sink: print i I've heard of Boost Phoenix , but I couldn't find an example of a lazy transform behaving in the same way as Python's imap . Edit: to clarify my question, the idea is not only to apply functions in sequence using a for , but rather to be able to use algorithms like std::transform on infinite generators. The way the functions are

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

Does Prolog use Eager Evaluation?

我怕爱的太早我们不能终老 提交于 2019-12-04 01:11:40
问题 Because Prolog uses chronological backtracking(from the Prolog Wikipedia page) even after an answer is found(in this example where there can only be one solution), would this justify Prolog as using eager evaluation? mother_child(trude, sally). father_child(tom, sally). father_child(tom, erica). father_child(mike, tom). sibling(X, Y) :- parent_child(Z, X), parent_child(Z, Y). parent_child(X, Y) :- father_child(X, Y). parent_child(X, Y) :- mother_child(X, Y). With the following output: ?-

clojure deferred function execution

痞子三分冷 提交于 2019-12-04 00:56:18
Ok Here is what i am trying to do (defn addresses [person-id] ;addresses-retrival ) (defn person [id] (merge {:addresses (addresses id)} {:name "john"})) In the above person function i want addresses to be retrieved only on demand , like only when i do (:addresses (person 10)) and not when (person 10) I am not sure if i am going about this right, being new to clojure. Nicolas Oury You can use delay. (defn person [id] (delay {:addresses (addresses id) :name "john"})) (person 2) will then return a delayed, without evaluating anything. To access the content and evaluate the delayed object, use