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
92
votes
8 answers

What are Haskell's strictness points?

We all know (or should know) that Haskell is lazy by default. Nothing is evaluated until it must be evaluated. So when must something be evaluated? There are points where Haskell must be strict. I call these "strictness points", although this…
Dan Burton
  • 53,238
  • 27
  • 117
  • 198
92
votes
6 answers

What's so bad about Lazy I/O?

I've generally heard that production code should avoid using Lazy I/O. My question is, why? Is it ever OK to use Lazy I/O outside of just toying around? And what makes the alternatives (e.g. enumerators) better?
Dan Burton
  • 53,238
  • 27
  • 117
  • 198
86
votes
7 answers

Does Python evaluate if's conditions lazily?

For example, if I have the following statement: if( foo1 or foo2) ... ... if foo1 is true, will python check the condition of foo2?
ogama8
  • 999
  • 1
  • 6
  • 6
84
votes
12 answers

Lazy evaluation in C++

C++ does not have native support for lazy evaluation (as Haskell does). I'm wondering if it is possible to implement lazy evaluation in C++ in a reasonable manner. If yes, how would you do it? EDIT: I like Konrad Rudolph's answer. I'm wondering if…
user51568
  • 2,623
  • 2
  • 20
  • 23
84
votes
6 answers

withFilter instead of filter

Is it always more performant to use withFilter instead of filter, when afterwards applying functions like map, flatmap etc.? Why are only map, flatmap and foreach supported? (Expected functions like forall/exists as well)
Kigyo
  • 5,668
  • 1
  • 20
  • 24
76
votes
14 answers

hibernate: LazyInitializationException: could not initialize proxy

Here's one that has me perplexed. I'm trying to implement a basic Hibernate DAO structure, but am having a problem. Here's the essential code: int startingCount = sfdao.count(); sfdao.create( sf ); SecurityFiling sf2 = sfdao.read( sf.getId()…
Piko
  • 4,132
  • 2
  • 21
  • 13
74
votes
8 answers

`def` vs `val` vs `lazy val` evaluation in Scala

Am I right understanding that def is evaluated every time it gets accessed lazy val is evaluated once it gets accessed val is evaluated once it gets into the execution scope?
Ivan
  • 63,011
  • 101
  • 250
  • 382
74
votes
5 answers

Clojure: rest vs. next

I'm having a hard time understanding the difference between rest and next in Clojure. The official site's page on laziness indicates that the preference should probably be to use rest, but it doesn't really explain clearly the difference between the…
Daniel Yankowsky
  • 6,956
  • 1
  • 35
  • 39
74
votes
6 answers

Do the &= and |= operators for bool short-circuit?

When writing code like this in C++: bool allTrue = true; allTrue = allTrue && check_foo(); allTrue = allTrue && check_bar(); check_bar() will not be evaluated if check_foo() returned false. This is called short-circuiting or short-circuit…
73
votes
4 answers

Understanding a recursively defined list (fibs in terms of zipWith)

I'm learning Haskell, and came across the following code: fibs = 0 : 1 : zipWith (+) fibs (tail fibs) which I'm having a bit of trouble parsing, in terms of how it works. It's very neat, I understand that nothing more is needed, but I'd like to…
Frank
  • 4,341
  • 8
  • 41
  • 57
72
votes
14 answers

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

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()){ …
someone
  • 6,577
  • 7
  • 37
  • 60
72
votes
7 answers

Lazy Evaluation and Time Complexity

I was looking around stackoverflow Non-Trivial Lazy Evaluation, which led me to Keegan McAllister's presentation: Why learn Haskell. In slide 8, he shows the minimum function, defined as: minimum = head . sort and states that its complexity is…
leco
  • 1,989
  • 16
  • 30
70
votes
6 answers

How does non-strict and lazy differ?

I often read that lazy is not the same as non-strict but I find it hard to understand the difference. They seem to be used interchangeably but I understand that they have different meanings. I would appreciate some help understanding the…
user295190
67
votes
3 answers

Lazy evaluation in Python

What is lazy evaluation in Python? One website said : In Python 3.x the range() function returns a special range object which computes elements of the list on demand (lazy or deferred evaluation): >>> r = range(10) >>> print(r) range(0, 10) >>>…
Vipul
  • 4,038
  • 9
  • 32
  • 56
65
votes
7 answers

Pass arguments to dplyr functions

I want to parameterise the following computation using dplyr that finds which values of Sepal.Length are associated with more than one value of Sepal.Width: library(dplyr) iris %>% group_by(Sepal.Length) %>% …
asnr
  • 1,692
  • 1
  • 14
  • 17