X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=polymorphic-life%2F001%2Fsrc%2Fpolymorphic_life.ml;h=c28852a70a937b8b9bbb02c3ee7584b8981c3d19;hb=65dbef41f5989d9d3e8bd1f9067a6235e8062070;hp=e25bbf623bb4ac49afc6447916e953ecd04c2f44;hpb=0ce0e7980a7b2b94b646d063127f064290340cfa;p=cellular-automata.git diff --git a/polymorphic-life/001/src/polymorphic_life.ml b/polymorphic-life/001/src/polymorphic_life.ml index e25bbf6..c28852a 100644 --- a/polymorphic-life/001/src/polymorphic_life.ml +++ b/polymorphic-life/001/src/polymorphic_life.ml @@ -10,7 +10,9 @@ module type MATRIX = sig val set : 'a t -> row:int -> col:int -> data:'a -> unit - val map : 'a t -> f:(row:int -> col:int -> data:'a -> 'b) -> 'b t + val map : 'a t -> f:('a -> 'b) -> 'b t + + val mapi : 'a t -> f:(row:int -> col:int -> data:'a -> 'b) -> 'b t val iter : 'a t -> f:(row:int -> col:int -> data:'a -> unit) -> unit @@ -40,6 +42,9 @@ module Matrix : MATRIX = struct ) let map t ~f = + Array.map t ~f:(Array.map ~f:(fun x -> f x)) + + let mapi t ~f = Array.mapi t ~f:( fun row cols -> Array.mapi cols ~f:( @@ -85,7 +90,8 @@ module Conway : CELL = struct | D -> " " | A -> "o" - let init () = Random.int 2 |> of_int + let init () = + Random.int 2 |> of_int let state = to_int @@ -96,14 +102,14 @@ module Conway : CELL = struct | A when live_neighbors < 4 -> A | A when live_neighbors > 3 -> D | D when live_neighbors = 3 -> A - | t -> t + | A -> A + | D -> D end let main rows cols () = Random.self_init (); - let init ~row:_ ~col:_ ~data = Conway.init data in - let grid = Matrix.create ~rows ~cols ~data:() |> Matrix.map ~f:init in + let grid = Matrix.create ~rows ~cols ~data:() |> Matrix.map ~f:Conway.init in Matrix.print grid ~to_string:Conway.to_string