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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:38:39+00:00 2026-06-02T15:38:39+00:00

I’m afraid this is another noob question. What I want to do is to

  • 0

I’m afraid this is another noob question.

What I want to do is to use a Map in order to count how often a word appears in a poe…m and then print the results to the console.
I went to the following code which I believe is working (while probably not quite idiomatic):

val poe_m="""Once upon a midnight dreary, while I pondered weak and weary,
            |Over many a quaint and curious volume of forgotten lore,
            |While I nodded, nearly napping, suddenly there came a tapping,
            |As of some one gently rapping, rapping at my chamber door.
            |`'Tis some visitor,' I muttered, `tapping at my chamber door -
            |Only this, and nothing more.'"""

val separators=Array(' ',',','.','-','\n','\'','`')
var words=new collection.immutable.HashMap[String,Int]
for(word<-poe_m.stripMargin.split(separators) if(!word.isEmpty))  
    words=words+(word.toLowerCase -> (words.getOrElse(word.toLowerCase,0)+1))

words.foreach(entry=>println("Word : "+entry._1+" count : "+entry._2))

As far as I understand, in Scala, immutable data structures are preferred to mutable ones and val preferable to varso I’m facing a dilemma : words should be a var (allowing a new instance of map to be used for each iteration) if results are to be stored in an immutable Map while turning words into a val implies to use a mutable Map.

Could someone enlighten me about the proper way to deal with this existential problem?

  • 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-02T15:38:42+00:00Added an answer on June 2, 2026 at 3:38 pm

    In this case you can use groupBy and mapValues:

    val tokens = poe_m.stripMargin.split(separators).filterNot(_.isEmpty)
    val words = tokens.groupBy(w => w).mapValues(_.size)
    

    More generally this is a job for a fold:

     val words = tokens.foldLeft(Map.empty[String, Int]) {
       case (m, t) => m.updated(t, m.getOrElse(t, 0) + 1)
     }
    

    The Wikipedia entry on folds gives some good clarifying examples.

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

Sidebar

Related Questions

Another complete Noob question about KnockoutJS I'm afraid... I understand the Observables feature, in
I am afraid this question is not supposed to be asked here but I
Okay, this is (probably) a very simple question, but I am afraid I know
We currently use VSS 6, this is not going to change I am afraid.
I'm afraid this is going to be a ridiculously stupid question, you might be
This is a beginner's question I'm afraid: I have an UIText which covers the
So I'm afraid this is another one of those slideshow questions that clutters up
This is probably a simple question, and I'm afraid the answer might be no,
I'm afraid I know the answer to this but I'll ask on the longshot
This will be a bit subjective, I'm afraid, but I'd value the advice of

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.