A more sensible way to count.
[cellular-automata.git] / polymorphism / 001 / src / polymorphism.ml
index 62730e4..ab2cbd5 100644 (file)
@@ -211,7 +211,7 @@ struct
 
     val of_int : int -> t
 
-    val to_int : t -> int
+    val is_alive : t -> bool
 
     val to_cell : t -> Cell.t
 
@@ -227,9 +227,9 @@ struct
       | 1 -> A
       | _ -> assert false
 
-    let to_int = function
-      | D -> 0
-      | A -> 1
+    let is_alive = function
+      | D -> false
+      | A -> true
 
     let to_pheno = function
       | D -> PhenoType.create ' ' None
@@ -263,13 +263,14 @@ struct
   let create () =
     Random.int 2 |> State.of_int |> State.to_cell
 
-  let live_neighbors neighbors =
-    neighbors |> List.map ~f:(State.of_cell_state |- State.to_int)
-              |> List.fold_left ~init:0 ~f:(+)
+  let count_of_live =
+       List.map       ~f:State.of_cell_state
+    |- List.filter    ~f:State.is_alive
+    |- List.length
 
   let transition ~self ~neighbors =
     self |> State.of_cell_state
-         |> State.next ~live_neighbors:(live_neighbors neighbors)
+         |> State.next ~live_neighbors:(count_of_live neighbors)
          |> State.to_cell
 end
 
@@ -284,8 +285,6 @@ struct
 
     val of_int : int -> t
 
-    val to_int : t -> int
-
     val to_cell : t -> Cell.t
 
     val of_cell_state : Cell.State.t -> t
@@ -306,11 +305,6 @@ struct
       | 2 -> B
       | _ -> assert false
 
-    let to_int = function
-      | E -> 0
-      | T -> 1
-      | B -> 2
-
     let to_pheno = function
       | E -> PhenoType.create ' ' None
       | T -> PhenoType.create 'T' (Some `green)
@@ -351,15 +345,14 @@ struct
   let create () =
     Random.int 3 |> State.of_int |> State.to_cell
 
-  let burning_neighbors neighbors =
-    neighbors |> List.map ~f:State.of_cell_state
-              |> List.filter ~f:State.is_burning
-              |> List.map ~f:State.to_int
-              |> List.fold_left ~init:0 ~f:(+)
+  let count_of_burning =
+       List.map       ~f:State.of_cell_state
+    |- List.filter    ~f:State.is_burning
+    |- List.length
 
   let transition ~self ~neighbors =
     self |> State.of_cell_state
-         |> State.next ~burning_neighbors:(burning_neighbors neighbors)
+         |> State.next ~burning_neighbors:(count_of_burning neighbors)
          |> State.to_cell
 end
 
This page took 0.031131 seconds and 4 git commands to generate.