Given an array of lambdas and an array of values (both created at run time),
values.map{ |value| lambdas[0].(value) }
would (obviously) return as an array the result of applying the first of the lambdas to each of the values. But what I need to do is apply all of the lambdas, i.e. the equivalent of
values.
map{ |value| lambdas[0].(value) }.
map{ |value| lambdas[1].(value) }.
...
map{ |value| lambdas[-1].(value) }
I can certainly write a method to do so, e.g.
def map_all(lambdas, values)
if lambdas.length == 0
values
else
map_all(lambdas.drop(1), values.map{ |value| lambdas.first.(value) })
end
end
but is there a more elegant or idiomatic way to do this?
Sure, here you go: