Hide (int -> state) mappings.
[cellular-automata.git] / polymorphism / 001 / src / polymorphism.ml
index 0444595..5d6fbab 100644 (file)
@@ -209,9 +209,9 @@ struct
   sig
     type t = D | A
 
-    val of_int : int -> t
+    val random : unit -> t
 
-    val to_int : t -> int
+    val is_alive : t -> bool
 
     val to_cell : t -> Cell.t
 
@@ -222,14 +222,15 @@ struct
   struct
     type t = D | A
 
-    let of_int = function
+    let random () =
+      match Random.int 2 with
       | 0 -> D
       | 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
@@ -260,13 +261,13 @@ struct
       | D -> D
   end
 
-  let create () =
-    Random.int 2 |> State.of_int |> State.to_cell
+  let create =
+    State.random |- State.to_cell
 
   let count_of_live =
-       List.map       ~f:State.of_cell_state
-    |- List.map       ~f:State.to_int
-    |- List.fold_left ~f:(+) ~init:0
+       List.map    ~f:State.of_cell_state
+    |- List.filter ~f:State.is_alive
+    |- List.length
 
   let transition ~self ~neighbors =
     self |> State.of_cell_state
@@ -283,9 +284,7 @@ struct
 
     val is_burning : t -> bool
 
-    val of_int : int -> t
-
-    val to_int : t -> int
+    val random : unit -> t
 
     val to_cell : t -> Cell.t
 
@@ -301,17 +300,13 @@ struct
       | T -> false
       | B -> true
 
-    let of_int = function
+    let random () =
+      match Random.int 3 with
       | 0 -> E
       | 1 -> T
       | 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)
@@ -349,14 +344,13 @@ struct
       | B, _                            -> E
   end
 
-  let create () =
-    Random.int 3 |> State.of_int |> State.to_cell
+  let create =
+    State.random |- State.to_cell
 
   let count_of_burning =
-       List.map       ~f:State.of_cell_state
-    |- List.filter    ~f:State.is_burning
-    |- List.map       ~f:State.to_int
-    |- List.fold_left ~f:(+) ~init:0
+       List.map    ~f:State.of_cell_state
+    |- List.filter ~f:State.is_burning
+    |- List.length
 
   let transition ~self ~neighbors =
     self |> State.of_cell_state
This page took 0.046738 seconds and 4 git commands to generate.