add {max_exn,min_exn}

This commit is contained in:
Simon Cruanes 2017-02-02 21:29:32 +01:00
parent 12743ab24f
commit ffe157a1c6
2 changed files with 23 additions and 0 deletions

View file

@ -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 =

View file

@ -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 *)