lazy-evaluation

Difference between Haskell's Lazy and Strict monads (or transformers)

久未见 提交于 2019-11-30 04:40:48
When browsing Hackage, most of the monads have a Lazy and a Strict version. What is the difference exactly? Can you highlight it with some examples for the common monads (State, Reader, Writer)? I don't know of a separation into lazy and strict for the reader monad, the reason for the State(T) and Writer(T) separation doesn't apply there. The difference between the lazy and strict Writer and State monads resp. their monad transformers is the implementation of the monadic bind (>>=) , fmap etc. In the strict versions, the implementation pattern-matches on the pair ( (result, state) , resp.

Is it bad practice to have my getter method change the stored value?

大憨熊 提交于 2019-11-30 04:09:13
Is it bad practice to change my getter method like version 2 in my class. Version 1: public String getMyValue(){ return this.myValue } Version 2: public String getMyValue(){ if(this.myValue == null || this.myValue.isEmpty()){ this.myValue = "N/A"; } return this.myValue; } I think it is actually quite a bad practice if your getter methods change the internal state of the object. To achieve the same I would suggest just returning the "N/A" . Generally speaking this internal field might be used in other places (internally) for which you don't need to use the getter method. So in the end, the call

Why is django's settings object a LazyObject?

我的梦境 提交于 2019-11-30 04:07:32
问题 Looking in django.conf I noticed that settings are implemented like this: class LazySettings(LazyObject): ... What is the rationale behind making settings objects lazy? 回答1: Check out this section of the Django coding style. The reason is explained in there (quoted below). In addition to performance, third-party modules can modify settings when they are imported. Accessing settings should be delayed to ensure this configuration happens first. Modules should not in general use settings stored

Streaming recursive descent of a directory in Haskell

房东的猫 提交于 2019-11-30 03:54:06
I am trying to do a recursive descent of a directory structure using Haskell. I would like to only retrieve the child directories and files as needed (lazily). I wrote the following code, but when I run it, the trace shows that all directories are visited before the first file: module Main where import Control.Monad ( forM, forM_, liftM ) import Debug.Trace ( trace ) import System.Directory ( doesDirectoryExist, getDirectoryContents ) import System.Environment ( getArgs ) import System.FilePath ( (</>) ) -- From Real World Haskell, p. 214 getRecursiveContents :: FilePath -> IO [FilePath]

Lazily evaluated indexed sequence type

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 03:12:14
问题 I need to build a sequence of objects that are loaded from an external resource. This loading being an expensive operation needs to be delayed until the time the objects are needed. After the collection is built, I need an indexed access to the contained objects. Does Scala standard library provide a collection suited to this use case? If not, what will be the best way to implement it? Edit: The indexed lookup should preferably be an O(1) operation. 回答1: Strange, Miles recently tweeted about

A way of achieving lazy evaluation in C++

爷,独闯天下 提交于 2019-11-30 02:05:23
So I was answering a question about lazy evaluation ( here , my answer is overkill for that case but the idea seems interesting) and it made me think about how lazy evaluation might be done in C++. I came up with a way but I wasn't sure of all the pitfalls in this. Are there other ways of achieving lazy evaluation? how might this be done? What are the pitfalls and this and other designs? Here is my idea: #include <iostream> #include <functional> #include <memory> #include <string> #define LAZY(E) lazy<decltype((E))>{[&](){ return E; }} template<class T> class lazy { private: typedef std:

Any way to define getters for lazy variables in Javascript arrays?

时光怂恿深爱的人放手 提交于 2019-11-30 01:37:23
问题 I'm trying to add elements to an array that are lazy-evaluated. This means that the value for them will not be calculated or known until they are accessed. This is like a previous question I asked but for objects. What I ended up doing for objects was Object.prototype.lazy = function(var_name, value_function) { this.__defineGetter__(var_name, function() { var saved_value = value_function(); this.__defineGetter__(var_name, function() { return saved_value; }); return saved_value; }); } lazy(

Trying to understand how linq/deferred execution works

孤人 提交于 2019-11-29 23:53:23
问题 I have the following methods, part of the logic for performing stratified k-fold crossvalidation. private static IEnumerable<IEnumerable<int>> GenerateFolds( IClassificationProblemData problemData, int numberOfFolds) { IRandom random = new MersenneTwister(); IEnumerable<double> values = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices); var valuesIndices = problemData.TrainingIndices.Zip(values, (i, v) => new { Index = i, Value = v }); IEnumerable

When are cache and persist executed (since they don't seem like actions)?

大憨熊 提交于 2019-11-29 20:01:04
问题 I am implementing a spark application, of which below is a sample snippet(Not the exact same code): val rdd1 = sc.textfile(HDFS_PATH) val rdd2 = rdd1.map(func) rdd2.persist(StorageLevel.MEMORY_AND_DISK) println(rdd2.count) On checking the performance of this code from the Spark Application Master UI, I see an entry for the count action, but not for the persist . The DAG for this count action also has a node for the 'map' transformation (line 2 of the above code). Is it safe to conclude that

Lazy Evaluation vs Macros

守給你的承諾、 提交于 2019-11-29 19:43:11
I'm used to lazy evaluation from Haskell, and find myself getting irritated with eager-by-default languages now that I've used lazy evaluation properly. This is actually quite damaging, as the other languages I use mainly make lazily evaluating stuff very awkward, normally involving the rolling out of custom iterators and so forth. So just by acquiring some knowledge, I've actually made myself less productive in my original languages. Sigh. But I hear that AST macros offer another clean way of doing the same thing. I've often heard statements like 'Lazy evaluation makes macros redundant' and