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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:30:32+00:00 2026-05-14T20:30:32+00:00

I have some mutable scala code that I am trying to rewrite in a

  • 0

I have some mutable scala code that I am trying to rewrite in a more functional style. It is a fairly intricate piece of code, so I am trying to refactor it in pieces. My first thought was this:

def iterate(count:Int,d:MyComplexType) = {
  //Generate next value n
  //Process n causing some side effects
  return iterate(count - 1, n)
}

iterate(2000000,initialValue)

This didn’t seem functional at all to me, since I still have side effects mixed throughout my code. My second thought was this:

def generateStream(d:MyComplexType):Stream[MyComplexType] = {
  //Generate next value n
  return Stream.cons(n, generateStream(n))
}

for (n <- generateStream(initialValue).take(2000000)) {
  //process n causing some side effects
}

This seemed like a better solution to me, because at least I’ve isolated my functional value-generation code from the mutable value-processing code. However, this is much less memory efficient because I am generating a large list that I don’t really need to store.

This leaves me with 3 choices:

  1. Write a tail-recursive function, bite the bullet and refactor the value-processing code
  2. Use a lazy list. This is not a memory sensitive app (although it is performance sensitive)
  3. Come up with a new approach.

I guess what I really want is a lazily evaluated sequence where I can discard the values after I’ve processed them. Any suggestions?

  • 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-14T20:30:32+00:00Added an answer on May 14, 2026 at 8:30 pm

    Bear in mind that performance-critical algorithms often work better when mutable. So beware of premature de-optimization!

    In Scala 2.8 you can use Iterator.iterate to create an infinite sequence without stored values. But I’m not sure that alone will be the key step to refactoring the code to be more functional. It’s that “process data with side effects” part that is tricky.

    You might put the whole thing into the iterate block:

    Iterator.iterate(initialState)(x => {
      // create a new state based upon state x
    }).drop(2000000).next
    

    where you now have defined an infinite processing stream by sequentially modifying your initial state. You throw away the first 2000000 values (one of which is the initial value), then get the next one (which is the 2000000’th generated value). Try it out with 0 and x=>x+1 to see it in action.

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

Sidebar

Ask A Question

Stats

  • Questions 455k
  • Answers 455k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer it's add(invOne, BorderLayout.CENTER);. You should use constants from BorderLayout class… May 15, 2026 at 10:01 pm
  • Editorial Team
    Editorial Team added an answer You could create your ouwn helper, and put it in… May 15, 2026 at 10:01 pm
  • Editorial Team
    Editorial Team added an answer The best thing would be to have a flag on… May 15, 2026 at 10:01 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.