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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:16:11+00:00 2026-06-12T08:16:11+00:00

IntHistogram counts words and displays them count-descending: object IntHistogram { def main(args: Array[String]) {

  • 0

IntHistogram counts words and displays them count-descending:

object IntHistogram {

  def main(args: Array[String]) {
    val wordsToCount = "foo foo bar foo bar wtf foo bar".split(" ")
    val histogram = new IntHistogram
    for (word <- wordsToCount) histogram(word) += 1
    println(histogram)
    /* 
       (foo,4)
       (bar,3)
       (wtf,1)
    */
  }

}

class IntHistogram extends collection.mutable.HashMap[String,Int] {
  override def default(key:String) = 0
  def descendingPairs = toList.sortBy(_._2).reverse
  override def toString() = descendingPairs.mkString("\n")
}

I needed a DoubleHistogram, and I resorted to copy-and-paste because I couldn’t figure out how to define a generic “Histogram[NumberSuperClassOfIntAndDouble]” trait:

class DoubleHistogram extends collection.mutable.HashMap[String,Double] {
  override def default(key:String) = 0
  def descendingPairs = toList.sortBy(_._2).reverse
  override def toString() = descendingPairs.mkString("\n")
}

Can a smart/knowledgeable person show me how to define such a supertrait, so I can avoid the ugly copy-and-paste boilerplate?

Thanks in advance,

PT

P.S. I really want Histogram to be a trait, so I can mix the Histogram behavior into any numerically-valued Map. In reality there are lots of behaviors I need, besides a descending toString method; I simplified it for this question. Here Num is the fictional numeric superclass of Int and Double:

trait Histogram[Num] extends collection.Map[String,Num] {
  override def default(key:String) = 0
  def descendingPairs = toList.sortBy(_._2).reverse
  override def toString() = descendingPairs.mkString("\n")
}

I tried using Numeric, Number, import Ordering.Implicits._, all kinds of stuff… to no avail.

  • 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-12T08:16:13+00:00Added an answer on June 12, 2026 at 8:16 am

    This seems to work just fine with Numeric. Not that you can’t make Histogram a trait because of the context bound on N, but IntHistogram and DoubleHistogram can be traits.

    object IntHistogram {
    
      def main(args: Array[String]) {
        val wordsToCount = "foo foo bar foo bar wtf foo bar".split(" ")
        val histogram = new IntHistogram {}
        for (word <- wordsToCount) histogram(word) += 1
        println(histogram)
        /* 
           (foo,4)
           (bar,3)
           (wtf,1)
        */
      }
    
    }
    
    abstract class Histogram[N:Numeric] extends collection.mutable.HashMap[String,N] {
      override def default(key:String) = implicitly[Numeric[N]].zero
      def descendingPairs = toList.sortBy(_._2).reverse
      override def toString = descendingPairs.mkString("\n")
    }
    
    trait IntHistogram extends Histogram[Int]
    trait DoubleHistogram extends Histogram[Double]
    

    If you really, really need Histogram to be a trait, you can do it this way:

    trait Histogram[N] extends collection.mutable.HashMap[String,N] {
      implicit val n:Numeric[N]
      override def default(key:String) = n.zero
      def descendingPairs = toList.sortBy(_._2).reverse
      override def toString = descendingPairs.mkString("\n")
    }
    

    But then you have to instantiate it like this in main:

    val histogram = new Histogram[Int] { val n = implicitly[Numeric[Int]] }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a program to compress a string using the counts of repeated characters.
Woohoo, I've come to arrays now, thank god. Now, I've got 2 arrays! int
I have the following snippet: import qualified Data.Vector as V import qualified Data.ByteString.Lazy as
I'm using 6 arrays of ints, each holding 256 elements. Pretty standard, I'd think.

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.