this repo has no description
1module List = Odoc_list
2
3type 'a tree = { node : 'a; children : 'a forest }
4and 'a forest = 'a tree list
5
6module type S = sig
7 type 'a t
8
9 val fold_left : f:('acc -> 'a -> 'acc) -> 'acc -> 'a t -> 'acc
10 val iter : f:('a -> unit) -> 'a t -> unit
11 val map : f:('a -> 'b) -> 'a t -> 'b t
12 val to_json : ('a -> Json.json) -> 'a t -> Json.json
13end
14
15type 'a t = 'a tree
16
17let rec to_json json_of { node; children } : Json.json =
18 `Object [ ("node", json_of node); ("children", to_json_f json_of children) ]
19
20and to_json_f json_of f = `Array (List.map (to_json json_of) f)
21
22let leaf node = { node; children = [] }
23
24let rec fold_left ~f acc { node; children } =
25 let acc = f acc node in
26 fold_left_forest ~f acc children
27
28and fold_left_forest ~f acc forest = List.fold_left (fold_left ~f) acc forest
29
30let rec iter ~f { node; children } =
31 let () = f node in
32 iter_forest ~f children
33
34and iter_forest ~f forest = List.iter (iter ~f) forest
35
36let rec map ~f { node; children } =
37 let node = f node in
38 let children = map_forest ~f children in
39 { node; children }
40
41and map_forest ~f forest = List.map (map ~f) forest
42
43let rec filter_map ~f { node; children } =
44 match f node with
45 | None -> None
46 | Some node ->
47 let children = filter_map_forest ~f children in
48 Some { node; children }
49
50and filter_map_forest ~f forest = List.filter_map (filter_map ~f) forest
51
52module Forest = struct
53 type 'a t = 'a forest
54
55 let fold_left = fold_left_forest
56 let iter = iter_forest
57 let map = map_forest
58 let filter_map = filter_map_forest
59 let to_json = to_json_f
60end