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 6358447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:22:33+00:00 2026-05-24T23:22:33+00:00

For example, suppose I have val list: List[(String, Double)] with values 04-03-1985, 1.5 05-03-1985,

  • 0

For example, suppose I have

val list: List[(String, Double)]

with values

"04-03-1985", 1.5
"05-03-1985", 2.4
"05-03-1985", 1.3

How could I produce a new List

"04-03-1985", 1.5
"05-03-1985", 3.7
  • 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-05-24T23:22:34+00:00Added an answer on May 24, 2026 at 11:22 pm

    Here’s a one-liner. It’s not particularly readable, unless one really internalizes the types of these higher order functions.

    val s = Seq(("04-03-1985" -> 1.5),
                ("05-03-1985" -> 2.4),
                ("05-03-1985" -> 1.3))
    
    s.groupBy(_._1).mapValues(_.map(_._2).sum)
    // returns: Map(04-03-1985 -> 1.5, 05-03-1985 -> 3.7)
    

    Another approach is to add the key-value pairs one-by-one using fold,

    s.foldLeft(Map[String, Double]()) { case (m, (k, v)) =>
      m + (k -> (v + m.getOrElse(k, 0d)))
    }
    

    The equivalent for comprehension is most accessible, in my opinion,

    var m = Map[String, Double]()
    for ((k, v) <- s) {
      m += k -> (v + m.getOrElse(k, 0d))
    }
    

    Maybe something nicer can be done with Scalaz’s monoid typeclass for Map.

    Note that you can convert between Map[K, V] and Seq[(K, V)] using the toSeq and toMap methods.


    Update. After pondering it some more, I think the natural abstraction would be a “multimap” conversion, of type,

    def seqToMultimap[A, B](s: Seq[(A, B)]): Map[A, Seq[B]]
    

    With the appropriate implicit extension in one’s personal library, one could then write:

    s.toMultimap.mapValues(_.sum)
    

    This is the clearest of all, in my opinion!

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

Sidebar

Related Questions

For example suppose I have a sorted list val sorted = List(1, 5, 15,
Suppose I have a string 'nvarchar(50)', which is for example the T-SQL string segment
For example, suppose I have a class: class Foo { public: std::string& Name() {
For example suppose I have val sortedSignal = signal.sortBy(_.variable) And I wish to apply
For example suppose I have the following string we went to the (big) zoo
For example suppose I have the following join join on p.match_id = o.match_id Could
For example suppose I have $blah = C$#@#.a534&; I wish to filter the string
For example suppose I have val letters = ('a', 'b', 'c', 'd', 'e') val
Suppose I have a page located at www.example.com/foo , and it contains an <iframe>
Suppose I have written a decorator that does something very generic. For example, it

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.