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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:00:26+00:00 2026-05-27T18:00:26+00:00

Coming from Java, I am learning Scala. I am interested in game and virtual

  • 0

Coming from Java, I am learning Scala. I am interested in game and virtual worlds programming, so I have decided my first program to be a tiny game world simulator. To my mind, all game elements usually live in the following phases: creation, update, deletion. It is absolutely clear for me in Java or other OOP. Now I come to Scala… What I have implemented so far, is just a container for a number of cells that should mutate every cycle. Here is the code:

//init
val rand : Random = new Random

//mutation variations
def mutF(f:Int=>Int, v: Int) : Int = {f(v)}

def mutFA(v:Int) : Int = mutF(x => x, v)
def mutFB(v:Int) : Int = mutF(x => x + x, v)
def mutFC(v:Int) : Int = mutF(x => x - x, v)

    //mutation variance
val mutFS : List[Int=>Int] = List(mutFA, mutFB, mutFC)

    //cycle through mutation functions
def mutFF(f:Int=>Int) : Int=>Int = {
    val i = mutFS.indexOf(f)
    if(i < mutFS.length) mutFS(i + 1)
    else mutFS(0)
}

//objects
class Cell(value:Int)(f:Int => Int){    //TODO: what will be without currying???
    def mutate() : Cell = new Cell(f(value))(f)
    def output() {
        print("[" + value + "]")
    }
}

//the main class
class Breed(generation:Int, num:Int, margins:Int, cells: List[Cell]) {

    def this(num:Int, margins:Int) = this(0, num, margins, build()) //<<<<<

    //make 1 cell
    def makeCell() : Cell = {
        val mutF:Int=>Int = mutFS(rand.nextInt(mutFS.length))
        val v = rand.nextInt(margins)
        println("BREED: making cell " + v)
        new Cell(v)(mutF)
    }

    //fill with random cells
    def build() : List[Cell] = {
        def addCell(acc:Int, list:List[Cell]) : List[Cell] = {
            println("BREED: build(), acc= " + acc + " list=" + list)
            if(acc <= 0) list
            else addCell(acc - 1, makeCell :: list)
        }
        addCell(num, List())
    }

//  val cells : List[Cell] = build()

    //go several generations ahead, print every generation
    def mutate(generations:Int) {
        def mutateF(acc:Int, breed : Breed) : Breed = {
            if (acc == 0) breed
            else {
                print("BREED: mutating, ")
                breed.output()
                mutateF(acc - 1, mutate(breed))
            }
        }
        mutateF(generations, this)
    }

    //mutate this breed
    def mutate(breed : Breed) : Breed = {
        def mutateF(l : List[Cell]) : List[Cell] = {
            l match {
                case Nil => Nil
                case y :: yx => y.mutate() :: mutateF(yx)
            }
        }
        new Breed(generation, num, margins, mutateF(build))
    }

    def output() {
        print("BREED: [" + generation + "] ")
        for(i <- 0 to num - 1) cells(i).output()
        println()
    }
}

Firstly – my question is – how to makу the ‘build()’ function work in the aux constructor? In Java that was no problem. What is Scala way to solve this? Secondly, can you please comment on my mistakes from the point of view from the functional approach in Scala?

UPDATE: I would appreciate a rewrite of this code as you would write it in pure Scala way.

  • 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-27T18:00:27+00:00Added an answer on May 27, 2026 at 6:00 pm

    Since you cannot call a method on an object before that object has been initialized, you have to move build elsewhere. A natural place for it would be the companion object. Note that build uses num, which has not been initialized at the time you are calling build, so you’ll have to pass it as a parameter.

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

Sidebar

Related Questions

I'm currently learning ObjC and Cocoa programming, coming from the Java world. To test
I am learning Scala (coming from a background of mostly Java). I am trying
this is my first post.. so I'm learning Android & Java (coming from Actionscript),
When I was learning Java coming from a background of some 20 years of
I just started learning C++ (coming from Java ) and am having some serious
Coming from PHP with some Java Struts 2.0 experience any tips/tricks on learning GWT?
I'm learning how to code in Java after after coming from C. In C
I'm learning C++ coming from a Java background (knowing a little C from many
I'm just learning c++ coming from a Java background. Just playing around with simple
G'Day Programmers, I am from Java background however I have just started learning C++

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.