From 1e5560632350b31c3a51ecf63799ebd900bf2879 Mon Sep 17 00:00:00 2001 From: Jacques-Pascal Deplaix Date: Sun, 3 Dec 2017 16:29:56 +0000 Subject: [PATCH] Add List.remove_last --- src/core/CCList.ml | 11 +++++++++++ src/core/CCList.mli | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/src/core/CCList.ml b/src/core/CCList.ml index f7552587..8c3dbf11 100644 --- a/src/core/CCList.ml +++ b/src/core/CCList.ml @@ -1109,6 +1109,17 @@ let remove_at_idx i l0 = remove_at_idx 5 [1;2;3;4] = [1;2;3;4] *) +let rec remove_last = function + | [] | [_] -> [] + | x::xs -> x::remove_last xs + +(*$T + remove_last [] = [] + remove_last [1] = [] + remove_last [1; 2] = [1] + remove_last [1; 2; 3] = [1; 2] +*) + let range_by ~step i j = let rec range i j acc = if i=j then i::acc else range i (j-step) (j::acc) diff --git a/src/core/CCList.mli b/src/core/CCList.mli index f83407bf..eaca8c4d 100644 --- a/src/core/CCList.mli +++ b/src/core/CCList.mli @@ -261,6 +261,11 @@ val remove : ?eq:('a -> 'a -> bool) -> x:'a -> 'a t -> 'a t @param eq equality function @since 0.11 *) +val remove_last : 'a list -> 'a list +(** [remove_last l] removes the last element of [l]. If [l] is empty, + an empty list will be returned. + @since NEXT_RELEASE *) + val filter_map : ('a -> 'b option) -> 'a t -> 'b t (** Map and remove elements at the same time *)