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
749
votes
30 answers

Read a file one line at a time in node.js?

I am trying to read a large file one line at a time. I found a question on Quora that dealt with the subject but I'm missing some connections to make the whole thing fit together. var Lazy=require("lazy"); new Lazy(process.stdin) .lines …
Alex C
  • 16,624
  • 18
  • 66
  • 98
386
votes
7 answers

When should I use Lazy?

I found this article about Lazy: Laziness in C# 4.0 – Lazy What is the best practice to have best performance using Lazy objects? Can someone point me to a practical use in a real application? In other words, when should I use it?
danyolgiax
  • 12,798
  • 10
  • 65
  • 116
305
votes
3 answers

What does the exclamation mark mean in a Haskell declaration?

I came across the following definition as I try to learn Haskell using a real project to drive it. I don't understand what the exclamation mark in front of each argument means and my books didn't seem to mention it. data MidiMessage = MidiMessage…
David
  • 5,991
  • 5
  • 33
  • 39
272
votes
7 answers

What does a lazy val do?

I noticed that Scala provide lazy vals. But I don't get what they do. scala> val x = 15 x: Int = 15 scala> lazy val y = 13 y: Int = scala> x res0: Int = 15 scala> y res1: Int = 13 The REPL shows that y is a lazy val, but how is it…
kiritsuku
  • 52,967
  • 18
  • 114
  • 136
264
votes
8 answers

Extract a dplyr tbl column as a vector

Is there a more succinct way to get one column of a dplyr tbl as a vector, from a tbl with database back-end (i.e. the data frame/table can't be subset directly)? require(dplyr) db <- src_sqlite(tempfile(), create = TRUE) iris2 <- copy_to(db,…
nacnudus
  • 6,328
  • 5
  • 33
  • 47
172
votes
4 answers

"gettext()" vs "gettext_lazy()" in Django

I have a question about using ugettext and gettext_lazy() for translations. I learned that in models I should use gettext_lazy(), while in views ugettext. But are there any other places, where I should use gettext_lazy() too? What about form…
Dzejkob
  • 2,302
  • 2
  • 15
  • 20
172
votes
14 answers

Zipping streams using JDK8 with lambda (java.util.stream.Streams.zip)

In JDK 8 with lambda b93 there was a class java.util.stream.Streams.zip in b93 which could be used to zip streams (this is illustrated in the tutorial Exploring Java8 Lambdas. Part 1 by Dhananjay Nene). This function : Creates a lazy and sequential…
artella
  • 5,068
  • 4
  • 27
  • 35
171
votes
6 answers

What's the (hidden) cost of Scala's lazy val?

One handy feature of Scala is lazy val, where the evaluation of a val is delayed until it's necessary (at first access). Of course, a lazy val must have some overhead - somewhere Scala must keep track of whether the value has already been evaluated…
Jesper
  • 202,709
  • 46
  • 318
  • 350
161
votes
10 answers

How do lexical closures work?

While I was investigating a problem I had with lexical closures in Javascript code, I came along this problem in Python: flist = [] for i in xrange(3): def func(x): return x * i flist.append(func) for f in flist: print f(2) Note that…
Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
134
votes
4 answers

foldl versus foldr behavior with infinite lists

The code for the myAny function in this question uses foldr. It stops processing an infinite list when the predicate is satisfied. I rewrote it using foldl: myAny :: (a -> Bool) -> [a] -> Bool myAny p list = foldl step False list where …
titaniumdecoy
  • 18,900
  • 17
  • 96
  • 133
131
votes
22 answers

Why is lazy evaluation useful?

I have long been wondering why lazy evaluation is useful. I have yet to have anyone explain to me in a way that makes sense; mostly it ends up boiling down to "trust me". Note: I do not mean memoization.
Joel McCracken
  • 2,275
  • 3
  • 17
  • 11
123
votes
4 answers

How is this fibonacci-function memoized?

By what mechanism is this fibonacci-function memoized? fib = (map fib' [0..] !!) where fib' 1 = 1 fib' 2 = 1 …
bjornars
  • 1,506
  • 2
  • 10
  • 13
105
votes
5 answers

How to convert lazy sequence to non-lazy in Clojure

I tried the following in Clojure, expecting to have the class of a non-lazy sequence returned: (.getClass (doall (take 3 (repeatedly rand)))) However, this still returns clojure.lang.LazySeq. My guess is that doall does evaluate the entire…
Tim Clemons
  • 6,231
  • 4
  • 25
  • 23
104
votes
4 answers

Does Haskell have tail-recursive optimization?

I discovered the "time" command in unix today and thought I'd use it to check the difference in runtimes between tail-recursive and normal recursive functions in Haskell. I wrote the following functions: --tail recursive fac :: (Integral a) => a ->…
93
votes
1 answer

Angular lazy one-time binding for expressions

AngularJS has a new feature since the version 1.3.0-beta.10: the "lazy one-time binding". Simple expressions can be prefixed with ::, telling angular to stop watching after the expression was first evaluated. The common example given is something…
seldary
  • 6,186
  • 4
  • 40
  • 55
1
2 3
99 100