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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:48:29+00:00 2026-05-31T08:48:29+00:00

I would like to write a method mergeKeys that groups the values in an

  • 0

I would like to write a method mergeKeys that groups the values in an Iterable[(K, V)] by the keys. For example, I could write:

  def mergeKeysList[K, V](iter: Iterable[(K, V)]) = {
     iter.foldLeft(Map[K, List[V]]().withDefaultValue(List.empty[V])) {
        case (map, (k, v)) =>
          map + (k -> (v :: map(k)))
     }
  }

However, I would like to be able to use any Monoid instead of writing a method for List. For example, the values may be integers and I want to sum them instead of appending them in a list. Or they may be tuples (String, Int) where I want to accumulate the strings in a set but add the integers. How can I write such a method? Or is there something else I can use in scalaz to get this done?

Update: I wasn’t as far away as I thought. I got a little bit closer, but I still don’t know how to make it work if the values are tuples. Do I need to write yet another implicit conversion? I.e., one implicit conversion for each number of type parameters?

sealed trait SuperTraversable[T, U, F[_]]
extends scalaz.PimpedType[TraversableOnce[(T, F[U])]] {
  def mergeKeys(implicit mon: Monoid[F[U]]): Map[T, F[U]] = {
    value.foldLeft(Map[T, F[U]]().withDefaultValue(mon.zero)) {
      case (map, (k, v)) =>
        map + (k -> (map(k) |+| v))
    }
  }
}

implicit def superTraversable[T, U, F[_]](
  as: TraversableOnce[(T, F[U])]
): SuperTraversable[T, U, F] = 
    new SuperTraversable[T, U, F] {
      val value = as
    }
  • 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-31T08:48:31+00:00Added an answer on May 31, 2026 at 8:48 am

    First, while it’s not relevant to your question, you are limiting your code’s
    generality by explicitly mentioning the type constructor F[_]. It works fine
    without doing so:

    sealed trait SuperTraversable[K, V]
    extends scalaz.PimpedType[TraversableOnce[(K, V)]] {
        def mergeKeys(implicit mon: Monoid[V]): Map[K, V] = {
            value.foldLeft(Map[K, V]().withDefaultValue(mon.zero)) {
                case (map, (k, v)) =>
                    map + (k -> (map(k) |+| v))
            }
        }
    }
    
    [...]
    

    Now, for your actual question, there’s no need to change mergeKeys to handle
    funny kinds of combinations; just write a Monoid to handle whatever kind of
    combining you want to do. Say you wanted to do your Strings+Ints example:

    implicit def monoidStringInt = new Monoid[(String, Int)] {
        val zero = ("", 0)
        def append(a: (String, Int), b: => (String, Int)) = (a, b) match {
            case ((a1, a2), (b1, b2)) => (a1 + b1, a2 + b2)
        }
    }
    
    println {
        List(
            "a" -> ("Hello, ", 20),
            "b" -> ("Goodbye, ", 30),
            "a" -> ("World", 12)
        ).mergeKeys
    }
    

    gives

    Map(a -> (Hello, World,32), b -> (Goodbye, ,30))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to write a method in ruby that takes a class with
HI i would like to write a method that returns all the events that
I would like to write a method that works sort of like this: public
I would like to write a method that deletes my application's Core Data store,
I have a template class, and would like to write a member method that's
I would like to write a method similar to the following def appendFile(fileName: String,
I would like to write a C# method that would transform any title into
If I would like to write a method that takes a variable number of
I would like to write a code internal to my method that print which
I would like to write a plug-in that will allow a custom written CRM

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.