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
42
votes
1 answer

Understanding the different behavior of thunks when GHCi let bindings are involved

I've been playing with some examples from Simon Marlow's book about parallel and concurrent programming in Haskell and stumbled across an interesting behavior that I don't really understand. This is really about me trying to understand some of the…
raichoo
  • 2,557
  • 21
  • 28
42
votes
4 answers

Lazy evaluation of terms in an infinite list in Haskell

I am curious about the runtime performance of an infinite list like the one below: fibs = 1 : 1 : zipWith (+) fibs (tail fibs) This will create an infinite list of the fibonacci sequence. My question is that if I do the following: takeWhile (<5)…
akg
  • 523
  • 4
  • 5
41
votes
1 answer

How do experienced Haskell developers approach laziness at *design* time?

I'm an intermediate Haskell programmer with tons of experience in strict FP and non-FP languages. Most of my Haskell code analyzes moderately large datasets (10^6..10^9 things), so laziness is always lurking. I have a reasonably good understanding…
41
votes
4 answers

How to implement a lazy setdefault?

One minor annoyance with dict.setdefault is that it always evaluates its second argument (when given, of course), even when the first the first argument is already a key in the dictionary. For example: import random def noisy_default(): ret =…
kjo
  • 33,683
  • 52
  • 148
  • 265
40
votes
2 answers

Scala's lazy arguments: How do they work?

In the file Parsers.scala (Scala 2.9.1) from the parser combinators library I seem to have come across a lesser known Scala feature called "lazy arguments". Here's an example: def ~ [U](q: => Parser[U]): Parser[~[T, U]] = { lazy val p = q // lazy…
python dude
  • 7,980
  • 11
  • 40
  • 53
40
votes
4 answers

Passing a variable name to a function in R

I've noticed that quite a few packages allow you to pass symbol names that may not even be valid in the context where the function is called. I'm wondering how this works and how I can use it in my own code? Here is an example with ggplot2: a <-…
static_rtti
  • 53,760
  • 47
  • 136
  • 192
39
votes
2 answers

Recursive function causing a stack overflow

I am trying to write a simple sieve function to calculate prime numbers in clojure. I've seen this question about writing an efficient sieve function, but I am not to that point yet. Right now I am just trying to write a very simple (and slow)…
dbyrne
  • 59,111
  • 13
  • 86
  • 103
36
votes
1 answer

Swift: implement a protocol variable as a lazy var?

It seems that it's not possible to implement a variable that's required by a protocol, with a lazy variable. For example: protocol Foo { var foo: String { get } } struct Bar: Foo { lazy var foo: String = "Hello World" } Compiler complains that…
Kevin Renskers
  • 5,156
  • 4
  • 47
  • 95
35
votes
3 answers

Haskell: how to detect "lazy memory leaks"

After few hours of debugging, I realized that a very simple toy example was not efficient due to a missing ! in an expression return $ 1 + x (thanks duplode!... but how come ghc does not optimize that??). I also realized it because I was comparing…
tobiasBora
  • 1,542
  • 14
  • 23
35
votes
4 answers

Pass parameters to constructor, when initializing a lazy instance

public class myClass { public myClass(String InstanceName) { Name = InstanceName; } public String Name { get; set; } } // Now using myClass lazily I have: Lazy myLazy; Console.WriteLine(myLazy.Value.Name); My question…
Xaqron
  • 29,931
  • 42
  • 140
  • 205
35
votes
9 answers

Does Java have lazy evaluation?

I know that Java has smart/lazy evaluation in this case: public boolean isTrue() { boolean a = false; boolean b = true; return b || (a && b); // (a && b) is not evaluated since b is true } But what about: public boolean isTrue() { …
m0skit0
  • 25,268
  • 11
  • 79
  • 127
34
votes
7 answers

How to reduce memory usage in a Haskell app?

I am new to functional programming, and now learn Haskell. As an exercise I decided to implement the explicit Euler method for 1D linear diffusion equation. While the code below works correctly, I am not happy about its performance. In fact, I am…
34
votes
1 answer

Do Immutable.js or Lazy.js perform short-cut fusion?

First, let me define what is short-cut fusion for those of you who don't know. Consider the following array transformation in JavaScript: var a = [1,2,3,4,5].map(square).map(increment); console.log(a); function square(x) { return x *…
Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
33
votes
3 answers

Non-Trivial Lazy Evaluation

I'm currently digesting the nice presentation Why learn Haskell? by Keegan McAllister. There he uses the snippet minimum = head . sort as an illustration of Haskell's lazy evaluation by stating that minimum has time-complexity O(n) in Haskell.…
Nordlöw
  • 11,838
  • 10
  • 52
  • 99
33
votes
3 answers

What is the opposite of lazy loading?

Is there a common term / catch-phrase for the opposite of lazy loading?
hakre
  • 193,403
  • 52
  • 435
  • 836