From 4b30aba080c5ab2f36f7b212e42f6f3069372a07 Mon Sep 17 00:00:00 2001 From: Jacques-Pascal Deplaix Date: Sat, 23 Dec 2017 15:25:00 +0000 Subject: [PATCH] Allow negative indexes in CCList.remove_at_idx --- src/core/CCList.ml | 9 ++++++++- src/core/CCList.mli | 4 +++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/core/CCList.ml b/src/core/CCList.ml index 290dbaa5..bdfddf68 100644 --- a/src/core/CCList.ml +++ b/src/core/CCList.ml @@ -1111,12 +1111,19 @@ let remove_at_idx i l0 = | y::l' -> aux l' (y::acc) (i-1) in - aux l0 [] i + if i < 0 then + aux l0 [] (List.length l0 + i) + else + aux l0 [] i (*$T remove_at_idx 0 [1;2;3;4] = [2;3;4] remove_at_idx 3 [1;2;3;4] = [1;2;3] remove_at_idx 5 [1;2;3;4] = [1;2;3;4] + remove_at_idx (-1) [1;2;3;4] = [1;2;3] + remove_at_idx (-2) [1;2;3;4] = [1;2;4] + remove_at_idx (-3) [1;2;3;4] = [1;3;4] + remove_at_idx (-4) [1;2;3;4] = [2;3;4] *) let range_by ~step i j = diff --git a/src/core/CCList.mli b/src/core/CCList.mli index cb5a379b..c380419f 100644 --- a/src/core/CCList.mli +++ b/src/core/CCList.mli @@ -359,7 +359,9 @@ val insert_at_idx : int -> 'a -> 'a t -> 'a t val remove_at_idx : int -> 'a t -> 'a t (** Remove element at given index. Does nothing if the index is - too high. *) + too high. + If the index is negative, it will remove element starting from the end + of the list. *) (** {2 Set Operators}