From 23e3544adce9254fec1ffa30f4f446fb903120cf Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Tue, 23 Feb 2016 15:34:26 +0100 Subject: [PATCH] add `CCList.hd_tl` --- src/core/CCList.ml | 9 +++++++++ src/core/CCList.mli | 5 +++++ 2 files changed, 14 insertions(+) diff --git a/src/core/CCList.ml b/src/core/CCList.ml index def7c977..bef9b423 100644 --- a/src/core/CCList.ml +++ b/src/core/CCList.ml @@ -454,6 +454,15 @@ let rec drop n l = match l with | _ when n=0 -> l | _::l' -> drop (n-1) l' +let hd_tl = function + | [] -> failwith "hd_tl" + | x :: l -> x, l + +(*$T + try ignore (hd_tl []); false with Failure _ -> true + hd_tl [1;2;3] = (1, [2;3]) +*) + let take_drop n l = take n l, drop n l let split = take_drop diff --git a/src/core/CCList.mli b/src/core/CCList.mli index b665c918..ef7e0d3d 100644 --- a/src/core/CCList.mli +++ b/src/core/CCList.mli @@ -106,6 +106,11 @@ val take : int -> 'a t -> 'a t val drop : int -> 'a t -> 'a t (** Drop the [n] first elements, keep the rest *) +val hd_tl : 'a t -> 'a * 'a t +(** [hd_tl (x :: l)] returns [hd, l]. + @raise Failure if the list is empty + @since NEXT_RELEASE *) + val take_drop : int -> 'a t -> 'a t * 'a t (** [take_drop n l] returns [l1, l2] such that [l1 @ l2 = l] and [length l1 = min (length l) n] *)