Trying to sum values of items in a collection of sets by passing a lambda. I'm thinking this is just some syntax error:
# inputs
setCosts = {["A"] => 3, ["B"] => 4, ["A", "B"] => 5 }
collectionOfSets= [[["A"], ["B"]], [["A"], ["A", "B"]]]
# method and lambda
getSetCost = ->(x) { setCosts[x] }
def SumEachBy(collec, &lamb) # stack trace starts here
sum = 0
collec.each { |x| sum += lamb(x) }
return sum
end
# process output
collecValues = Hash[collectionOfSets.map { |set| [set, SumEachBy(set, getSetCost)] }]
I'm getting:
ArgumentError: wrong number of arguments (2 for 1)
I'm expecting collecValues to be:
{[["A"], ["B"]] => 7, [["A"], ["A", "B"]] => 8}
Where is my error?
By the way, if there's a better way to do this in Ruby, please let me know that too.