lazy-evaluation

Funky haskell lazy list implicit recursion

In Haskell, you can build infinite lists due to laziness: Prelude> let g = 4 : g Prelude> g !! 0 4 Prelude> take 10 g [4,4,4,4,4,4,4,4,4,4] Now, what exactly goes on when I try to construct a list like this? Prelude> let f = f !! 10 : f Prelude> f !! 0 Interrupted. Prelude> take 10 f [Interrupted. Prelude> The Interrupted.s are me ...

Lazy Initialization for data access

I'm accessing database to bulk populate fields. PopulateAobjs(); PopulateBobjs(); PopulateCobjs(); ... Once the values are populated, I store the entities in a Dictionary<Id, Entity>. I'd like to make this process lazy, in such a way that, if I don't need C objects then I don't need to call that method. etc. How can this be done? L...

Clojure: gc overhead limit exceeded, lazy evaluation, pi sequence

For the next code: (ns clojure101.series) (defn avg [[x y]] (/ (+ x y) 2)) (defn avg-damp [seq] (map avg (partition 2 seq))) (defn avg-damp-n [n] (apply comp (repeat n avg-damp))) (defn sums [seq] (reductions + seq)) (defn Gregory-Leibniz-n [n] (/ (Math/pow -1 n) (inc (* 2 n)))) (def Gregory-Leibniz-pi (map #(...

How should I avoid unintentionally capturing the local scope in function literals?

I'll ask this with a Scala example, but it may well be that this affects other languages which allow hybrid imperative and functional styles. Here's a short example (UPDATED, see below): def method: Iterator[Int] { // construct some large intermediate value val huge = (1 to 1000000).toList val small = List.fill(5)(s...

Haskell ByteStrings - ending up with large file loaded into memory

Greetings, I'm trying to understand why I'm seeing the entire file loaded into memory with the following program, yet if you comment out the line below "(***)" then the program runs in constant (about 1.5M) space. EDIT: The file is about 660MB, the field in column 26 is a date string like '2009-10-01', and there are one million lines....

Enumerating all pairs constructible from two lazy lists in OCaml

Hi everyone, I am attempting to enumerate the set of all pairs made of elements from two lazy lists (first element from the first list, second element from the second list) in OCaml using the usual diagonalization idea. The idea is, in strict evaluation terms, something like enum [0;1;2;...] [0;1;2;...] = [(0,0);(0,1);(1;0);(0;2);(1;1)...

Lazy "n choose k" in OCaml

Hi guys, As part of a bigger problem of enumerating a set, I need to write an OCaml function 'choose' which takes a list and outputs as the list of all possible sequences of size k made up of elements of that list (without repeating sequences which can be obtained from each other by permutation). The order they are put in the end list i...

How to get make stats in constant memory

I have a function, which creates some random numerical results. I know, that the result will be an integer in a (small, a - b approx 50) range a, b. I want to create a function which execute the above function let's say 1000000 times and calculates, how often the each result appears. (The function takes a random generator to produce the ...

How to get the nearest and the smallest token before the known one?

I have a string which looks like: "some token","another token","yet another token","Necessary Token","KNOWN TOKEN","next to known token" How to get "Necessary Token" with regular expression from the given string? I tried the next expression: \"(.+)\","KNOWN TOKEN" but it matches the whole substring from the beginning to the...