Image for post
Image for post

Implementing Microbial I: Types

type Species = Species Characteristics

type alias Characteristics =
{ name : SpeciesName
, minNumberOfCells : Int
, maxNumberOfCells : Int
, growthRate : Float
, minArea : Float
, maxArea : Float
, color : Color
, lifeSpan : Int
, moves : Motion
}
type SpeciesName = Mono | Brio| Ferocious
type Motion = Immobile | Random Int | Hunter Int
type Organism =
Organism OrganismData

type alias OrganismData =
{
id : Int
, species : Species
, diameter : Float
, area : Float
, numberOfCells : Int
, position : Position
, age : Int
}
mono : Species
mono = Species {
name = Mono
, minNumberOfCells = 1
, maxNumberOfCells = 1
, maxArea = 3
, minArea = 1
, growthRate = 0.01
, color = Color.rgb 0 0.7 0.8
, lifeSpan = 600
, moves = Random 1
}
monoSeed : Organism
monoSeed =
Organism {
id = 0
, species = mono
, area = 1
, diameter = 1
, numberOfCells = 1
, position = {row = 5, column = 5}
, age = 0
}

Dynamics

type alias State =
{ organisms : List Organism
, seed : Random.Seed
, nextId : Int
}
nextState : State -> State
nextState state =
state
|> tick
|> moveOrganisms
|> growOrganisms
|> cellDivision
|> cellDeath

Mapping with State

List.map : (a -> b) -> List a -> List b
mapWithState : (s -> a -> (s, a)) -> (s, List a) -> (s, List a)
move : Seed -> Organism -> (Seed, Organism)
moveOrganisms : (Seed, List Organism) -> (Seed, List Organism)
moveOrganisms (s, list) =
mapWithState move (s, list)

Coda: some nitty-gritty code

mapWithState : (s -> a -> (s, a)) -> (s, List a) -> (s, List a)
mapWithState f (state, list) =
let
folder : a -> (s, List a) -> (s, List a)
folder item (state_, list_) =
let
(newState_, item_) = f state_ item
in
(newState_, item_ :: list_)
in
List.foldl folder (state, []) list

Written by

jxxcarlson on elm slack, http://jxxcarlson.github.io

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store