Skip to content

Commit e5d88c9

Browse files
committed
Update to 1.3
1 parent fcbce97 commit e5d88c9

16 files changed

+236
-57
lines changed

mksrc.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
echo "Checking out ocaml-container..."
44
git submodule update --init
55
cd ocaml-containers
6-
git checkout 1.2
6+
git checkout 1.3
77
echo "Making..."
88
make
99
echo "Copying sources..."

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bs-containers-core",
3-
"version": "1.2.0-5",
3+
"version": "1.3.0+20170817",
44
"description": "OCaml-containers core for BuckleScript",
55
"main": "index.js",
66
"scripts": {
@@ -13,6 +13,6 @@
1313
},
1414
"license": "BSD",
1515
"devDependencies": {
16-
"bs-platform": "^1.7.4"
16+
"bs-platform": "^1.8.2"
1717
}
1818
}

src/CCArray.ml

+6-4
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,13 @@ let rec find_aux f a i =
212212
| Some _ as res -> res
213213
| None -> find_aux f a (i+1)
214214

215-
let find f a =
216-
find_aux (fun _ -> f ) a 0
215+
let find_map f a = find_aux (fun _ -> f ) a 0
217216

218-
let findi f a =
219-
find_aux f a 0
217+
let find = find_map
218+
219+
let find_map_i f a = find_aux f a 0
220+
221+
let findi = find_map_i
220222

221223
let find_idx p a =
222224
find_aux (fun i x -> if p x then Some (i,x) else None) a 0

src/CCArray.mli

+27-13
Original file line numberDiff line numberDiff line change
@@ -71,30 +71,44 @@ val sorted : ('a -> 'a -> int) -> 'a t -> 'a array
7171

7272
val sort_indices : ('a -> 'a -> int) -> 'a t -> int array
7373
(** [sort_indices cmp a] returns a new array [b], with the same length as [a],
74-
such that [b.(i)] is the index of the [i]-th element of [a] in [sort cmp a].
75-
In other words, [map (fun i -> a.(i)) (sort_indices a) = sorted cmp a].
76-
[a] is not modified.
74+
such that [b.(i)] is the index at which the [i]-th element of [sorted cmp a]
75+
appears in [a]. [a] is not modified.
76+
77+
In other words, [map (fun i -> a.(i)) (sort_indices cmp a) = sorted cmp a].
78+
[sort_indices] yields the inverse permutation of {!sort_ranking}.
79+
7780
@since 1.0 *)
7881

7982
val sort_ranking : ('a -> 'a -> int) -> 'a t -> int array
8083
(** [sort_ranking cmp a] returns a new array [b], with the same length as [a],
81-
such that [b.(i)] is the position in [sorted cmp a] of the [i]-th
82-
element of [a].
83-
[a] is not modified.
84+
such that [b.(i)] is the index at which the [i]-the element of [a] appears
85+
in [sorted cmp a]. [a] is not modified.
8486
85-
In other words, [map (fun i -> (sorted cmp a).(i)) (sort_ranking cmp a) = a].
87+
In other words, [map (fun i -> (sorted cmp a).(i)) (sort_ranking cmp a) = a].
88+
[sort_ranking] yields the inverse permutation of {!sort_indices}.
8689
87-
Without duplicates, we also have
90+
In the absence of duplicate elements in [a], we also have
8891
[lookup_exn a.(i) (sorted a) = (sorted_ranking a).(i)]
8992
@since 1.0 *)
9093

94+
val find_map : ('a -> 'b option) -> 'a t -> 'b option
95+
(** [find_map f a] returns [Some y] if there is an element [x] such
96+
that [f x = Some y], else it returns [None]
97+
@since 1.3
98+
*)
99+
91100
val find : ('a -> 'b option) -> 'a t -> 'b option
92-
(** [find f a] returns [Some y] if there is an element [x] such
93-
that [f x = Some y], else it returns [None] *)
101+
(** Alias to {!find_map}
102+
@deprecated since 1.3 *)
103+
104+
val find_map_i : (int -> 'a -> 'b option) -> 'a t -> 'b option
105+
(** Like {!find_map}, but also pass the index to the predicate function.
106+
@since 1.3 *)
94107

95108
val findi : (int -> 'a -> 'b option) -> 'a t -> 'b option
96-
(** Like {!find}, but also pass the index to the predicate function.
97-
@since 0.3.4 *)
109+
(** Alias to {!find_map_i}
110+
@since 0.3.4
111+
@deprecated since 1.3 *)
98112

