mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2026-01-28 11:54:51 -05:00
feat: add CCList.take_last
This commit is contained in:
parent
c1b13f1c7f
commit
fafb8aa0a8
4 changed files with 33 additions and 0 deletions
|
|
@ -823,6 +823,20 @@ let hd_tl = function
|
||||||
|
|
||||||
let take_drop n l = take n l, drop n l
|
let take_drop n l = take n l, drop n l
|
||||||
|
|
||||||
|
let rec take_last = function
|
||||||
|
| [] -> failwith "take_last"
|
||||||
|
| [ x ] -> [], x
|
||||||
|
| hd :: tl ->
|
||||||
|
let tl, lt = take_last tl in
|
||||||
|
hd :: tl, lt
|
||||||
|
|
||||||
|
let rec take_last_opt = function
|
||||||
|
| [] -> [], None
|
||||||
|
| [ x ] -> [], Some x
|
||||||
|
| hd :: tl ->
|
||||||
|
let tl, lt = take_last_opt tl in
|
||||||
|
hd :: tl, lt
|
||||||
|
|
||||||
let sublists_of_len ?(last = fun _ -> None) ?offset n l =
|
let sublists_of_len ?(last = fun _ -> None) ?offset n l =
|
||||||
if n < 1 then invalid_arg "sublists_of_len: n must be > 0";
|
if n < 1 then invalid_arg "sublists_of_len: n must be > 0";
|
||||||
let offset =
|
let offset =
|
||||||
|
|
|
||||||
|
|
@ -414,6 +414,12 @@ val take_drop_while : ('a -> bool) -> 'a t -> 'a t * 'a t
|
||||||
@since 1.2, but only
|
@since 1.2, but only
|
||||||
@since 2.2 with labels *)
|
@since 2.2 with labels *)
|
||||||
|
|
||||||
|
val take_last : 'a t -> 'a t * 'a
|
||||||
|
(** [take_last l] = (tl l, last l). *)
|
||||||
|
|
||||||
|
val take_last_opt : 'a t -> 'a t * 'a option
|
||||||
|
(** [take_last l] = (tl l, last_opt l). *)
|
||||||
|
|
||||||
val last : int -> 'a t -> 'a t
|
val last : int -> 'a t -> 'a t
|
||||||
(** [last n l] takes the last [n] elements of [l] (or less if
|
(** [last n l] takes the last [n] elements of [l] (or less if
|
||||||
[l] doesn't have that many elements). *)
|
[l] doesn't have that many elements). *)
|
||||||
|
|
|
||||||
|
|
@ -447,6 +447,12 @@ val take_drop_while : f:('a -> bool) -> 'a t -> 'a t * 'a t
|
||||||
@since 1.2, but only
|
@since 1.2, but only
|
||||||
@since 2.2 with labels *)
|
@since 2.2 with labels *)
|
||||||
|
|
||||||
|
val take_last : 'a t -> 'a t * 'a
|
||||||
|
(** [take_last l] = (tl l, last l). *)
|
||||||
|
|
||||||
|
val take_last_opt : 'a t -> 'a t * 'a option
|
||||||
|
(** [take_last l] = (tl l, last_opt l). *)
|
||||||
|
|
||||||
val last : int -> 'a t -> 'a t
|
val last : int -> 'a t -> 'a t
|
||||||
(** [last n l] takes the last [n] elements of [l] (or less if
|
(** [last n l] takes the last [n] elements of [l] (or less if
|
||||||
[l] doesn't have that many elements). *)
|
[l] doesn't have that many elements). *)
|
||||||
|
|
|
||||||
|
|
@ -807,6 +807,13 @@ t @@ fun () -> take_while (fun x -> x <> 0) [ 0; 1; 2; 3 ] = [];;
|
||||||
t @@ fun () -> take_while (fun _ -> true) [] = [];;
|
t @@ fun () -> take_while (fun _ -> true) [] = [];;
|
||||||
t @@ fun () -> take_while (fun _ -> true) (1 -- 10) = 1 -- 10;;
|
t @@ fun () -> take_while (fun _ -> true) (1 -- 10) = 1 -- 10;;
|
||||||
t @@ fun () -> take_while (fun _ -> true) (1 -- 300_000) = 1 -- 300_000;;
|
t @@ fun () -> take_while (fun _ -> true) (1 -- 300_000) = 1 -- 300_000;;
|
||||||
|
t @@ fun () -> take_last [ 1 ] = ([], 1);;
|
||||||
|
t @@ fun () -> take_last [ 1; 2; 3; 4; 5 ] = ([ 1; 2; 3; 4 ], 5);;
|
||||||
|
t @@ fun () -> take_last (1 -- 10_000) = (1 -- 9_999, 10_000);;
|
||||||
|
t @@ fun () -> take_last_opt [] = ([], None);;
|
||||||
|
t @@ fun () -> take_last_opt [ 1 ] = ([], Some 1);;
|
||||||
|
t @@ fun () -> take_last_opt [ 1; 2; 3; 4; 5 ] = ([ 1; 2; 3; 4 ], Some 5);;
|
||||||
|
t @@ fun () -> take_last_opt (1 -- 10_000) = (1 -- 9_999, Some 10_000);;
|
||||||
|
|
||||||
q
|
q
|
||||||
Q.(pair (fun1 Observable.int bool) (list small_int))
|
Q.(pair (fun1 Observable.int bool) (list small_int))
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue