Source file TableclothArray.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
type 'a t = 'a array
let singleton (a : 'a) : 'a array = [| a |]
let clone = Base.Array.copy
let initialize (length : int) ~(f : int -> 'a) =
if length <= 0 then [||] else Base.Array.init length ~f
let repeat element ~length =
initialize length ~f:(TableclothFun.constant element)
let range ?(from = 0) (to_ : int) : int array =
Base.Array.init (max 0 (to_ - from)) ~f:(fun i -> i + from)
let fromList = Base.List.to_array
let from_list = fromList
let length (a : 'a array) : int = Base.Array.length a
let isEmpty a = length a = 0
let is_empty = isEmpty
let first t = if length t < 1 then None else Some t.(0)
let last t = if length t < 1 then None else Some t.(length t - 1)
let get = Base.Array.get
let getAt a ~index =
if index >= 0 && index < length a then Some (Base.Array.get a index) else None
let get_at = getAt
let ( .?() ) (array : 'element t) (index : int) : 'element option =
getAt array ~index
let set = Base.Array.set
let setAt t ~index ~value = set t index value
let set_at = setAt
let filter = Base.Array.filter
let sum (type a) t (module M : TableclothContainer.Sum with type t = a) =
Base.Array.fold t ~init:M.zero ~f:M.add
let filterMap = Base.Array.filter_map
let filter_map = filterMap
let flatMap = Base.Array.concat_map
let flat_map = flatMap
let fold a ~initial ~f = Base.Array.fold a ~init:initial ~f
let foldRight a ~initial ~f =
Base.Array.fold_right a ~init:initial ~f:(Fun.flip f)
let fold_right = foldRight
let count t ~f =
fold t ~initial:0 ~f:(fun total element ->
total + match f element with true -> 1 | false -> 0 )
let swap = Base.Array.swap
let find = Base.Array.find
let findIndex = Base.Array.findi
let find_index = findIndex
let map = Base.Array.map
let mapWithIndex = Base.Array.mapi
let map_with_index = mapWithIndex
let map2 (a : 'a array) (b : 'b array) ~(f : 'a -> 'b -> 'c) : 'c array =
let minLength = min (length a) (length b) in
Base.Array.init minLength ~f:(fun i -> f a.(i) b.(i))
let zip = map2 ~f:(fun left right -> (left, right))
let map3
(arrayA : 'a array)
(arrayB : 'b array)
(arrayC : 'c array)
~(f : 'a -> 'b -> 'c -> 'd) =
let minLength =
Base.min (length arrayA) (Base.min (length arrayC) (length arrayB))
in
Base.Array.init minLength ~f:(fun i -> f arrayA.(i) arrayB.(i) arrayC.(i))
let partition = Base.Array.partition_tf
let splitAt a ~index =
( Base.Array.init index ~f:(fun i -> a.(i))
, Base.Array.init (length a - index) ~f:(fun i -> a.(index + i)) )
let split_at = splitAt
let splitWhen a ~f =
match findIndex a ~f:(fun _index element -> f element) with
| None ->
(a, [||])
| Some (index, _) ->
splitAt a ~index
let split_when = splitWhen
let unzip = Base.Array.unzip
let append (a : 'a array) (a' : 'a array) : 'a array = Base.Array.append a a'
let flatten (al : 'a array array) : 'a array =
Base.Array.concat (Base.Array.to_list al)
let intersperse array ~sep =
Base.Array.init
(max 0 ((Array.length array * 2) - 1))
~f:(fun i -> if i mod 2 <> 0 then sep else array.(i / 2))
let any = Base.Array.exists
let all = Base.Array.for_all
let includes = Base.Array.mem
let reverse = Base.Array.rev_inplace
let values t =
let result =
fold t ~initial:[] ~f:(fun results element ->
match element with None -> results | Some value -> value :: results )
|> fromList
in
reverse result ;
result
let join t ~sep = Stdlib.String.concat sep (Array.to_list t)
let groupBy t comparator ~f =
fold t ~initial:(TableclothMap.empty comparator) ~f:(fun map element ->
let key = f element in
TableclothMap.update map ~key ~f:(function
| None ->
Some [ element ]
| Some elements ->
Some (element :: elements) ) )
let group_by = groupBy
let slice ?to_ array ~from =
let defaultTo = match to_ with None -> length array | Some i -> i in
let sliceFrom =
if from >= 0
then min (length array) from
else max 0 (min (length array) (length array + from))
in
let sliceTo =
if defaultTo >= 0
then min (length array) defaultTo
else max 0 (min (length array) (length array + defaultTo))
in
if sliceFrom >= sliceTo
then [||]
else Base.Array.init (sliceTo - sliceFrom) ~f:(fun i -> array.(i + sliceFrom))
let sliding ?(step = 1) a ~size =
let n = Array.length a in
if size > n
then [||]
else
initialize
(1 + ((n - size) / step))
~f:(fun i -> initialize size ~f:(fun j -> a.((i * step) + j)))
let chunksOf t ~size = sliding t ~step:size ~size
let chunks_of = chunksOf
let maximum = Base.Array.max_elt
let minimum = Base.Array.min_elt
let extent t ~compare =
fold t ~initial:None ~f:(fun range element ->
match range with
| None ->
Some (element, element)
| Some (min, max) ->
Some
( ( match compare element min < 0 with
| true ->
element
| false ->
min )
, match compare element max > 0 with
| true ->
element
| false ->
max ) )
let sort t = Base.Array.sort t
let forEach a ~f = Base.Array.iter a ~f
let for_each = forEach
let forEachWithIndex a ~f = Base.Array.iteri a ~f
let for_each_with_index = forEachWithIndex
let toList (a : 'a array) : 'a list = Base.Array.to_list a
let to_list = toList
let toIndexedList a =
Base.Array.fold_right
a
~init:(length a - 1, [])
~f:(fun x (i, acc) -> (i - 1, (i, x) :: acc))
|> Base.snd
let to_indexed_list = toIndexedList
let equal equal a b =
if length a <> length b
then false
else if length a = 0
then true
else
let rec loop index =
if index = length a
then true
else equal a.(index) b.(index) && loop (index + 1)
in
loop 0
let compare compare a b =
match Int.compare (length a) (length b) with
| 0 ->
if length a == 0
then 0
else
let rec loop index =
if index = length a
then 0
else
match compare a.(index) b.(index) with
| 0 ->
loop (index + 1)
| result ->
result
in
loop 0
| result ->
result