99113
val find_idx : ('a -> bool) -> 'a t -> (int * 'a) option
100114
(** [find_idx p x] returns [Some (i,x)] where [x] is the [i]-th element of [l],
@@ -107,7 +121,7 @@ val lookup : ?cmp:'a ord -> 'a -> 'a t -> int option
107121
[Some i] ([i] the index of the key) otherwise *)
108122

109123
val lookup_exn : ?cmp:'a ord -> 'a -> 'a t -> int
110-
(** Same as {!lookup_exn}, but
124+
(** Same as {!lookup}, but
111125
@raise Not_found if the key is not present *)
112126

113127
val bsearch : ?cmp:('a -> 'a -> int) -> 'a -> 'a t ->

src/CCArray_slice.mli

+12-9
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,23 @@ val sorted : ('a -> 'a -> int) -> 'a t -> 'a array
8686

8787
val sort_indices : ('a -> 'a -> int) -> 'a t -> int array
8888
(** [sort_indices cmp a] returns a new array [b], with the same length as [a],
89-
such that [b.(i)] is the index of the [i]-th element of [a] in [sort cmp a].
90-
In other words, [map (fun i -> a.(i)) (sort_indices a) = sorted cmp a].
91-
[a] is not modified.
89+
such that [b.(i)] is the index at which the [i]-th element of [sorted cmp a]
90+
appears in [a]. [a] is not modified.
91+
92+
In other words, [map (fun i -> a.(i)) (sort_indices cmp a) = sorted cmp a].
93+
[sort_indices] yields the inverse permutation of {!sort_ranking}.
94+
9295
@since 1.0 *)
9396

9497
val sort_ranking : ('a -> 'a -> int) -> 'a t -> int array
9598
(** [sort_ranking cmp a] returns a new array [b], with the same length as [a],
96-
such that [b.(i)] is the position in [sorted cmp a] of the [i]-th
97-
element of [a].
98-
[a] is not modified.
99+
such that [b.(i)] is the index at which the [i]-the element of [a] appears
100+
in [sorted cmp a]. [a] is not modified.
99101
100-
In other words, [map (fun i -> (sorted cmp a).(i)) (sort_ranking cmp a) = a].
102+
In other words, [map (fun i -> (sorted cmp a).(i)) (sort_ranking cmp a) = a].
103+
[sort_ranking] yields the inverse permutation of {!sort_indices}.
101104
102-
Without duplicates, we also have
105+
In the absence of duplicate elements in [a], we also have
103106
[lookup_exn a.(i) (sorted a) = (sorted_ranking a).(i)]
104107
@since 1.0 *)
105108

@@ -122,7 +125,7 @@ val lookup : ?cmp:'a ord -> 'a -> 'a t -> int option
122125
[Some i] ([i] the index of the key) otherwise *)
123126

124127
val lookup_exn : ?cmp:'a ord -> 'a -> 'a t -> int
125-
(** Same as {!lookup_exn}, but
128+
(** Same as {!lookup}, but
126129
@raise Not_found if the key is not present *)
127130

128131
val bsearch : ?cmp:('a -> 'a -> int) -> 'a -> 'a t ->

src/CCBool.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ let equal (a:bool) b = a=b
77

88
let compare (a:bool) b = Pervasives.compare a b
99

10-
let negate x = not x
10+
let negate = not
1111

1212
type 'a printer = Format.formatter -> 'a -> unit
1313

src/CCBool.mli

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ val compare : t -> t -> int
1111
val equal : t -> t -> bool
1212

1313
val negate : t -> t
14-
(** Negation on booleans (functional version of [not]) *)
14+
(** Negation on booleans (functional version of [not])
15+
@deprecate since 1.3, simply use {!not} instead *)
1516

1617
type 'a printer = Format.formatter -> 'a -> unit
1718

src/CCFormat.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ let text out (s:string): unit =
7777
(*$= & ~printer:(fun s->CCFormat.sprintf "%S" s)
7878
"a\nb\nc" (sprintf_no_color "@[<v>%a@]%!" text "a b c")
7979
"a b\nc" (sprintf_no_color "@[<h>%a@]%!" text "a b\nc")
80-
*)
80+
*)
8181

8282
let list ?(sep=return ",@ ") pp fmt l =
8383
let rec pp_list l = match l with

src/CCFormat.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -311,5 +311,5 @@ module Dump : sig
311311
val result : 'a t -> ('a, string) Result.result t
312312
val result' : 'a t -> 'e t -> ('a, 'e) Result.result t
313313
val to_string : 'a t -> 'a -> string
314-
(** Alias to {!to_string} *)
314+
(** Alias to {!CCFormat.to_string} *)
315315
end

src/CCList.ml

+83-10
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ let combine l1 l2 =
366366
if List.length l1=List.length l2 \
367367
then CCList.combine l1 l2 = List.combine l1 l2 \
368368
else Q.assume_fail() )
369-
*)
369+
*)
370370

