X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=polymorphic-life%2F001%2Fsrc%2Fpolymorphic_life.ml;h=544136cf1b13c43e3742624e07c5ecda28aa42aa;hb=949d1c88ee5df62985ffd56e851544e4713334eb;hp=4a289923855094250e67b9a2a1328119578eeee1;hpb=b36c0aefcdd063383fcb93863d47741d8bcc7d5a;p=cellular-automata.git diff --git a/polymorphic-life/001/src/polymorphic_life.ml b/polymorphic-life/001/src/polymorphic_life.ml index 4a28992..544136c 100644 --- a/polymorphic-life/001/src/polymorphic_life.ml +++ b/polymorphic-life/001/src/polymorphic_life.ml @@ -8,9 +8,9 @@ module type MATRIX = sig val get : 'a t -> row:int -> col:int -> 'a - val set : 'a t -> row:int -> col:int -> data:'a -> unit + val map : 'a t -> f:('a -> 'b) -> 'b t - val map : 'a t -> f:(row:int -> col:int -> data:'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 +40,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:( @@ -50,9 +53,6 @@ module Matrix : MATRIX = struct let get t ~row ~col = t.(row).(col) - - let set t ~row ~col ~data = - t.(row).(col) <- data end @@ -104,8 +104,7 @@ 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