lazy-evaluation

How could I make my own lazy iterator?

对着背影说爱祢 提交于 2019-12-05 17:13:37
问题 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

Haskell Lazy Evaluation and Reuse

不问归期 提交于 2019-12-05 16:19:23
问题 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

Clarification on Lazy Evaluation and its efficiency

我怕爱的太早我们不能终老 提交于 2019-12-05 15:42:04
I came across following sentence on Real World Haskell: Lazy evaluation has some spooky effects. Let's say we want to find the k least-valued elements of an unsorted list. In a traditional language, the obvious approach would be to sort the list and take the first k elements, but this is expensive. For efficiency, we would instead write a special function that takes these values in one pass, and it would have to perform some moderately complex book-keeping. In Haskell, the sort-then-take approach actually performs well: laziness ensures that the list will only be sorted enough to find the k

How is foldl lazy?

扶醉桌前 提交于 2019-12-05 14:30:34
问题 There are lots of good questions and answers about foldl , foldr , and foldl' in Haskell. So now I know that: 1) foldl is lazy 2) don't use foldl because it can blow up the stack 3) use foldl' instead because it is strict (ish) How foldl is evaluated: 1) a whole bunch of thunks are created 2) after Haskell is done creating thunks, the thunks are reduced 3) overflow the stack if there are too many thunks What I'm confused about: 1) why does reduction have to occur after all thunk-ing? 2) why

InvalidOperationException in my Lazy<> value factory

寵の児 提交于 2019-12-05 11:16:19
问题 I have a class containing something like the following: public static class Config { private static Lazy<ConfigSource> _cfgSrc = new Lazy<ConfigSource>( () => { /* "ValueFactory" here... */ }, true); public static ConfigSource ConfigSource { get { return _cfgSrc.Value; } } } In accessing the ConfigSource property, I encountered this InvalidOperationException : ValueFactory attempted to access the Value property of this instance. I don't see anything in my "value factory" method that accesses

lazy variable with closure

做~自己de王妃 提交于 2019-12-05 10:51:11
In this article , it says (referencing the code below): "You must use lazy to prevent the closure for being created more than once." private lazy var variable:SomeClass = { let fVariable = SomeClass() fVariable.value = 10 return fVariable }() Why would lazy prevent the closure from being created more than once? And why would the lack of lazy cause it to evaluate more than once? The tutorial code you quote is this: private lazy var variable:SomeClass = { let fVariable = SomeClass() fVariable.value = 10 return fVariable }() Contrast it with this: private var variable:SomeClass { let fVariable =

lazy list computed using mutable state?

孤街浪徒 提交于 2019-12-05 10:07:41
I'd like to figure out in general how to use mutable state in the computation of lazy lists. For instance, here is a naive Sieve of Eratosthenes implemented using a mutable array ( source ): import Control.Monad.ST import Data.Array.ST import Data.Array.Unboxed import Control.Monad import Data.List prime :: Int -> UArray Int Bool prime n = runSTUArray $ do arr <- newArray ( 2 , n ) True :: ST s ( STUArray s Int Bool ) forM_ ( takeWhile ( \x -> x*x <= n ) [ 2 .. n ] ) $ \i -> do ai <- readArray arr i when ( ai ) $ forM_ [ i^2 , i^2 + i .. n ] $ \j -> do writeArray arr j False -- yield i ???

iOS lazy var UIBarButtonItem target issue

允我心安 提交于 2019-12-05 08:58:26
I found this UIBarButtonItem target issue unconsciously when using lazy var initialization. class ViewController: UIViewController { lazy var barButtonItem1 = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(action1)) lazy var barButtonItem2: UIBarButtonItem = { let barButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(action2)) return barButtonItem } override func viewDidLoad() { super.viewDidLoad() print(barButtonItem1.target, barButtonItem2.target) } } The printed results showed that barButtonItem1.target was nil, and

Java split stream by predicate into stream of streams

北慕城南 提交于 2019-12-05 08:30:02
I have hundreds of large (6GB) gziped log files that I'm reading using GZIPInputStream s that I wish to parse. Suppose each one has the format: Start of log entry 1 ...some log details ...some log details ...some log details Start of log entry 2 ...some log details ...some log details ...some log details Start of log entry 3 ...some log details ...some log details ...some log details I'm streaming the gziped file contents line by line through BufferedReader.lines() . The stream looks like: [ "Start of log entry 1", " ...some log details", " ...some log details", " ...some log details", "Start

Can someone please explain this lazy evaluation code?

夙愿已清 提交于 2019-12-05 08:04:00
So, this question was just asked on SO: How to handle an "infinite" IEnumerable? My sample code: public static void Main(string[] args) { foreach (var item in Numbers().Take(10)) Console.WriteLine(item); Console.ReadKey(); } public static IEnumerable<int> Numbers() { int x = 0; while (true) yield return x++; } Can someone please explain why this is lazy evaluated? I've looked up this code in Reflector, and I'm more confused than when I began. Reflector outputs: public static IEnumerable<int> Numbers() { return new <Numbers>d__0(-2); } For the numbers method, and looks to have generated a new