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
21
votes
5 answers

Call by need vs call by name

I didn't understand the diffrence between Call-by-name and Call-by-need. As I understood, Call-by-need method restores the answer returned. But how It helps us, and Is there any fundamental difference between the results ? For example, begin integer…
Adam Sh
  • 8,137
  • 22
  • 60
  • 75
21
votes
3 answers

What does seq actually do in Haskell?

From Real World Haskell I read It operates as follows: when a seq expression is evaluated, it forces its first argument to be evaluated, then returns its second argument. It doesn't actually do anything with the first argument: seq exists solely as…
21
votes
2 answers

Haskell Lazy ByteString + read/write progress function

I am learing Haskell Lazy IO. I am looking for an elegant way to copy a large file (8Gb) while printing copy progress to console. Consider the following simple program that copies a file silently. module Main where import System import qualified…
oshyshko
  • 2,008
  • 2
  • 21
  • 31
21
votes
3 answers

Why :sprint always prints a "_"?

Prelude> let a = 3 Prelude> :sprint a a = _ Prelude> let c = "ab" Prelude> :sprint c c = _ Why does it always print a _? I don't quite get the semantics of the :sprint command.
vik santata
  • 2,989
  • 8
  • 30
  • 52
21
votes
3 answers

How does Log4j 2.x implement lazy argument evaluation?

Given the Java argument evaluation mechanism, how does Log4j 2.x implement lazy evaluation when formatting the message with curly brackets "to avoid the cost of parameter construction" when log is disabled? e.g. logger.debug("Entry number: {} is…
Linuslabo
  • 1,568
  • 2
  • 24
  • 34
21
votes
18 answers

Truly declarative language?

Does anyone know of a truly declarative language? The behavior I'm looking for is kind of what Excel does, where I can define variables and formulas, and have the formula's result change when the input changes (without having set the answer again…
21
votes
3 answers

What is "Call By Name"?

I'm working on a homework assignment where we are asked to implement an evaluation strategy called "call by name" in a certain language that we developed (using Scheme). We were given an example in Scala, but I don't understand how "call by name"…
forellana
  • 937
  • 1
  • 8
  • 19
21
votes
3 answers

Lazy evaluation in Ruby

I have a situation for Ruby, where an object is possibly necessary to be created, but it is not sure. And as the creation of the object might be costly I am not too eager creating it. I think this is a clear case for lazy loading. How can I define…
fifigyuri
  • 5,771
  • 8
  • 30
  • 50
21
votes
2 answers

How to create lazy_evaluated dataframe columns in Pandas

A lot of times, I have a big dataframe df to hold the basic data, and need to create many more columns to hold the derivative data calculated by basic data columns. I can do that in Pandas like: df['derivative_col1'] = df['basic_col1'] +…
bigbug
  • 55,954
  • 42
  • 77
  • 96
21
votes
5 answers

Explain this chunk of haskell code that outputs a stream of primes

I have trouble understanding this chunk of code: let sieve (p:xs) = p : sieve (filter (\ x -> x `mod` p /= 0) xs) in sieve [2 .. ] Can someone break it down for me? I understand there is recursion in it, but thats the problem I can't understand…
nubela
  • 1
  • 24
  • 75
  • 123
21
votes
3 answers

Is it possible to match with decomposed sequences in F#?

I seem to remember an older version of F# allowing structural decomposition when matching sequences just like lists. Is there a way to use the list syntax while keeping the sequence lazy? I'm hoping to avoid a lot of calls to Seq.head and Seq.skip…
Ball
  • 2,591
  • 3
  • 19
  • 26
21
votes
1 answer

Any difference between Lazy evaluation and Short-circuit evaluation?

From Wikipedia: Lazy evaluation is: In programming language theory, lazy evaluation or call-by-need is an evaluation strategy which delays the evaluation of an expression until its value is needed Short-circuit evaluation is: Short-circuit…
Afshin Mehrabani
  • 33,262
  • 29
  • 136
  • 201
20
votes
2 answers

Pattern matching and infinite streams

So, I'm working to teach myself Scala, and one of the things I've been playing with is the Stream class. I tried to use a naïve translation of the classic Haskell version of Dijkstra's solution to the Hamming number problem: object LazyHammingBad { …
Pillsy
  • 9,781
  • 1
  • 43
  • 70
20
votes
4 answers

Explanation of "Lose your head" in lazy sequences

In Clojure programming language, why this code passes with flying colors? (let [r (range 1e9)] [(first r) (last r)]) While this one fails: (let [r (range 1e9)] [(last r) (first r)]) I know it is about "Losing your head" advice but would you please…
Chiron
  • 20,081
  • 17
  • 81
  • 133
20
votes
2 answers

R: passing expression to an inner function

Further delving into the mysteries of R evaluation...This is closely related to my previous question ( How to write an R function that evaluates an expression within a data-frame ). Let's say I want to write a function topfn that takes a data-frame…
Prasad Chalasani
  • 19,912
  • 7
  • 51
  • 73