diff --git a/sledge/nonstdlib/array.ml b/sledge/nonstdlib/array.ml index afc97c784..498d92fba 100644 --- a/sledge/nonstdlib/array.ml +++ b/sledge/nonstdlib/array.ml @@ -69,6 +69,7 @@ let combine_exn xs ys = let combine xs ys = try Some (combine_exn xs ys) with Invalid_argument _ -> None +let mem x xs ~eq = mem ~eq x xs let iter xs ~f = iter ~f xs let iteri xs ~f = iteri ~f xs let exists xs ~f = exists ~f xs diff --git a/sledge/nonstdlib/array.mli b/sledge/nonstdlib/array.mli index a62e3136d..66d4effba 100644 --- a/sledge/nonstdlib/array.mli +++ b/sledge/nonstdlib/array.mli @@ -23,6 +23,7 @@ val split : ('a * 'b) t -> 'a t * 'b t val combine : 'a t -> 'b t -> ('a * 'b) t option val combine_exn : 'a t -> 'b t -> ('a * 'b) t val is_empty : 'a t -> bool +val mem : 'a -> 'a t -> eq:('a -> 'a -> bool) -> bool val iter : 'a t -> f:('a -> unit) -> unit val iteri : 'a t -> f:(int -> 'a -> unit) -> unit val exists : 'a t -> f:('a -> bool) -> bool diff --git a/sledge/nonstdlib/iter.ml b/sledge/nonstdlib/iter.ml index e3293fd8b..bc9b7cc32 100644 --- a/sledge/nonstdlib/iter.ml +++ b/sledge/nonstdlib/iter.ml @@ -11,6 +11,26 @@ module Import = struct type 'a iter = 'a t end +let mem elt seq ~eq = mem ~eq ~x:elt seq +let sort seq ~cmp = sort ~cmp seq +let sort_uniq seq ~cmp = sort_uniq ~cmp seq +let sorted seq ~cmp = sorted ~cmp seq +let group_succ_by seq ~eq = group_succ_by ~eq seq +let group_by seq ~hash ~eq = group_by ~hash ~eq seq +let count seq ~hash ~eq = count ~hash ~eq seq +let uniq seq ~eq = uniq ~eq seq +let join_by ~eq ~hash k1 k2 ~merge = join_by ~eq ~hash k1 k2 ~merge +let join_all_by ~eq ~hash k1 k2 ~merge = join_all_by ~eq ~hash k1 k2 ~merge +let group_join_by ~eq ~hash = group_join_by ~eq ~hash +let inter ~eq ~hash = inter ~eq ~hash +let union ~eq ~hash = union ~eq ~hash +let diff ~eq ~hash = diff ~eq ~hash +let subset ~eq ~hash = subset ~eq ~hash +let max seq ~lt = max ~lt seq +let max_exn seq ~lt = max_exn ~lt seq +let min seq ~lt = min ~lt seq +let min_exn seq ~lt = min_exn ~lt seq + let pop seq = match head seq with Some x -> Some (x, drop 1 seq) | None -> None diff --git a/sledge/nonstdlib/iter.mli b/sledge/nonstdlib/iter.mli index 8f74134a2..0968d6fb1 100644 --- a/sledge/nonstdlib/iter.mli +++ b/sledge/nonstdlib/iter.mli @@ -11,6 +11,56 @@ module Import : sig type 'a iter = 'a t end +val mem : 'a -> 'a t -> eq:('a -> 'a -> bool) -> bool +val sort : 'a t -> cmp:('a -> 'a -> int) -> 'a t +val sort_uniq : 'a t -> cmp:('a -> 'a -> int) -> 'a t +val sorted : 'a t -> cmp:('a -> 'a -> int) -> bool +val group_succ_by : 'a t -> eq:('a -> 'a -> bool) -> 'a list t + +val group_by : + 'a t -> hash:('a -> int) -> eq:('a -> 'a -> bool) -> 'a list t + +val count : + 'a t -> hash:('a -> int) -> eq:('a -> 'a -> bool) -> ('a * int) t + +val uniq : 'a t -> eq:('a -> 'a -> bool) -> 'a t + +val join_by : + eq:'key equal + -> hash:'key hash + -> ('a -> 'key) + -> ('b -> 'key) + -> merge:('key -> 'a -> 'b -> 'c option) + -> 'a t + -> 'b t + -> 'c t + +val join_all_by : + eq:'key equal + -> hash:'key hash + -> ('a -> 'key) + -> ('b -> 'key) + -> merge:('key -> 'a list -> 'b list -> 'c option) + -> 'a t + -> 'b t + -> 'c t + +val group_join_by : + eq:'a equal + -> hash:'a hash + -> ('b -> 'a) + -> 'a t + -> 'b t + -> ('a * 'b list) t + +val inter : eq:'a equal -> hash:'a hash -> 'a t -> 'a t -> 'a t +val union : eq:'a equal -> hash:'a hash -> 'a t -> 'a t -> 'a t +val diff : eq:'a equal -> hash:'a hash -> 'a t -> 'a t -> 'a t +val subset : eq:'a equal -> hash:'a hash -> 'a t -> 'a t -> bool +val max : 'a t -> lt:('a -> 'a -> bool) -> 'a option +val max_exn : 'a t -> lt:('a -> 'a -> bool) -> 'a +val min : 'a t -> lt:('a -> 'a -> bool) -> 'a option +val min_exn : 'a t -> lt:('a -> 'a -> bool) -> 'a val pop : 'a iter -> ('a * 'a iter) option val find : 'a t -> f:('a -> bool) -> 'a option val find_exn : 'a t -> f:('a -> bool) -> 'a diff --git a/sledge/nonstdlib/list.ml b/sledge/nonstdlib/list.ml index cf2b2417a..e6cba4977 100644 --- a/sledge/nonstdlib/list.ml +++ b/sledge/nonstdlib/list.ml @@ -15,6 +15,7 @@ let hd = function [] -> None | hd :: _ -> Some hd let tl_exn = tl let tl = function [] -> None | _ :: tl -> Some tl let pop_exn = function x :: xs -> (x, xs) | [] -> raise Not_found +let mem elt seq ~eq = mem ~eq elt seq let exists xs ~f = exists ~f xs let for_all xs ~f = for_all ~f xs let find_exn xs ~f = find ~f xs @@ -46,6 +47,10 @@ let combine_exn = combine let combine xs ys = try Some (combine_exn xs ys) with Invalid_argument _ -> None +let group_by seq ~hash ~eq = group_by ~hash ~eq seq +let join_by ~eq ~hash k1 k2 ~merge = join_by ~eq ~hash k1 k2 ~merge +let join_all_by ~eq ~hash k1 k2 ~merge = join_all_by ~eq ~hash k1 k2 ~merge +let group_join_by ~eq ~hash = group_join_by ~eq ~hash let fold xs ~init ~f = fold_left ~f ~init xs let reduce xs ~f = @@ -84,3 +89,11 @@ let pp_diff ~cmp sep pp_elt fs (xs, ys) = | Right y -> Format.fprintf fs "++ %a" pp_elt y in pp sep pp_diff_elt fs (symmetric_diff ~cmp xs ys) + +module Assoc = struct + include Assoc + + let mem elt seq ~eq = mem ~eq elt seq +end + +let mem_assoc elt seq ~eq = mem_assoc ~eq elt seq diff --git a/sledge/nonstdlib/list.mli b/sledge/nonstdlib/list.mli index 27f2fa8bf..693585bb8 100644 --- a/sledge/nonstdlib/list.mli +++ b/sledge/nonstdlib/list.mli @@ -29,6 +29,7 @@ val hd_exn : 'a t -> 'a val tl : 'a t -> 'a t option val tl_exn : 'a t -> 'a t val pop_exn : 'a list -> 'a * 'a list +val mem : 'a -> 'a t -> eq:('a -> 'a -> bool) -> bool val exists : 'a t -> f:('a -> bool) -> bool val for_all : 'a t -> f:('a -> bool) -> bool val find : 'a t -> f:('a -> bool) -> 'a option @@ -53,9 +54,49 @@ val rev_map_split : 'a t -> f:('a -> 'b * 'c) -> 'b list * 'c list val combine : 'a t -> 'b t -> ('a * 'b) t option val combine_exn : 'a t -> 'b t -> ('a * 'b) t + +val group_by : + 'a t -> hash:('a -> int) -> eq:('a -> 'a -> bool) -> 'a list t + +val join_by : + eq:('key -> 'key -> bool) + -> hash:('key -> int) + -> ('a -> 'key) + -> ('b -> 'key) + -> merge:('key -> 'a -> 'b -> 'c option) + -> 'a t + -> 'b t + -> 'c t + +val join_all_by : + eq:('key -> 'key -> bool) + -> hash:('key -> int) + -> ('a -> 'key) + -> ('b -> 'key) + -> merge:('key -> 'a list -> 'b list -> 'c option) + -> 'a t + -> 'b t + -> 'c t + +val group_join_by : + eq:('a -> 'a -> bool) + -> hash:('a -> int) + -> ('b -> 'a) + -> 'a t + -> 'b t + -> ('a * 'b list) t + val fold : 'a list -> init:'s -> f:('s -> 'a -> 's) -> 's val reduce : 'a t -> f:('a -> 'a -> 'a) -> 'a option val fold2_exn : 'a t -> 'b t -> init:'s -> f:('s -> 'a -> 'b -> 's) -> 's val symmetric_diff : cmp:('a -> 'a -> int) -> 'a t -> 'a t -> ('a, 'a) Either.t t + +module Assoc : sig + include module type of Assoc + + val mem : 'a -> ('a, _) t -> eq:('a -> 'a -> bool) -> bool +end + +val mem_assoc : 'a -> ('a * _) t -> eq:('a -> 'a -> bool) -> bool