lazy-evaluation

Clarification on Lazy Evaluation and its efficiency

ⅰ亾dé卋堺 提交于 2019-12-07 12:39:31
问题 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

Listing all the contents of a directory by breadth-first order results in low efficiency

时光毁灭记忆、已成空白 提交于 2019-12-07 10:05:30
问题 I writed a Haskell module to list all the contents of a directory by breadth-first order. The below is the source code. module DirElements (dirElem) where import System.Directory (getDirectoryContents, doesDirectoryExist) import System.FilePath ((</>)) dirElem :: FilePath -> IO [[FilePath]] dirElem dirPath = iterateM (not.null) (concatMapM getDirectoryContents') [dirPath] >>= return.tail getDirectoryContents' :: FilePath -> IO [FilePath] getDirectoryContents' dirPath = do isDir <- do

How to abstract lazy initialization in C++?

早过忘川 提交于 2019-12-07 09:00:49
问题 While refactoring some code for performance the other day, I needed an answer to creating member variables that are lazy initialized, but that also provides a convenient, though optional, non-lambda interface for non c++11 compilers. Here's the typical pattern for lazy instantiation that I want to abstract: if( !bInitialized ) { value = doInitialization(); bInitialized = true; } return value; For my use, I'd like some flexibility: allow explicit initialization, like the example above provide

How to create a memory efficient Ruby Pipe class with lazy evaluation?

﹥>﹥吖頭↗ 提交于 2019-12-07 08:09:25
I would like to create a Pipe class to emulate Unix commands in Ruby in a two step fashion. First step is to compile a pipeline by adding a number of commands, and the second step is to run that pipeline. Here is a mockup: #!/usr/bin/env ruby p = Pipe.new p.add(:cat, input: "table.txt") p.add(:cut, field: 2) p.add(:grep, pattern: "foo") p.add(:puts, output: "result.txt") p.run The question is how to code this using lazy evaluation , so that the pipe is processed record by record when run() is called without loading all of the data into memory at any one time? Take a look at the http://ruby-doc

Java split stream by predicate into stream of streams

天大地大妈咪最大 提交于 2019-12-07 05:00:45
问题 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: [

Getting functional sieve of Eratosthenes fast

岁酱吖の 提交于 2019-12-07 04:57:29
问题 I read this other post about a F# version of this algorithm. I found it very elegant and tried to combine some ideas of the answers. Although I optimized it to make fewer checks (check only numbers around 6) and leave out unnecessary caching, it is still painfully slow. Calculating the 10,000 th prime already take more than 5 minutes. Using the imperative approach, I can test all 31-bit integers in not that much more time. So my question is if I am missing something that makes all this so

lazy variable with closure

a 夏天 提交于 2019-12-07 04:27:47
问题 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? 回答1: The tutorial code you quote is this: private lazy var variable:SomeClass = { let fVariable = SomeClass()

iOS lazy var UIBarButtonItem target issue

笑着哭i 提交于 2019-12-07 04:26:36
问题 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

Lazy Load on MULTIPLE horizontal containers

ぐ巨炮叔叔 提交于 2019-12-07 04:04:25
I'm using Lazy Load jQuery plugin: http://www.appelsiini.net/projects/lazyload My question is: is it possible to have multiple scrolling containers each with a lazyload images inside, like this: $("#container1 img.lazy").lazyload({ container: $("#container1")}); $("#container2 img.lazy").lazyload({ container: $("#container2")}); When I do this it seems to only keep track of the most recently called. I tried to call the function two times (one for each container) but it didn't work, I also tried to call both containers this way: container: $("#container1, #container2")}); and didn't work either

Can someone please explain this lazy evaluation code?

家住魔仙堡 提交于 2019-12-07 03:41:53
问题 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>