I'm exploring Functional Programming with the Ruby language. Below is my version of a Fold in Ruby. I've tested it on a variety of functions, reverse, filter, map etc, and it returns the results as expected. But it mutates data and needs assignment statements. Can anyone help me to do the same but without violating the Functional paradigm? Can anyone help me with the partial application of the curried function at the bottom? I suspect there something obvious I'm missing. Thanks.
fold_l = lambda do |ray, base, funcky|
if ray == []
base
else
base = funcky.call(base,ray.first)
ray.shift
fold_l.call(ray,base,funcky)
end
end
abc = [1, 2, 3, 4, 5, 6, 7]
mapper = lambda {|sum, x| sum << x*x}
lengthy = lambda {|sum, _| sum + 1}
p fold_l.call(abc,[],mapper) ## works fine
p abc ## but mutates data!!
abc = [1, 2, 3, 4, 5, 6, 7]
p curryFold = fold_l.curry.(abc).(0).(lengthy) ## works fine
lengthC = curryFold.(base:0).(funcky:lengthy)
p lengthC.call.(abc) ## but this gives error