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
33
votes
2 answers

How to force evaluation in Haskell?

I am relatively new to Haskell and I am trying to learn how different actions can be executed in sequence using the do notation. In particular, I am writing a program to benchmark an algorithm (a function) foo :: [String] -> [String] To this…
Giorgio
  • 5,023
  • 6
  • 41
  • 71
32
votes
1 answer

How do laziness and parallelism coexist in Haskell?

People argue that Haskell has an advantage in parallelism since it has immutable data structures. But Haskell is also lazy. It means data actually can be mutated from thunk to evaluated result. So it seems laziness can harm the advantage of…
32
votes
3 answers

What is the relationship between unboxed types and strictness?

Unboxed types, like Int#, and strict functions, like f (!x) = ..., are something different, but I see conceptual similarity - they disallow thunks/laziness in some way. If Haskell was a strict language like Ocaml, every function would be strict and…
sdcvvc
  • 25,343
  • 4
  • 66
  • 102
32
votes
2 answers

How much memory does a thunk use?

Let's say I have a very large number (millions/billions+) of these simple Foo data structures: data Foo = Foo { a :: {-# UNPACK #-}!Int , b :: Int } With so many of these floating around, it becomes necessary to think about how much…
Mike Izbicki
  • 6,286
  • 1
  • 23
  • 53
31
votes
2 answers

Forward References - why does this code compile?

Consider this snippet: object A { val b = c val c = "foo" } println( A.b ) // prints "null" As part of a larger program, this would lead to a failure at runtime. The compiler apparently permits the forward reference from 'b' to…
Gregor Scheidt
  • 3,952
  • 4
  • 24
  • 28
31
votes
6 answers

Execute dplyr operation only if column exists

Drawing on the discussion on conditional dplyr evaluation I would like conditionally execute a step in pipeline depending on whether the reference column exists in the passed data frame. Example The results generated by 1) and 2) should be…
Konrad
  • 17,740
  • 16
  • 106
  • 167
31
votes
8 answers

Return first non-null value

I have a number of functions: String first(){} String second(){} ... String default(){} Each can return a null value, except the default. each function can take different parameters. For example, first could take no arguments, second could take in…
lorenzocastillo
  • 985
  • 3
  • 13
  • 24
31
votes
9 answers

How can I get a lazy array in Ruby?

How can I get a lazy array in Ruby? In Haskell, I can talk about [1..], which is an infinite list, lazily generated as needed. I can also do things like iterate (+2) 0, which applies whatever function I give it to generate a lazy list. In this…
carlfilips
  • 2,574
  • 3
  • 21
  • 27
31
votes
3 answers

Non-standard evaluation (NSE) in dplyr's filter_ & pulling data from MySQL

I'd like to pull some data from a sql server with a dynamic filter. I'm using the great R package dplyr in the following way: #Create the filter filter_criteria = ~ column1 %in% some_vector #Connect to the database connection <- src_mysql(dbname <-…
Lorenzo Rossi
  • 1,481
  • 1
  • 9
  • 16
31
votes
1 answer

Idris eager evaluation

In Haskell, I might implement if like this: if' True x y = x if' False x y = y spin 0 = () spin n = spin (n - 1) This behaves how I expect: haskell> if' True (spin 1000000) () -- takes a moment haskell> if' False (spin 1000000) () --…
Snowball
  • 11,102
  • 3
  • 34
  • 51
31
votes
1 answer

What is spine strictness

In Haskell, the term spine strictness is often mentioned in relation to lazy evaluation. Though I have a vague understanding of that it means, it would be nice to have a more concrete explanation about: What is the spine of a data structure What…
Markus1189
  • 2,829
  • 1
  • 23
  • 32
31
votes
5 answers

Python class member lazy initialization

I would like to know what is the python way of initializing a class member but only when accessing it, if accessed. I tried the code below and it is working but is there something simpler than that? class MyClass(object): _MY_DATA = None …
user1919510
30
votes
3 answers

How Are Lazy Sequences Implemented in Clojure?

I like Clojure. One thing that bothers me about the language is that I don't know how lazy sequences are implemented, or how they work. I know that lazy sequences only evaluate the items in the sequence that are asked for. How does it do this? What…
mudgen
  • 7,213
  • 11
  • 46
  • 46
30
votes
2 answers

Why GADT/existential data constructors cannot be used in lazy patterns?

Today, I got a compiler error when trying to use a lazy pattern when matching on an existential GADT constructor: An existential or GADT data constructor cannot be used inside a lazy (~) pattern Why is that limitation? What "bad" stuff could…
Petr
  • 62,528
  • 13
  • 153
  • 317
29
votes
4 answers

nhibernate lazy load options

What is the difference between lazy="true" and lazy="proxy" in nhibernate?
Ajay