Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8985857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:25:05+00:00 2026-06-15T21:25:05+00:00

I am having trouble with the syntax for reduce. I have a hash of

  • 0

I am having trouble with the syntax for reduce. I have a hash of the following format:

H = {"Key1" => 1, "Key2" => 2}

I would like to use reduce to find the sum of the values in this function.

Something Like

H.reduce(0) {|memo, elem| memo+=elem}

I know this is wrong. I dont understand how I can make elem the value of the hash.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:25:06+00:00Added an answer on June 15, 2026 at 9:25 pm

    Use Enumerable#reduce, if you’re ok with getting nil if the hash happens to be empty:

    H.values.reduce(:+) # => 3
    Hash.new.values.reduce(:+) # => nil
    

    To safely get 0 when the hash is empty, use:

    H.values.reduce(0) { |sum,x| sum + x } # or...
    H.reduce(0) { |sum,(key,val)| sum + val } # ...if you need to inspect the key
    

    Here’s a quick benchmark, for kicks. Note that it appears to be slightly faster to reduce just the values rather than values from the key/value pairs:

                                   user     system      total        real
    H.values.reduce(:+)        4.510000   0.080000   4.590000 (  4.595229)
    H.values.reduce(0) {...}   4.660000   0.080000   4.740000 (  4.739708)
    H.reduce(0) {...}          5.160000   0.070000   5.230000 (  5.241916)
    
    require 'benchmark'
    
    size = 1_000
    hash = Hash[* Array.new(size*2) { rand } ]
    
    N=10_000
    Benchmark.bm(24) do |x|
      x.report('H.values.reduce(:+)')      { N.times { hash.dup.values.reduce(:+) } }
      x.report('H.values.reduce(0) {...}') { N.times { hash.dup.values.reduce(0) { |sum,x| sum + x } } }
      x.report('H.reduce(0) {...}')        { N.times { hash.dup.reduce(0) { |sum,(_,v)| sum + v } } }
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to reduce my confusion about Haskell's syntax and would like to find
Im having some trouble getting the syntax right. I have a movie clip that
I am having a bit of trouble figuring out the exact syntax to use
Im having trouble with basic syntax. I want to use jquery to select all
I'm having trouble finding exactly the syntax I need to use to set the
I am having trouble with structure definitions. I understand the syntax but can't use
I'm having some trouble with the syntax of Delphi. I have a record: type
I am having trouble writing the proper LINQ syntax. I have used it before
I am having trouble getting the right syntax for this. Say I have two
I think know how to do this in C# but I'm having syntax trouble

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.