package prbnmcn-dagger

  1. Overview
  2. Docs

Source file foldable.ml

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
module Make_list (M : Intf.Core) :
  Intf.Foldable with type 'a t = 'a list and type 'a m := 'a M.t = struct
  type 'a t = 'a list

  let fold f acc m =
    let open M.Infix in
    List.fold_left
      (fun acc elt ->
        let* acc = acc in
        f acc elt)
      (return acc)
      m

  let iter f m =
    let open M.Infix in
    List.fold_left
      (fun acc elt ->
        let* () = acc in
        f elt)
      (return ())
      m
end

module Make_seq (M : Intf.Core) :
  Intf.Foldable with type 'a t = 'a Seq.t and type 'a m := 'a M.t = struct
  type 'a t = 'a Seq.t

  let fold f acc m =
    let open M.Infix in
    Seq.fold_left
      (fun acc elt ->
        let* acc = acc in
        f acc elt)
      (return acc)
      m

  let iter f m =
    let open M.Infix in
    Seq.fold_left
      (fun acc elt ->
        let* () = acc in
        f elt)
      (return ())
      m
end

module Make_array (M : Intf.Core) :
  Intf.Foldable with type 'a t = 'a array and type 'a m := 'a M.t = struct
  type 'a t = 'a array

  let fold f acc m =
    let open M.Infix in
    Array.fold_left
      (fun acc elt ->
        let* acc = acc in
        f acc elt)
      (return acc)
      m

  let iter f m =
    let open M.Infix in
    Array.fold_left
      (fun acc elt ->
        let* () = acc in
        f elt)
      (return ())
      m
end
OCaml

Innovation. Community. Security.