1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
module type CUSTOM = sig
type t
val t : t Caqti_type.t
end
let list_group l ~break =
let groups =
List.fold_left
(fun acc x ->
match acc with
| [] -> [ [ x ] ]
| current_group :: tl ->
if break (List.hd current_group) x then [ x ] :: current_group :: tl
else (x :: current_group) :: tl)
[] l
in
match groups with [] -> [] | l -> List.rev_map List.rev l
let load_many (get_parent, parent_key) children_getters_and_setters data =
data
|> list_group ~break:(fun x y ->
parent_key (get_parent x) <> parent_key (get_parent y))
|> List.map (fun group ->
let parents, children =
( List.map get_parent group,
List.map
(fun (getter, setter) -> (setter, List.map getter group))
children_getters_and_setters )
in
let parent = List.hd parents in
List.fold_left
(fun current_parent (setter, children) ->
setter current_parent children)
parent children)
module Internal = struct
module Dynparam = struct
type t = Pack : 'a Caqti_type.t * 'a -> t
let empty = Pack (Caqti_type.unit, ())
let add t x (Pack (t', x')) = Pack (Caqti_type.tup2 t' t, (x', x))
end
end