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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:56:30+00:00 2026-06-06T11:56:30+00:00

Please show me how to rewrite toString in a functional way. The code is

  • 0

Please show me how to rewrite toString in a functional way.

The code is ok, but nothing to be proud of, there are 3 temporary variables in it.

class Field(x: Int, y: Int) {
  val value = init(x,y)
  private def init(x: Int, y: Int) = List.fill(x,y)(new Cell)
  override def toString(): String = {
    val temp = new StringBuilder
    for(i <- value) {
      for(j <- i) {
        temp.append(j.toString())
      }
      temp.append("\n")
    }
    temp.mkString
  }
}

Thanks guys!

  • 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-06T11:56:31+00:00Added an answer on June 6, 2026 at 11:56 am

    What about this (look ma! one line):

    override def toString() = value.map(_.mkString).mkString("\n")
    

    Sometimes it frightens me how compact Scala code can be…


    BTW if you don’t need to reuse init() method you can simply say:

    val value = List.fill(x,y)(new Cell)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why this code does not work ? Please show me the correct way. private
Can someone please show me, if there is a better way to remove one
Could some one please show me how I can rewrite the below method in
Just for my curiousity, I am trying to rewrite the following code // show
Could someone please show me a small snippet of code which demonstrates how to
Could someone please show me some code or a tutorial of how to use
Could somebody please show code which would do this quickly? Assume we get three
Can someone please show me a code example about how to get a long
Can someone please show me an example usage of Schneier's Blowfish code ( http://www.schneier.com/code/bfsh-sch.zip
Please show me the code how to import winforms controls, into excel xls. file.

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.