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
)
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:(
let get t ~row ~col =
t.(row).(col)
-
- let set t ~row ~col ~data =
- t.(row).(col) <- data
end
module type CELL = sig
type t
- val init : unit -> t
+ val create : unit -> t
val to_string : t -> string
| D -> " "
| A -> "o"
- let init () = Random.int 2 |> of_int
+ let create () =
+ Random.int 2 |> of_int
let state = to_int
| 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.create in
Matrix.print grid ~to_string:Conway.to_string