package bwd

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file Bwd.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
type 'a bwd =
  | Emp
  | Snoc of 'a bwd * 'a

module BwdLabels =
struct
  type 'a t = 'a bwd =
    | Emp
    | Snoc of 'a t * 'a

  let length xs =
    let rec go acc =
      function
      | Emp -> acc
      | Snoc (xs, _) -> (go[@tailcall]) (acc+1) xs
    in
    go 0 xs

  let snoc l x = Snoc (l, x)

  let nth xs i =
    if i < 0 then
      invalid_arg "BwdLabels.nth"
    else
      let rec go =
        function
        | Emp, _ -> failwith "BwdLabels.nth"
        | Snoc (_, x), 0 -> x
        | Snoc (xs, _), i -> (go[@tailcall]) (xs, i - 1)
      in go (xs, i)

  let append xs ys =
    let rec go =
      function
      | xs, [] -> xs
      | xs, y :: ys ->
        (go[@tailcall]) (Snoc (xs, y), ys)
    in go (xs, ys)

  let prepend xs ys =
    let rec go =
      function
      | Emp, ys -> ys
      | Snoc (xs, x), ys ->
        (go[@tailcall]) (xs, x :: ys)
    in go (xs, ys)

  let equal ~eq xs ys =
    let rec go =
      function
      | Emp, Emp -> true
      | Snoc (xs, x), Snoc (ys, y) ->
        eq x y && (go[@tailcall]) (xs, ys)
      | _ -> false
    in go (xs, ys)

  let compare ~cmp xs ys =
    let rec go =
      function
      | Emp, Emp -> 0
      | Emp, _ -> -1
      | _, Emp -> 1
      | Snoc (xs, x), Snoc (ys, y) ->
        let c = cmp x y in
        if c <> 0 then c else (go[@tailcall]) (xs, ys)
    in go (xs, ys)

  let iter ~f =
    let rec go =
      function
      | Emp -> ()
      | Snoc (xs, x) ->
        f x; (go[@tailcall]) xs
    in go

  let map ~f =
    let[@tail_mod_cons] rec go =
      function
      | Emp -> Emp
      | Snoc (xs, x) -> Snoc ((go[@tailcall]) xs, f x)
    in go

  let mapi ~f =
    let[@tail_mod_cons] rec go i =
      function
      | Emp -> Emp
      | Snoc (xs, x) -> Snoc ((go[@tailcall]) (i + 1) xs, f i x)
    in
    go 0

  let filter_map ~f =
    let[@tail_mod_cons] rec go =
      function
      | Emp -> Emp
      | Snoc (xs, x) ->
        match f x with
        | None -> go xs
        | Some fx -> Snoc ((go[@tailcall]) xs, fx)
    in go

  let concat_map ~f =
    let rec go =
      function
      | Emp -> Emp
      | Snoc (xs, x) -> append (go xs) (f x)
    in go

  let fold_left ~f ~init =
    let rec go =
      function
      | Emp -> init
      | Snoc (xs, x) ->
        f (go xs) x
    in go

  let fold_right ~f xs ~init =
    let rec go init =
      function
      | Emp -> init
      | Snoc (xs, x) ->
        let init = f x init in
        (go[@tailcall]) init xs
    in go init xs

  let fold_right2 ~f xs ys ~init =
    let rec go init =
      function
      | Emp, Emp -> init
      | Snoc (xs, x), Snoc (ys, y) ->
        let init = f x y init in
        (go[@tailcall]) init (xs, ys)
      | _ -> invalid_arg "BwdLabels.fold_right2"
    in
    go init (xs, ys)

  let for_all ~f =
    let rec go =
      function
      | Emp -> true
      | Snoc (xs, x) ->
        f x && (go[@tailcall]) xs
    in go

  let exists ~f =
    let rec go =
      function
      | Emp -> false
      | Snoc (xs, x) ->
        f x || (go[@tailcall]) xs
    in go

  let mem a ~set =
    let rec go =
      function
      | Emp -> false
      | Snoc (xs, x) ->
        a = x || (go[@tailcall]) xs
    in go set

  let filter ~f =
    let[@tail_mod_cons] rec go =
      function
      | Emp -> Emp
      | Snoc (xs, x) ->
        if f x then
          go xs
        else
          Snoc ((go[@tailcall]) xs, x)
    in go

  let to_list xs =
    prepend xs []

  let of_list xs =
    append Emp xs
end

module BwdNotation =
struct
  open BwdLabels
  let (#<) = snoc
  let (<><) = append
  let (<>>) = prepend
end
OCaml

Innovation. Community. Security.