I'm working on creating a (simple) agent-based model to learn Scala and functional programming.
I already created it in Python, so my idea is to code it following the already existing ideas, but I ran into a problem just at the beginning:
I have a class which describes an agent in the model, and another class which describes the society it lives in. The society is composed of N agents, where N is an integer. In Python I would do a list-comprehension to store all the instances of the class, but how do I do this in Scala? Is there a better way than using the following code:
import agent.Agent
class Society([omitted-for-brevity]){
val tmp_agents = List()
for(i <- 1 to puntos){
val tmp_agent = new Agent(pos_X = 0, name="Agent "+i)
tmp_agents :+ tmp_agent
}
- The Agent has values that can change, so the
val tmp_agent
in the for-loop shouldn't it be a var? Or is it valid if the Agent class hasvar
arguments as inputs? - Should the
tmp_agents
list be a val if only the objects in it are going to change "internal" values? Is it OK to append to a val?