mirror of
https://github.com/c-cube/iter.git
synced 2025-12-06 03:05:29 -05:00
add {max_exn,min_exn}
This commit is contained in:
parent
12743ab24f
commit
ffe157a1c6
2 changed files with 23 additions and 0 deletions
|
|
@ -554,6 +554,10 @@ let max ?(lt=fun x y -> x < y) seq =
|
||||||
| Some y -> if lt y x then ret := Some x);
|
| Some y -> if lt y x then ret := Some x);
|
||||||
!ret
|
!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 min ?(lt=fun x y -> x < y) seq =
|
||||||
let ret = ref None in
|
let ret = ref None in
|
||||||
seq
|
seq
|
||||||
|
|
@ -562,6 +566,15 @@ let min ?(lt=fun x y -> x < y) seq =
|
||||||
| Some y -> if lt x y then ret := Some x);
|
| Some y -> if lt x y then ret := Some x);
|
||||||
!ret
|
!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
|
exception ExitHead
|
||||||
|
|
||||||
let head seq =
|
let head seq =
|
||||||
|
|
|
||||||
|
|
@ -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
|
@return None if the sequence is empty, Some [m] where [m] is the maximal
|
||||||
element otherwise *)
|
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
|
val min : ?lt:('a -> 'a -> bool) -> 'a t -> 'a option
|
||||||
(** Min element of the sequence, using the given comparison function.
|
(** Min element of the sequence, using the given comparison function.
|
||||||
see {!max} for more details. *)
|
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
|
val head : 'a t -> 'a option
|
||||||
(** First element, if any, otherwise [None]
|
(** First element, if any, otherwise [None]
|
||||||
@since 0.5.1 *)
|
@since 0.5.1 *)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue