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

  • Home
  • SEARCH
  • 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 7975345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:37:09+00:00 2026-06-04T08:37:09+00:00

Using the solution here , I’m adding two maps together and treating them as

  • 0

Using the solution here, I’m adding two maps together and treating them as if they were sparse vectors. So

def addTwoVectors(map1: Map[Int, Double], map2: Map[Int, Double]) = {
  map1 ++ map2.map{ case (k,v) => k -> (v + map1.getOrElse(k,0)) }
}

Now I’d like to make this generic so that

def addTwoMaps[I, D <% Numeric[D]](m1: Map[I, D], m2: Map[I, D]) = {
  m1 ++ m2.map{ case (k,v) => k -> (v + m1.getOrElse(k, 0.asInstanceOf[D])) }
}

Unfortunately, it doesn’t seem to work:

error: type mismatch;
found   : D
required: String

So how do I make this function generic?

  • 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-04T08:37:10+00:00Added an answer on June 4, 2026 at 8:37 am

    How about this one?

    import scala.math.Numeric.Implicits._
    def addTwoMaps[I, D](m1: Map[I, D], m2: Map[I, D])(implicit numeric: scala.math.Numeric[D]) = {
      m1 ++ m2.map{ case (k: I,v: D) => k -> (v + m1.getOrElse(k, numeric.zero)) }
    }
    

    Cause I don’t know which one Numeric I have, I’m taking this info implicitly, and then importing zero method, which is specific for every Numeric type.

    Actually, I do believe, that scalaz solution will be much more cleaner and hope that somebody will post it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

the solution here: Jeditable CANCEL callback from AJAX callback? using onreset() doesnt seem to
Using the method presented here: http://cslibrary.stanford.edu/110/BinaryTrees.html#java 12. countTrees() Solution (Java) /** For the key
I created an app.config transform for my WinForms project using Dan Abramov's solution here
I'm using a solution I found here on stackoverflow that auto sizes an iframe
I'm not sure that regexes are the best solution here, but they seem logical
I'm new at stackoverflow, hope find my solution here :) Using PHP, i want
I am using the solution found here to show client side validation errors in
I am using the accepted solution here to convert an excel sheet into a
tried using solutions posted here How to fast get Hardware-ID in C#? and here
I am using a solution from this SO question to make a user download

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.