From 4ff1853222c497aff1ca2735bf1cb609e3a792d2 Mon Sep 17 00:00:00 2001 From: Nicola Mometto Date: Fri, 19 Apr 2024 09:55:03 +0100 Subject: [PATCH] feat(CCList): add cons_when --- src/core/CCList.ml | 6 ++++++ src/core/CCList.mli | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/src/core/CCList.ml b/src/core/CCList.ml index e8b5be45..87c7fefc 100644 --- a/src/core/CCList.ml +++ b/src/core/CCList.ml @@ -151,6 +151,12 @@ let cons_maybe o l = | Some x -> x :: l | None -> l +let cons_when b x l = + if b then + x :: l + else + l + [@@@iflt 4.14] let direct_depth_filter_ = 10_000 diff --git a/src/core/CCList.mli b/src/core/CCList.mli index 865dca79..4f54dbb3 100644 --- a/src/core/CCList.mli +++ b/src/core/CCList.mli @@ -25,6 +25,11 @@ val cons_maybe : 'a option -> 'a t -> 'a t [cons_maybe None l] is [l]. @since 0.13 *) +val cons_when : bool -> 'a -> 'a t -> 'a t +(** [cons_when true x l] is [x :: l]. + [cons_when false x l] is [l]. + @since NEXT_RELEASE *) + val cons' : 'a t -> 'a -> 'a t (** [cons' l x] is the same as [x :: l]. This is convenient for fold functions such as {!List.fold_left} or {!Array.fold_left}.