From ffe157a1c6954f2becbbef32e680232015bd0274 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Thu, 2 Feb 2017 21:29:32 +0100 Subject: [PATCH] add `{max_exn,min_exn}` --- src/Sequence.ml | 13 +++++++++++++ src/Sequence.mli | 10 ++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/Sequence.ml b/src/Sequence.ml index d36221d..af6ff4e 100644 --- a/src/Sequence.ml +++ b/src/Sequence.ml @@ -554,6 +554,10 @@ let max ?(lt=fun x y -> x < y) seq = | Some y -> if lt y x then ret := Some x); !ret +let max_exn ?lt seq = match max ?lt seq with + | Some x -> x + | None -> raise Not_found + let min ?(lt=fun x y -> x < y) seq = let ret = ref None in seq @@ -562,6 +566,15 @@ let min ?(lt=fun x y -> x < y) seq = | Some y -> if lt x y then ret := Some x); !ret +let min_exn ?lt seq = match min ?lt seq with + | Some x -> x + | None -> raise Not_found + +(*$= & ~printer:string_of_int + 100 (0 -- 100 |> max_exn ?lt:None) + 0 (0 -- 100 |> min_exn ?lt:None) +*) + exception ExitHead let head seq = diff --git a/src/Sequence.mli b/src/Sequence.mli index 872289d..d52cdac 100644 --- a/src/Sequence.mli +++ b/src/Sequence.mli @@ -290,10 +290,20 @@ val max : ?lt:('a -> 'a -> bool) -> 'a t -> 'a option @return None if the sequence is empty, Some [m] where [m] is the maximal element otherwise *) +val max_exn : ?lt:('a -> 'a -> bool) -> 'a t -> 'a +(** Unsafe version of {!max} + @raise Not_found if the sequence is empty + @since NEXT_RELEASE *) + val min : ?lt:('a -> 'a -> bool) -> 'a t -> 'a option (** Min element of the sequence, using the given comparison function. see {!max} for more details. *) +val min_exn : ?lt:('a -> 'a -> bool) -> 'a t -> 'a +(** Unsafe version of {!min} + @raise Not_found if the sequence is empty + @since NEXT_RELEASE *) + val head : 'a t -> 'a option (** First element, if any, otherwise [None] @since 0.5.1 *)