Lazy evaluation
Lazy evaluation is a common feature in functional programming languages and provides an way to delay the evaluation of an expression until the result is actually needed.
By default, Sidef is an eagerly evaluated language, but it supports a form of lazy evaluation, provided by the method Object.lazy(), almost in the same way as Ruby does:
1
say (^Inf -> lazy.grep{ .is_prime }.first(10)) # first 10 primes
Copied!
The .lazy method returns a Lazy object, which behaves almost like an Array, except that it executes the methods in a pipeline fashion and dynamically stops when no more elements are required, without creating any temporary arrays in memory:
1
for line in (DATA.lazy.grep{ _ < 'd' }.map{ print ">> "; .uc }) {
2
say line
3
}
4
5
__DATA__
6
a
7
b
8
c
9
d
Copied!
Output (which illustrates that .map{} is truly lazy):
1
>> A
2
>> B
3
>> C
Copied!
This mechanism is generic enough to support any kind of object that implements the .iter() method, which returns a Block that gives back one element at a time when it's called with no arguments. When the iteration ends, it must return nil.
1
class Example(data) {
2
method iter {
3
var p = 0
4
{
5
data[p++]
6
}
7
}
8
}
9
10
var obj = Example([1,2,3,4,5])
11
say obj.lazy.grep{.is_prime}.to_a # filters all the primes lazily
Copied!
Currently, the .iter() method is defined in the following built-in classes: Array, String, FileHandle, DirHandle, RangeString, RangeNumber and Lazy.
Last modified 1yr ago
Copy link