Y
Yuh-Ruey Chen
Two questions:
1) Are there equivalents for iteration/enumeration functions like map
that return iterator/enumeration objects (in a Python sense)?
An example:
def read_files(files)
files.each {|file|
# blah
}
end
read_file(['file1', 'file2', 'file3'].map_iter {|x| open(x)})
# map_iter would return an iterator object immediately instead of
opening every file and storing them into an array
I know that I could do this instead:
['file1', 'file2', 'file3'].each {|x|
open(x) {
# blah
}
}
but sometimes it's inconvenient to do that if there's already a
function that accepts an Enumerable such as read_files above.
2) Is there some lazy evaluation library that can recalculate lazy
expression when values change? Something with the following semantics
(or something like it):
x = lazy {a + b}
a = 1
b = 2
p x # 3 (calculates a + b)
p x # 3 (memoized value)
a = 3
p x # 5 (recalculates a + b)
a = [1,2]
b = [3,4]
p x # [1,2,3,4] (recalculates a + b)
p x # [1,2,3,4] (memoized value)
a[1] = '.'
p x # [1,'.',3,4] (recalculates a + b)
I know there's a library called lazy.rb, but it's not exactly what I'm
looking for as it doesn't implement this functionality.
Thanks.
1) Are there equivalents for iteration/enumeration functions like map
that return iterator/enumeration objects (in a Python sense)?
An example:
def read_files(files)
files.each {|file|
# blah
}
end
read_file(['file1', 'file2', 'file3'].map_iter {|x| open(x)})
# map_iter would return an iterator object immediately instead of
opening every file and storing them into an array
I know that I could do this instead:
['file1', 'file2', 'file3'].each {|x|
open(x) {
# blah
}
}
but sometimes it's inconvenient to do that if there's already a
function that accepts an Enumerable such as read_files above.
2) Is there some lazy evaluation library that can recalculate lazy
expression when values change? Something with the following semantics
(or something like it):
x = lazy {a + b}
a = 1
b = 2
p x # 3 (calculates a + b)
p x # 3 (memoized value)
a = 3
p x # 5 (recalculates a + b)
a = [1,2]
b = [3,4]
p x # [1,2,3,4] (recalculates a + b)
p x # [1,2,3,4] (memoized value)
a[1] = '.'
p x # [1,'.',3,4] (recalculates a + b)
I know there's a library called lazy.rb, but it's not exactly what I'm
looking for as it doesn't implement this functionality.
Thanks.