Lazy methods
Lazy methods provide an interesting concept of partially applying a method on a given object, delaying the evaluation until the result is needed.
1
var lz = 42.method('>') #=> LazyMethod
2
say lz(41) #=> true (i.e.: 42 > 41)
Copied!
Object.method() returns a LazyMethod object which can be called just like any other function, producing the result by calling the method which is stored inside the LazyMethod object.
This allow us to store or pass around partial expressions, which can be evaluated multiple times at any point in the future:
1
var str = "a-b-c"
2
var lzsplit = str.method(:uc).method(:split)
3
4
say lzsplit('') #=> ["A", "-", "B", "-", "C"]
5
say lzsplit('-') #=> ["A", "B", "C"]
Copied!
Last modified 1yr ago
Copy link