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.
Since you cannot call a method on an object before that object has been initialized, you have to move
buildelsewhere. A natural place for it would be the companion object. Note thatbuildusesnum, which has not been initialized at the time you are callingbuild, so you’ll have to pass it as a parameter.