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

Lazy loading vs thunking?

Recently I've heard definition to both of them and for me they sound identical. Is a thunk the same thing as Lazy load? If not what is the difference between those two definitions?
Filip Bartuzi
  • 5,711
  • 7
  • 54
  • 102
2
votes
1 answer

Using move semantics and perfect forwarding to implement a 'lazy' operator+

I am trying to write a friend T operator+( lhs, rhs){ }; Now, I would like to avoid construction of temporaries when possible. For example: If both lhs and rhs are const T& the operator+ should create a temp copy-constructing from lhs, then…
Kae
  • 279
  • 2
  • 10
2
votes
1 answer

Initialization on demand for multiple static variables without static nested classes?

I want to have a class with multiple static variables that will only be initialized on demand. public class Messages { public static final String message1 = init1(); public static final String message2 = init2(); } So when somewhere in the…
2
votes
1 answer

Haskell Peano Numbers and Laziness in Multiplication

I started learning Haskell recently and in my class right now, we have constructed a Peano number class and instanced it in the Num typeclass. During lecture, my professor claimed that depending on whether you viewed the successor function as S x =…
2
votes
3 answers

C# - Lazy loading with cache

I have a repository decorator. This decorator is responsible of the caching of the decorated repository. In most of my functions of this decorator I just return the result for the cache if exist or call the method on the decorated repository and…
Kapoue
  • 847
  • 2
  • 11
  • 15
2
votes
2 answers

Understanding Haskell lazy evaluation

Forgive my stupid question, I'm new to Haskell. I tried in Haskell the following: sum [fib n| n <- [1..], (even (fib n) && fib n < 4000000)] which takes infinite time. If I leave out n <- [1..], the solution comes at once. I thought it shouldn't…
2
votes
3 answers

Does this function make use of haskell's lazy evaluation

I wrote the following function to decide if a number is prime or not. isPrime :: Int -> Bool isPrime n = and (map (\x -> (n `mod` x > 0))[2..(intSquareRoot n)]) intSquareRoot :: Int -> Int intSquareRoot n = intSq n where intSq x | x*x >…
Slugger
  • 665
  • 1
  • 5
  • 17
2
votes
2 answers

Expressing recursion in Haskell - Prime numbers sequence

I need to express the sequence of prime numbers. (struggling with ex 3 in project Euler). I have happened to this recursive definition: is_not_dividable_by :: (Integral a) => a -> a -> Bool is_not_dividable_by x y = x `rem` y /= 0 accumulate_and ::…
Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
2
votes
5 answers

Can Lazy<> and Task<> be combined to defer database lookup?

I've coded a simplified version of my problem below. So I've got this service and in the method DoSomething - I want to do a synchronous check ( IsTrue() method ) followed by an async call ( database lookup ). I only want to do the database lookup…
Jochen van Wylick
  • 5,303
  • 4
  • 42
  • 64
2
votes
1 answer

Exchange Time for Memory in Python

Challenge here is in evaluating multiple large files. What coding will instruct Python to "load" a limited number of files into memory, process them, garbage collect and then load the next set? def main(directory): """ Create AudioAnalysis…
MikeiLL
  • 6,282
  • 5
  • 37
  • 68
2
votes
1 answer

How to initialize a Lazy list?

I've read about Lazy Initialization but practically I don't understand how to initialize the objects inside. This function returns a LazyList which is a kind of custom implementation of a Lazy type Public Function GetMethods(ByVal Assembly As…
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
2
votes
1 answer

Memory exploding upon writing a lazy bytestring to file in ghci

The following program does not explode when the executable (compiled via ghc -O0 Explode.hs) is run, but does explode when run in ghci (via either ghci Explode.hs or ghci -fobject-code Explode.hs) : --Explode.hs --Does not explode with : ghc -O0…
artella
  • 5,068
  • 4
  • 27
  • 35
2
votes
0 answers

The reverse state monad transformer in OCaml

How would you implement the reverse state monad transformer in OCaml? This is a follow-up of the question The reverse state monad in OCaml: I was able to rediscover @Lambdageek's answer by myself after understanding the tricks he or she used…
Bob
  • 1,713
  • 10
  • 23
2
votes
2 answers

def vs lazy val in case class

I have a DAO object which I defined as a case class. case class StudentDAO(id: Int) { def getGPA: Double = // Expensive database lookup goes here def getRank: Int = // Another expensive database operation and computation goes here def…
Sudheer Aedama
  • 2,116
  • 2
  • 21
  • 39
2
votes
4 answers

Termination of mutually recursive functions using the Maybe monad

Below is are two examples of mutually recursive function pairs. The first example terminates and produces the expected result. The second example is similar, except it uses the Maybe monad. fun1' does not terminate when called. fun1 = 1 + fun2 fun2…
knick
  • 941
  • 8
  • 17