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
28
votes
4 answers

To what extent is Haskell lazy?

I need some clarification about laziness with Haskell. If I have this function: myFunction arg | arg == 1 = a | arg == 2 = a*b | arg == 3 = b+c | otherwise = (a+b)*c where a = ... b = ... c = ... …
JeanJouX
  • 2,555
  • 1
  • 25
  • 37
28
votes
10 answers

AppRegistryNotReady: The translation infrastructure cannot be initialized

When I try to access to my app, I'm getting the following error below: AppRegistryNotReady: The translation infrastructure cannot be initialized before the apps registry is ready. Check that you don't make non-lazy gettext calls at import…
Alexandre Tea
  • 313
  • 1
  • 3
  • 7
28
votes
5 answers

How is debugging achieved in a lazy functional programming language?

I'd like to know how debugging is achieved in a lazy functional language. Can you use breakpoints, print statements and traditional techniques? Is this even a good idea? It is my understanding that pure functional programming does not allow…
CaptainCasey
  • 1,361
  • 1
  • 14
  • 21
27
votes
2 answers

How does seq force functions?

Background This question arises from a challenge Brent Yorgey posed at OPLSS: write a function f :: (Int -> Int) -> Bool that distinguishes f undefined from f (\x -> undefined). All of our answers either used seq or something like bang patterns that…
acfoltzer
  • 5,588
  • 31
  • 48
27
votes
3 answers

PHP Lazy Boolean Evaluation

I have a conditional statement thus: if($boolean && expensiveOperation()){ ...} Does PHP have lazy boolean evaluation, i.e. will it check $boolean and if it is false not bother performing the expensive operation? If so, what order should I put my…
fredley
  • 32,953
  • 42
  • 145
  • 236
27
votes
5 answers

Lazy List of Prime Numbers

How would one implement a list of prime numbers in Haskell so that they could be retrieved lazily? I am new to Haskell, and would like to learn about practical uses of the lazy evaluation functionality.
Mantas Vidutis
  • 16,376
  • 20
  • 76
  • 92
27
votes
4 answers

Scala case class prohibits call-by-name parameters?

Scenario: I want to implement an infinite list: abstract class MyList[+T] case object MyNil extends MyList[Nothing] case class MyNode[T](h:T,t: => MyList[T]) extends MyList[T] //error: `val' parameters may not be call-by-name Problem: The error is…
27
votes
8 answers

Setup dictionary lazily

Let's say I have this dictionary in python, defined at the module level (mysettings.py): settings = { 'expensive1' : expensive_to_compute(1), 'expensive2' : expensive_to_compute(2), ... } I would like those values to be computed when…
blueFast
  • 41,341
  • 63
  • 198
  • 344
26
votes
2 answers

Algorithms that don't terminate in a lazy language

According to http://www.reddit.com/r/programming/comments/gwqa2/the_real_point_of_laziness/c1rslxk Some algorithms don't terminate in an eager language, that do in a lazy one, and (a mild shocker for me to find,) vice-versa. The former is of…
rwallace
  • 31,405
  • 40
  • 123
  • 242
26
votes
5 answers

Spark Transformation - Why is it lazy and what is the advantage?

Spark Transformations are lazily evaluated - when we call the action it executes all the transformations based on lineage graph. What is the advantage of having the Transformations Lazily evaluated? Will it improve the performance and less amount of…
Shankar
  • 8,529
  • 26
  • 90
  • 159
26
votes
3 answers

Lazy evaluation for list generating functions?

I'm currently reading Programming in Haskell, by Graham Hutton. In p.40, a toy primality test is presented: factors :: Int -> [Int] factors n = [x | x <- [1..n], n `mod` x == 0] prime :: Int -> Bool prime n = factors n == [1,n] The author then…
Mister Smith
  • 27,417
  • 21
  • 110
  • 193
26
votes
3 answers

Can a thunk be duplicated to improve memory performance?

One of my struggles with lazy evaluation in Haskell is the difficulty of reasoning about memory usage. I think the ability to duplicate a thunk would make this much easier for me. Here's an example. Let's create a really big list: let xs =…
Mike Izbicki
  • 6,286
  • 1
  • 23
  • 53
25
votes
6 answers

Lazy lists in Prolog?

Is it possible to have lazy lists in Prolog? Something like the following: ones([1 | Y]) :- ones(Y). Although this obviously doesn't work as it's written.
Peteris
  • 3,548
  • 4
  • 28
  • 44
25
votes
4 answers

Lazy choices in Django form

I have a Django my_forms.py like this: class CarSearchForm(forms.Form): # lots of fields like this bodystyle = forms.ChoiceField(choices=bodystyle_choices()) Each choice is e.g. ("Saloon", "Saloon (15 cars)"). So the choices are…
Tom Viner
  • 6,655
  • 7
  • 39
  • 40
25
votes
1 answer

findFirst() for Java streams, but for n elements?

I want to collect the first n elements from a stream, without iterating through the entire thing. Is there a standard method that does this? Ala MyList.stream() .filter(x -> predicate(x)) .findFirstN(100) would return a collection of up to 100…
Andrew
  • 6,295
  • 11
  • 56
  • 95