diff --git a/src/iter/CCLazy_list.ml b/src/iter/CCLazy_list.ml index d4855bd9..3fc011ea 100644 --- a/src/iter/CCLazy_list.ml +++ b/src/iter/CCLazy_list.ml @@ -82,9 +82,21 @@ let rec flat_map ~f l = Lazy.force res ) +let default b a = + lazy ( + match a with + | lazy Nil -> Lazy.force b + | lazy a -> a + ) + +(*$= + [1] (default (return 1) empty |> to_list) +*) + module Infix = struct let (>|=) x f = map ~f x let (>>=) x f = flat_map ~f x + let (<|>) = default end include Infix diff --git a/src/iter/CCLazy_list.mli b/src/iter/CCLazy_list.mli index b6111661..06983522 100644 --- a/src/iter/CCLazy_list.mli +++ b/src/iter/CCLazy_list.mli @@ -45,9 +45,14 @@ val append : 'a t -> 'a t -> 'a t val flat_map : f:('a -> 'b t) -> 'a t -> 'b t (** Monadic flatten + map. *) +val default : 'a t -> 'a t -> 'a t +(** Choice operator. + @since NEXT_RELEASE *) + module Infix : sig val (>|=) : 'a t -> ('a -> 'b) -> 'b t val (>>=) : 'a t -> ('a -> 'b t) -> 'b t + val (<|>) : 'a t -> 'a t -> 'a t end include module type of Infix