lazy-evaluation
Forward References – why does this code compile?
The body of a class or an object is the primary constructor. A constructor, like a method, is a sequence of statements that are executed in order — to do anything else, it would have to be a very different language. I’m pretty sure you wouldn’t like for Scala to execute the statements of your … Read more
How to convert lazy sequence to non-lazy in Clojure
doall is all you need. Just because the seq has type LazySeq doesn’t mean it has pending evaluation. Lazy seqs cache their results, so all you need to do is walk the lazy seq once (as doall does) in order to force it all, and thus render it non-lazy. seq does not force the entire … Read more
Java streams lazy vs fusion vs short-circuiting
As for fusion. Let’s imagine here’s a map operation: .map(x -> x.squash()) It’s stateless and it just transforms any input according to the specified algorithm (in our case squashes them). Now the filter operation: .filter(x -> x.getColor() != YELLOW) It’s also stateless and it just removes some elements (in our case yellow ones). Now let’s … Read more
caching the result from a [n async] factory method iff it doesn’t throw
Does this get anywhere near your requirements? The behaviour falls somewhere between ExecutionAndPublication and PublicationOnly. While the initializer is in-flight all calls to Value will be handed the same task (which is cached temporarily but could subsequently succeed or fail); if the initializer succeeds then that completed task is cached permanently; if the initializer fails … Read more
Non-Trivial Lazy Evaluation
Have you ever written an AI? Isn’t it annoying that you have to thread pruning information (e.g. maximum depth, the minimum cost of an adjacent branch, or other such information) through the tree traversal function? This means you have to write a new tree traversal every time you want to improve your AI. That’s dumb. … Read more
Lazy evaluation in Python
The object returned by range() (or xrange() in Python2.x) is known as a lazy iterable. Instead of storing the entire range, [0,1,2,..,9], in memory, the generator stores a definition for (i=0; i<10; i+=1) and computes the next value only when needed (AKA lazy-evaluation). Essentially, a generator allows you to return a list like structure, but … Read more
Lazy quantifier {,}? not working as I would expect
First and foremost, please do not think of greediness and laziness in regex as means of getting the longest/shortest match. “Greedy” and “lazy” terms only pertain to the rightmost character a pattern can match, it does not have any impact on the leftmost one. When you use a lazy quantifier, it will guarantee that the … Read more
What does the exclamation mark mean in a Haskell declaration?
It’s a strictness declaration. Basically, it means that it must be evaluated to what’s called “weak head normal form” when the data structure value is created. Let’s look at an example, so that we can see just what this means: data Foo = Foo Int Int !Int !(Maybe Int) f = Foo (2+2) (3+3) (4+4) … Read more