371371
let combine_gen l1 l2 =
372372
let l1 = ref l1 in
@@ -385,7 +385,38 @@ let combine_gen l1 l2 =
385385
let res1 = combine (take n l1) (take n l2) in \
386386
let res2 = combine_gen l1 l2 |> of_gen in \
387387
res1 = res2)
388-
*)
388+
*)
389+
390+
let split l =
391+
let rec direct i l = match l with
392+
| [] -> [], []
393+
| [x1, y1] -> [x1], [y1]
394+
| [x1, y1; x2, y2] -> [x1;x2], [y1;y2]
395+
| [x1, y1; x2, y2; x3, y3] -> [x1;x2;x3], [y1;y2;y3]
396+
| [x1, y1; x2, y2; x3, y3; x4, y4] -> [x1;x2;x3;x4], [y1;y2;y3;y4]
397+
| _ when i=0 -> split_slow [] [] l
398+
| (x1, y1) :: (x2, y2) :: (x3, y3) :: (x4, y4) :: (x5, y5) :: l' ->
399+
let rx, ry = direct (i-1) l' in
400+
x1 :: x2 :: x3 :: x4 :: x5 :: rx,
401+
y1 :: y2 :: y3 :: y4 :: y5 :: ry
402+
and split_slow acc1 acc2 l = match l with
403+
| [] -> List.rev acc1, List.rev acc2
404+
| (x1, x2) :: tail ->
405+
let acc1 = x1 :: acc1
406+
and acc2 = x2 :: acc2 in
407+
split_slow acc1 acc2 tail
408+
in
409+
direct direct_depth_default_ l
410+
411+
(*$Q
412+
(Q.(list_of_size Gen.(0--10_000) (pair small_int small_string))) (fun l -> \
413+
let (l1, l2) = split l in \
414+
List.length l1 = List.length l \
415+
&& List.length l2 = List.length l)
416+
417+
Q.(list_of_size Gen.(0--10_000) (pair small_int small_int)) (fun l -> \
418+
split l = List.split l)
419+
*)
389420

390421
let return x = [x]
391422

@@ -688,18 +719,18 @@ let take_while p l =
688719
*)
689720

690721
(*$Q
691-
Q.(pair (fun1 small_int bool) (list small_int)) (fun (f,l) -> \
692-
let l1 = take_while f l in \
693-
List.for_all f l1)
722+
Q.(pair (fun1 Observable.int bool) (list small_int)) (fun (f,l) -> \
723+
let l1 = take_while (Q.Fn.apply f) l in \
724+
List.for_all (Q.Fn.apply f) l1)
694725
*)
695726

696727
let rec drop_while p l = match l with
697728
| [] -> []
698729
| x :: l' -> if p x then drop_while p l' else l
699730

700731
(*$Q
701-
Q.(pair (fun1 small_int bool) (list small_int)) (fun (f,l) -> \
702-
take_while f l @ drop_while f l = l)
732+
Q.(pair (fun1 Observable.int bool) (list small_int)) (fun (f,l) -> \
733+
take_while (Q.Fn.apply f) l @ drop_while (Q.Fn.apply f) l = l)
703734
*)
704735

705736
let take_drop_while p l =
@@ -720,9 +751,9 @@ let take_drop_while p l =
720751
direct direct_depth_default_ p l
721752

722753
(*$Q
723-
Q.(pair (fun1 small_int bool) (list small_int)) (fun (f,l) -> \
724-
let l1,l2 = take_drop_while f l in \
725-
(l1 = take_while f l) && (l2 = drop_while f l))
754+
Q.(pair (fun1 Observable.int bool) (list small_int)) (fun (f,l) -> \
755+
let l1,l2 = take_drop_while (Q.Fn.apply f) l in \
756+
(l1 = take_while (Q.Fn.apply f) l) && (l2 = drop_while (Q.Fn.apply f) l))
726757
*)
727758

728759
let last n l =
@@ -811,6 +842,48 @@ let filter_map f l =
811842
[ 1; 2; 3; 4; 5; 6 ])
812843
*)
813844

