X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=polymorphism%2F001%2Fsrc%2Fpolymorphism.ml;h=c2f0c3e5caff035ad83cfa1c95ba8e997cffd212;hb=46aacfbc9482c1b72d6c69d6b6e323a2ea33719f;hp=d292e289a5d05718f96b630ffffbbeb9258bdcb9;hpb=635c1868707b86e336209e0eff475526549a792e;p=cellular-automata.git diff --git a/polymorphism/001/src/polymorphism.ml b/polymorphism/001/src/polymorphism.ml index d292e28..c2f0c3e 100644 --- a/polymorphism/001/src/polymorphism.ml +++ b/polymorphism/001/src/polymorphism.ml @@ -51,7 +51,7 @@ sig type 'a t - val create : rs:int -> ks:int -> 'a -> 'a t + val make : rs:int -> ks:int -> 'a -> 'a t val get_neighbors : 'a t -> Point.t -> 'a list @@ -100,7 +100,7 @@ struct type 'a t = 'a array array - let create ~rs ~ks x = + let make ~rs ~ks x = Array.make_matrix ~dimx:rs ~dimy:ks x let iter t ~f = @@ -143,7 +143,7 @@ struct let neighborhood t point = List.map Direction.all ~f:Direction.to_offset - |> List.map ~f:(fun offset_point -> Point.(point + offset_point)) + |> List.map ~f:(Point.(+) point) |> List.filter ~f:(is_within_bounds t) let get_neighbors t point = @@ -155,7 +155,7 @@ module PhenoType : sig type t - val create : char -> Terminal.color option -> t + val make : char -> Terminal.color option -> t val to_string : t -> string end = @@ -164,7 +164,7 @@ struct ; character : char } - let create character color = + let make character color = {color; character} let to_string = function @@ -195,7 +195,7 @@ end module type RULE = sig - val create : unit -> Cell.t + val init : unit -> Cell.t val transition : self:Cell.State.t -> neighbors:Cell.State.t list @@ -209,7 +209,7 @@ struct sig type t = D | A - val of_int : int -> t + val random : unit -> t val is_alive : t -> bool @@ -222,7 +222,8 @@ struct struct type t = D | A - let of_int = function + let random () = + match Random.int 2 with | 0 -> D | 1 -> A | _ -> assert false @@ -232,8 +233,8 @@ struct | A -> true let to_pheno = function - | D -> PhenoType.create ' ' None - | A -> PhenoType.create 'o' (Some `white) + | D -> PhenoType.make ' ' None + | A -> PhenoType.make 'o' (Some `white) let of_cell_state = function | Cell.State.Dead -> D @@ -260,8 +261,8 @@ struct | D -> D end - let create () = - Random.int 2 |> State.of_int |> State.to_cell + let init = + State.random |- State.to_cell let count_of_live = List.map ~f:State.of_cell_state @@ -283,7 +284,7 @@ struct val is_burning : t -> bool - val of_int : int -> t + val random : unit -> t val to_cell : t -> Cell.t @@ -299,16 +300,17 @@ 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_pheno = function - | E -> PhenoType.create ' ' None - | T -> PhenoType.create 'T' (Some `green) - | B -> PhenoType.create '#' (Some `red) + | E -> PhenoType.make ' ' None + | T -> PhenoType.make 'T' (Some `green) + | B -> PhenoType.make '#' (Some `red) let of_cell_state = function | Cell.State.Dead -> E @@ -342,8 +344,8 @@ struct | B, _ -> E end - let create () = - Random.int 3 |> State.of_int |> State.to_cell + let init = + State.random |- State.to_cell let count_of_burning = List.map ~f:State.of_cell_state @@ -361,7 +363,7 @@ module Automaton : sig type t - val create : rows:int + val make : rows:int -> columns:int -> interval:float -> rules: (module RULE) list @@ -379,17 +381,17 @@ struct ; bar : string } - let create ~rows:rs ~columns:ks ~interval ~rules = + let make ~rows:rs ~columns:ks ~interval ~rules = let n = List.length rules in let init () = let rule = List.nth_exn rules (Random.int n) in let module Rule = (val rule : RULE) in { rule - ; data = Rule.create () + ; data = Rule.init () } in Terminal.clear (); - { grid = Matrix.map ~f:init (Matrix.create ~rs ~ks ()) + { grid = Matrix.map ~f:init (Matrix.make ~rs ~ks ()) ; interval = Time.Span.of_float interval ; bar = String.make ks '-' } @@ -434,7 +436,7 @@ let main interval () = ; (module ForestFire : RULE) ] in - Automaton.loop (Automaton.create ~rows:(rows - 3) ~columns ~interval ~rules) + Automaton.loop (Automaton.make ~rows:(rows - 3) ~columns ~interval ~rules) let spec =