1

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.

2 Answers 2

4

Adding & before the last parameter means it would be bound to a block used along with method invocation, and you want to pass lambda as a param.

Just remove it (def SumEachBy(collec, lamb)) and enjoy your lambda :)

Sign up to request clarification or add additional context in comments.

3 Comments

If I do that, I get NoMethodError: undefined method 'lamb' for main:Object at the line collec.each
Figured it out, have to call lambdas with square brackets, like lamb[x]. Thanks!
Peter Cooper has a great explanation video on this on youtube: youtube.com/watch?v=VBC-G6hahWA&feature=plcp
-1

I Dont know about the hole code but u can update the sum part by using inject:

def SumEachBy(collec, lamb) 
  collection.inject(0) { |sum, value| sum += value }
end

I could understand your code at all, what do u want to achieve here?

I edit the answer following Anton suggestion. =)

3 Comments

@Kache That is rude. The answers here are for you, the OP. If you improve your question, that is still for the purpose of you getting a better return. Don't take it the other way around. Regarding the variable names, they are not good. It is not a good habit to use camel case for variables in Ruby.
@sawa Well I'm glad (actually, no) you took it upon yourself to return to this SO post 3 hours after any new activity JUST to point out how rude I was in trying improve upon my bad naming. Also, I'd specifically pointed out I was a Ruby newbie (implying I'm not aware of convention), but someone (you) decided to edit that out of my post. And no, this isn't just for me. Another newbie may come across this later. It was just a simple syntactical error I was unable to identify while using Ruby for the first time.
@Kache You seemed to have misunderstood what I mentioned as rude. I did not write that the fact you edited your question is rude. I wrote that the words for ya in your comment is rude to Paulo. Whether you are a newbie or not is totally irrelevant. And I did not return to SO just to write the comment above. If you are newbie to SO, you shouldn't try to infer things from what you see on this site because you will likely take it wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.