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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:29:02+00:00 2026-05-28T17:29:02+00:00

It is a commonplace when one needs to accumulate some data. The way I

  • 0

It is a commonplace when one needs to accumulate some data. The way I get used to do it is appending data chunks to array. But it’s a bad practice in scala, so how can I avoid it?

  • 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-28T17:29:03+00:00Added an answer on May 28, 2026 at 5:29 pm

    Well, there are two generic ways of dealing with accumulation: recursion and folding. Let’s look into very simple examples of each, to compute the sum of values of a list.

    def sumRecursively(list: List[Int]): Int = {
      def recurse(list: List[Int], acc: Int): Int =
        if (list.isEmpty) acc
        else recurse(list.tail, acc + list.head)
      recurse(list, 0)
    }
    
    def sumFolding(list: List[Int]): Int =
      list.foldLeft(0){ case (acc, n) => acc + n }
    

    There are many variations on this, which handle better one case or another.

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

Sidebar

Related Questions

The following practice is fairly commonplace in the inline JavaScript I have to work
What I'm trying to achieve must be simple but I just can't get it
ETL is pretty common-place. Data is out there somewhere so you go get it.
This question is not a technical but a historical one. I was just thinking
My App needs to connect to and grab data from a MySQL database sitting
There's some legacy code that I would like to refactor. There is some data
My source base is mostly using UTF8, but some older library has Windows Latin1
I've seen statements to the effect that it is possible, even commonplace, to reference
What are some iPhone or Android applications that use SMS as their primary means
I have open-sourced some of my projects. I'd like to publish the artifacts in

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.