Questions tagged [lazy-evaluation]

Lazy evaluation refers to a variety of concepts that seek to avoid evaluation of an expression unless its value is needed, and to share the results of evaluation of an expression among all uses of its, so that no expression need be evaluated more than once.

Lazy evaluation refers to a variety of concepts that seek to avoid evaluation of an expression unless its value is needed, and to share the results of evaluation of an expression among all uses thereof, so that no expression need be evaluated more than once.

2555 questions
64
votes
6 answers

Accessing a non-static member via Lazy or any lambda expression

I have this code: public class MyClass { public int X { get; set; } public int Y { get; set; } private Lazy lazyGetSum = new Lazy(new Func(() => X + Y)); public int Sum{ get { return lazyGetSum.Value; } } } Gives me…
Saw
  • 6,199
  • 11
  • 53
  • 104
64
votes
3 answers

Why is seq bad?

Haskell has a magical function named seq, which takes an argument of any type and reduces it to Weak Head Normal Form (WHNF). I've read a couple of sources [not that I can remember who they were now...] which claim that "polymorphic seq is bad". In…
MathematicalOrchid
  • 61,854
  • 19
  • 123
  • 220
62
votes
2 answers

Explain a lazy evaluation quirk

I am reading Hadley Wickhams's book on Github, in particular this part on lazy evaluation. There he gives an example of consequences of lazy evaluation, in the part with add/adders functions. Let me quote that bit: This [lazy evaluation] is…
Maxim.K
  • 4,120
  • 1
  • 26
  • 43
56
votes
3 answers

Why and when to use lazy with Array in Swift?

[1, 2, 3, -1, -2].filter({ $0 > 0 }).count // => 3 [1, 2, 3, -1, -2].lazy.filter({ $0 > 0 }).count // => 3 What is the advantage of adding lazy to the second statement. As per my understanding, when lazy variable is used, memory is initialized to…
Deep Arora
  • 1,900
  • 2
  • 24
  • 40
55
votes
4 answers

What does {{{0}}} on string.Format do?

In the namespace MS.Internal, there is a class named NamedObject. It has a weird block of code: public override string ToString() { if (_name[0] != '{') { // lazily add {} around the name, to avoid allocating a string // until it's…
Mafii
  • 7,227
  • 1
  • 35
  • 55
50
votes
5 answers

Lazy Evaluation vs Macros

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…
Louis
  • 2,442
  • 1
  • 18
  • 15
50
votes
3 answers

How atomic are GHC's thunks?

How does GHC handle thunks that are accessed by multiple threads (either explicit threads, or the internal ones that evaluate sparks)? Can it happen that multiple threads evaluate the same thunk, duplicating work? Or, if thunks are synchronized,…
Petr
  • 62,528
  • 13
  • 153
  • 317
49
votes
11 answers

Kotlin: lateinit to val, or, alternatively, a var that can set once

Just curious: In Kotlin, I would love to get some val that can be initialized by lazy, but with a parameter. That's because I need something that's created very late in order to initialize it. Specifically, I wish I had: private lateinit val…
Maneki Neko
  • 1,177
  • 1
  • 14
  • 24
47
votes
7 answers

InvalidOperationException in my Lazy<> value factory

I have a class containing something like the following: public static class Config { private static Lazy _cfgSrc = new Lazy( () => { /* "ValueFactory" here... */ }, true); public static…
Jacob
  • 77,566
  • 24
  • 149
  • 228
46
votes
5 answers

Is everything in Haskell stored in thunks, even simple values?

What do the thunks for the following value/expression/function look like in the Haskell heap? val = 5 -- is `val` a pointer to a box containing 5? add x y = x + y result = add 2 val main = print $ result Would be nice to…
vis
  • 2,279
  • 1
  • 19
  • 27
45
votes
8 answers

Returning pure Django form errors in JSON

I have a Django form which I'm validating in a normal Django view. I'm trying to figure out how to extract the pure errors (without the HTML formatting). Below is the code I'm using at the moment. return json_response({ 'success' : False, …
Deniz Dogan
  • 25,711
  • 35
  • 110
  • 162
45
votes
3 answers

What is the connection between laziness and purity?

Laziness is what kept Haskell pure. If it had been strict, purity would soon go out the window. I fail to see the connection between the evaluation strategy of a language and its purity. Considering the reputation of the tweet's author I am most…
45
votes
5 answers

Are there something like Python generators in Ruby?

I am new to Ruby, is there a way to yield values from Ruby functions? If yes, how? If not, what are my options to write lazy code?
bodacydo
  • 75,521
  • 93
  • 229
  • 319
44
votes
6 answers

Directory.EnumerateFiles => UnauthorizedAccessException

There is a nice new method in .NET 4.0 for getting files in a directory in a streaming way via enumeration. The problem here is that if one wishes to enumerate all files one may not know in advance which files or folders are access protected and can…
43
votes
1 answer

Clojure lazy sequence usage

I'm having trouble understanding how one creates a lazy sequence in Clojure. The documentation for the macro isn't at all clear to me: Usage: (lazy-seq & body) Takes a body of expressions that returns an ISeq or nil, and yields a Seqable object…
Kricket
  • 4,049
  • 8
  • 33
  • 46