Use idiomatic abbreviations for rows and columns.
[cellular-automata.git] / polymorphic-life / 001 / src / polymorphic_life.ml
CommitLineData
8c93b722
SK
1open Core.Std
2
3
4d49c95e
SK
4module type MATRIX = sig
5 type 'a t
6
63fe855d 7 val create : rs:int -> ks:int -> data:'a -> 'a t
4d49c95e 8
63fe855d 9 val get : 'a t -> r:int -> k:int -> 'a
4d49c95e 10
65dbef41
SK
11 val map : 'a t -> f:('a -> 'b) -> 'b t
12
63fe855d 13 val mapi : 'a t -> f:(r:int -> k:int -> data:'a -> 'b) -> 'b t
4d49c95e 14
63fe855d 15 val iter : 'a t -> f:(r:int -> k:int -> data:'a -> unit) -> unit
0ce0e798
SK
16
17 val print : 'a t -> to_string:('a -> string) -> unit
4d49c95e
SK
18end
19
20module Matrix : MATRIX = struct
21 type 'a t = 'a array array
22
63fe855d
SK
23 let create ~rs ~ks ~data =
24 Array.make_matrix ~dimx:rs ~dimy:ks data
4d49c95e
SK
25
26 let iter t ~f =
27 Array.iteri t ~f:(
63fe855d
SK
28 fun r ks ->
29 Array.iteri ks ~f:(
30 fun k data ->
31 f ~r ~k ~data
4d49c95e
SK
32 )
33 )
34
0ce0e798
SK
35 let print t ~to_string =
36 Array.iter t ~f:(
63fe855d
SK
37 fun r ->
38 Array.iter r ~f:(fun x -> printf "%s" (to_string x));
0ce0e798
SK
39 print_newline ()
40 )
41
4d49c95e 42 let map t ~f =
65dbef41
SK
43 Array.map t ~f:(Array.map ~f:(fun x -> f x))
44
45 let mapi t ~f =
4d49c95e 46 Array.mapi t ~f:(
63fe855d
SK
47 fun r ks ->
48 Array.mapi ks ~f:(
49 fun k data ->
50 f ~r ~k ~data
4d49c95e
SK
51 )
52 )
53
63fe855d
SK
54 let get t ~r ~k =
55 t.(r).(k)
4d49c95e
SK
56end
57
58
da8f1674
SK
59module type CELL = sig
60 type t
61
b6599385 62 val create : unit -> t
0ce0e798
SK
63
64 val to_string : t -> string
65
da8f1674
SK
66 val state : t -> int
67
68 val react : t -> states:int list -> t
69end
70
71
0d6f7833
SK
72module Conway : CELL = struct
73 type t = D | A
74
0ce0e798
SK
75 let of_int = function
76 | 0 -> D
77 | 1 -> A
78 | _ -> assert false
79
80 let to_int = function
0d6f7833
SK
81 | D -> 0
82 | A -> 1
83
0ce0e798
SK
84 let to_string = function
85 | D -> " "
86 | A -> "o"
87
b6599385 88 let create () =
5c37daed 89 Random.int 2 |> of_int
0ce0e798
SK
90
91 let state = to_int
92
0d6f7833
SK
93 let react t ~states =
94 let live_neighbors = List.fold_left states ~init:0 ~f:(+) in
95 match t with
96 | A when live_neighbors < 2 -> D
97 | A when live_neighbors < 4 -> A
98 | A when live_neighbors > 3 -> D
99 | D when live_neighbors = 3 -> A
b36c0aef
SK
100 | A -> A
101 | D -> D
0d6f7833
SK
102end
103
104
63fe855d 105let main rs ks () =
0ce0e798 106 Random.self_init ();
63fe855d 107 let grid = Matrix.create ~rs ~ks ~data:() |> Matrix.map ~f:Conway.create in
0ce0e798 108 Matrix.print grid ~to_string:Conway.to_string
8c93b722
SK
109
110
7d89c037
SK
111let spec =
112 let summary = "Polymorphic Cellular Automata" in
113 let spec =
114 let open Command.Spec in
115 empty
116 +> flag "-rows" (optional_with_default 5 int) ~doc:"Height"
117 +> flag "-cols" (optional_with_default 5 int) ~doc:"Width"
118 in
119 Command.basic ~summary spec main
120
121
122let () = Command.run spec
This page took 0.030799 seconds and 4 git commands to generate.