From b0d92f44ea8cacce3817957ec40cd4d08a27a7f3 Mon Sep 17 00:00:00 2001 From: nathan moreau Date: Fri, 26 Jan 2018 09:25:17 +0100 Subject: [PATCH] Adding CCList.tail_opt --- src/core/CCList.ml | 7 +++++++ src/core/CCList.mli | 4 ++++ src/core/CCListLabels.mli | 4 ++++ 3 files changed, 15 insertions(+) diff --git a/src/core/CCList.ml b/src/core/CCList.ml index 064070d8..6f32f47e 100644 --- a/src/core/CCList.ml +++ b/src/core/CCList.ml @@ -855,6 +855,10 @@ let head_opt = function | [] -> None | x::_ -> Some x +let tail_opt = function + | [] -> None + | _ :: tail -> Some tail + let rec last_opt = function | [] -> None | [x] -> Some x @@ -864,6 +868,9 @@ let rec last_opt = function (Some 1) (head_opt [1;2;3]) (Some 1) (head_opt [1]) None (head_opt []) + (Some [2;3]) (tail_opt [1;2;3]) + (Some []) (tail_opt [1]) + None (tail_opt []) (Some 3) (last_opt [1;2;3]) (Some 1) (last_opt [1]) None (last_opt []) diff --git a/src/core/CCList.mli b/src/core/CCList.mli index 8dba2044..d97a5888 100644 --- a/src/core/CCList.mli +++ b/src/core/CCList.mli @@ -249,6 +249,10 @@ val head_opt : 'a t -> 'a option (** First element. @since 0.20 *) +val tail_opt : 'a t -> 'a t option +(** Return the given list without its first element. + @since NEXT_RELEASE *) + val last_opt : 'a t -> 'a option (** Last element. @since 0.20 *) diff --git a/src/core/CCListLabels.mli b/src/core/CCListLabels.mli index 66f477c0..995c3be7 100644 --- a/src/core/CCListLabels.mli +++ b/src/core/CCListLabels.mli @@ -159,6 +159,10 @@ val head_opt : 'a t -> 'a option (** First element. @since 0.20 *) +val tail_opt : 'a t -> 'a t option +(** Return the given list without its first element. + @since NEXT_RELEASE *) + val last_opt : 'a t -> 'a option (** Last element. @since 0.20 *)