Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/core/CCList.ml
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,20 @@ let hd_tl = function

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 =
if n < 1 then invalid_arg "sublists_of_len: n must be > 0";
let offset =
Expand Down
6 changes: 6 additions & 0 deletions src/core/CCList.mli
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,12 @@ val take_drop_while : ('a -> bool) -> 'a t -> 'a t * 'a t
@since 1.2, but only
@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
(** [last n l] takes the last [n] elements of [l] (or less if
[l] doesn't have that many elements). *)
Expand Down
6 changes: 6 additions & 0 deletions src/core/CCListLabels.mli
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,12 @@ val take_drop_while : f:('a -> bool) -> 'a t -> 'a t * 'a t
@since 1.2, but only
@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
(** [last n l] takes the last [n] elements of [l] (or less if
[l] doesn't have that many elements). *)
Expand Down
7 changes: 7 additions & 0 deletions tests/core/t_list.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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) (1 -- 10) = 1 -- 10;;
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.(pair (fun1 Observable.int bool) (list small_int))
Expand Down