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
25
votes
3 answers

Is Haskell's laziness an elegant alternative to Python's generators?

In a programming exercise, it was first asked to program the factorial function and then calculate the sum: 1! + 2! + 3! +... n! in O(n) multiplications (so we can't use the factorial directly). I am not searching the solution to this specific…
Sebastien
  • 682
  • 6
  • 13
25
votes
6 answers

LINQ's deferred execution, but how?

This must be something really simple. But i'm going to ask it anyway, because i think that others will also struggle with it. Why does following simple LINQ query is not executed always with the new variable-value instead of always using the…
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
24
votes
10 answers

String.format with lazy evaluation

I need something similar to String.format(...) method, but with lazy evaluation. This lazyFormat method should return some object whose toString() method would then evaluate the format pattern. I suspect that somebody has already done this. Is this…
Juha Syrjälä
  • 33,425
  • 31
  • 131
  • 183
24
votes
6 answers

Lazy evaluation for logging in Java 8

When you have values than are expensive to compute, a common pattern you see in logging frameworks is if (log.isDebugEnabled()) { String value = expensiveComputation(); log.debug("value: {}", value); } Since Java 8 added lambdas, it'd be…
David Ehrmann
  • 7,366
  • 2
  • 31
  • 40
24
votes
1 answer

Haskell: How "cache" friendly is Lazy Eval / call by need

I have been studying Haskell in my spare time for a couple of months now. I'm wondering how Haskell performs on the current stock hardware, in regards to the memory sub-system (L1, L2, L3 cache). Can someone please point me to any report/study on…
user3169543
  • 1,499
  • 1
  • 10
  • 16
24
votes
1 answer

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

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)?
ron
  • 9,262
  • 4
  • 40
  • 73
23
votes
5 answers

Convert a Lazy ByteString to a strict ByteString

I have a function that takes a lazy ByteString, that I wish to have return lists of strict ByteStrings (the laziness should be transferred to the list type of the output). import qualified Data.ByteString as B import qualified Data.ByteString.Lazy…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
23
votes
11 answers

Some solid OOP criticism?

I want to ask you to provide me with some articles (maybe books), which you possibly have found very convincing criticising the OOP methodology. I have read some in the WWW on this topic and I didn't really find a 'definitive demotivator'. It's not…
Bubba88
  • 1,910
  • 20
  • 44
23
votes
4 answers

What are the advantages of Lazy Evaluation?

What advantages are there to Lazy Evaluation as opposed to Eager Evaluation? What performance overhead is there? Is Lazy Evaluation going to be slower or faster? Why(or does it depend on implementation?)? How does lazy evaluation actually work in…
Earlz
  • 62,085
  • 98
  • 303
  • 499
22
votes
3 answers

How pure and lazy can Scala be?

This is just one of those "I was wondering..." questions. Scala has immutable data structures and (optional) lazy vals etc. How close can a Scala program be to one that is fully pure (in a functional programming sense) and fully lazy (or as Ingo…
22
votes
4 answers

Is performance of partial or curried functions well defined in Haskell?

In the following code: ismaxl :: (Ord a) => [a] -> a -> Bool ismaxl l x = x == maxel where maxel = maximum l main = do let mylist = [1, 2, 3, 5] let ismax = ismaxl mylist --Is each call O(1)? Does each call remember maxel? let…
22
votes
2 answers

How many ways are there to describe the Fibonacci sequence in Perl 6?

I've been looking at the various ways of constructing lazy lists in Perl 6 and I would like to collect all of the concise ways of describing the Fibonacci sequence. I will start this off with the three from masak's journal: my @fibs := (0, 1, -> $a,…
Eric Strom
  • 39,821
  • 2
  • 80
  • 152
22
votes
2 answers

Lazy var giving 'Instance member can not be used on type' error

I had this error several times now and I resorted to different workarounds, but I'm really curious why it happens. Basic scenario is following: class SomeClass { var coreDataStuff = CoreDataStuff! lazy var somethingElse =…
66o
  • 756
  • 5
  • 10
22
votes
1 answer

Lazy evaluation in R – is assign affected?

I read this basic question on renaming objects and @Shane 's answer to it, pointing me to lazy evaluation. Now I wonder if assign is evaluated lazily, too. Just like here: assign("someNewName",someOldObject) rm(someOldObject) The reason why I…
Matt Bannert
  • 27,631
  • 38
  • 141
  • 207
21
votes
4 answers

Evaluation strategy

How should one reason about function evaluation in examples like the following in Haskell: let f x = ... x = ... in map (g (f x)) xs In GHC, sometimes (f x) is evaluated only once, and sometimes once for each element in xs, depending on what…
Grzegorz Chrupała
  • 3,053
  • 17
  • 24