845+
let keep_some l = filter_map (fun x->x) l
846+
847+
let keep_ok l =
848+
filter_map
849+
(function
850+
| Result.Ok x -> Some x
851+
| Result.Error _ -> None)
852+
l
853+
854+
let all_some l =
855+
try Some (map (function Some x -> x | None -> raise Exit) l)
856+
with Exit -> None
857+
858+
(*$=
859+
(Some []) (all_some [])
860+
(Some [1;2;3]) (all_some [Some 1; Some 2; Some 3])
861+
None (all_some [Some 1; None; None; Some 4])
862+
*)
863+
864+
let all_ok l =
865+
let err = ref None in
866+
try
867+
Result.Ok
868+
(map
869+
(function Result.Ok x -> x | Error e -> err := Some e; raise Exit)
870+
l)
871+
with Exit ->
872+
begin match !err with
873+
| None -> assert false
874+
| Some e -> Result.Error e
875+
end
876+
877+
(*$inject
878+
open Result
879+
*)
880+
881+
(*$=
882+
(Ok []) (all_ok [])
883+
(Ok [1;2;3]) (all_ok [Ok 1; Ok 2; Ok 3])
884+
(Error "e2") (all_ok [Ok 1; Error "e2"; Error "e3"; Ok 4])
885+
*)
886+
814887
let mem ?(eq=(=)) x l =
815888
let rec search eq x l = match l with
816889
| [] -> false

src/CCList.mli

+32-7
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ val combine_gen : 'a list -> 'b list -> ('a * 'b) gen
9292
instead, the output has as many pairs as the smallest input list.
9393
@since 1.2 *)
9494

95+
val split : ('a * 'b) t -> 'a t * 'b t
96+
(** A tail-recursive version of {!List.split}. *)
97+
9598
val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int
9699

97100
val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
@@ -111,13 +114,13 @@ val fold_product : ('c -> 'a -> 'b -> 'c) -> 'c -> 'a t -> 'b t -> 'c
111114
val cartesian_product : 'a t t -> 'a t t
112115
(**
113116
For example:
114-
{[
115-
# cartesian_product [[1;2];[3];[4;5;6]] =
116-
[[1;3;4];[1;3;5];[1;3;6];[2;3;4];[2;3;5];[2;3;6]];;
117-
# cartesian_product [[1;2];[];[4;5;6]] = [];;
118-
# cartesian_product [[1;2];[3];[4];[5];[6]] =
119-
[[1;3;4;5;6];[2;3;4;5;6]];;
120-
]}
117+
{[
118+
# cartesian_product [[1;2];[3];[4;5;6]] =
119+
[[1;3;4];[1;3;5];[1;3;6];[2;3;4];[2;3;5];[2;3;6]];;
120+
# cartesian_product [[1;2];[];[4;5;6]] = [];;
121+
# cartesian_product [[1;2];[3];[4];[5];[6]] =
122+
[[1;3;4;5;6];[2;3;4;5;6]];;
123+
]}
121124
invariant: [cartesian_product l = map_product id l].
122125
@since 1.2 *)
123126

@@ -247,6 +250,26 @@ val remove : ?eq:('a -> 'a -> bool) -> x:'a -> 'a t -> 'a t
247250
val filter_map : ('a -> 'b option) -> 'a t -> 'b t
248251
(** Map and remove elements at the same time *)
249252

253+
val keep_some : 'a option t -> 'a t
254+
(** [filter_some l] retains only elements of the form [Some x].
255+
Same as [filter_map CCFun.id]
256+
@since 1.3 *)
257+
258+
val keep_ok : ('a, _) Result.result t -> 'a t
259+
(** [filter_some l] retains only elements of the form [Some x].
260+
Same as [filter_map CCFun.id]
261+
@since 1.3 *)
262+
263+
val all_some : 'a option t -> 'a t option
264+
(** [all_some l] returns [Some l'] if all elements of [l] are of the form [Some x],
265+
or [None] otherwise.
266+
@since 1.3 *)
267+
268+
val all_ok : ('a, 'err) Result.result t -> ('a t, 'err) Result.result
269+
(** [all_ok l] returns [Ok l'] if all elements of [l] are of the form [Ok x],
270+
or [Error e] otherwise (with the first error met).
271+
@since 1.3 *)
272+
250273
val sorted_merge : ?cmp:('a -> 'a -> int) -> 'a list -> 'a list -> 'a list
251274
(** Merges elements from both sorted list *)
252275

@@ -498,3 +521,5 @@ end
498521

499522
val pp : ?start:string -> ?stop:string -> ?sep:string ->
500523
'a printer -> 'a t printer
524+
525+
(** {2 Lists of pairs} *)

src/CCResult.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ let fold_ok f acc r = match r with
129129
(*$=
130130
42 (fold_ok (+) 2 (Ok 40))
131131
40 (fold_ok (+) 40 (Error "foo"))
132-
*)
132+
*)
133133

134134
let is_ok = function
135135
| Ok _ -> true

0 commit comments

Comments
 (0)