4 module type MATRIX = sig
7 val create : rows:int -> cols:int -> data:'a -> 'a t
9 val get : 'a t -> row:int -> col:int -> 'a
11 val set : 'a t -> row:int -> col:int -> data:'a -> unit
13 val map : 'a t -> f:(row:int -> col:int -> data:'a -> 'b) -> 'b t
15 val iter : 'a t -> f:(row:int -> col:int -> data:'a -> unit) -> unit
18 module Matrix : MATRIX = struct
19 type 'a t = 'a array array
21 let create ~rows ~cols ~data =
22 Array.make_matrix ~dimx:rows ~dimy:cols data
45 let set t ~row ~col ~data =
50 module type CELL = sig
55 val react : t -> states:int list -> t
59 module Conway : CELL = struct
67 let live_neighbors = List.fold_left states ~init:0 ~f:(+) in
69 | A when live_neighbors < 2 -> D
70 | A when live_neighbors < 4 -> A
71 | A when live_neighbors > 3 -> D
72 | D when live_neighbors = 3 -> A
78 let pool = Matrix.create ~rows:5 ~cols:5 ~data:() in
80 fun ~row ~col ~data:() -> printf "R: %d, K: %d\n" row col