From ed5a602f4e39af07c027e6072c689b0a5afef758 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Mon, 7 Jul 2014 13:57:20 +0200 Subject: [PATCH] add monadic operators and >|= --- sequence.ml | 8 +++++++- sequence.mli | 10 ++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/sequence.ml b/sequence.ml index f5f3d6f..b0f48ef 100644 --- a/sequence.ml +++ b/sequence.ml @@ -48,7 +48,9 @@ let from_fun f = let empty = fun k -> () -let singleton x = fun k -> k x +let singleton x k = k x + +let return x k = k x (** Infinite sequence of the same element *) let repeat x = fun k -> while true do k x done @@ -695,6 +697,10 @@ module Infix = struct let (--) i j = int_range ~start:i ~stop:j let (--^) i j = int_range_dec ~start:i ~stop:j + + let (>>=) x f = flat_map f x + + let (>|=) x f = map f x end include Infix diff --git a/sequence.mli b/sequence.mli index 6416be8..f5b3781 100644 --- a/sequence.mli +++ b/sequence.mli @@ -76,6 +76,9 @@ val empty : 'a t val singleton : 'a -> 'a t (** Singleton sequence, with exactly one element. *) +val return : 'a -> 'a t + (** Synonym to {!singleton} *) + val repeat : 'a -> 'a t (** Infinite sequence of the same element. You may want to look at {!take} if you iterate on it. *) @@ -433,10 +436,17 @@ module Infix : sig (** [a --^ b] is the range of integers from [b] to [a], both included, in decreasing order (starts from [a]). It will therefore be empty if [a < b]. *) + + val (>>=) : 'a t -> ('a -> 'b t) -> 'b t + (** Monadic bind (infix version of {!flat_map} *) + + val (>|=) : 'a t -> ('a -> 'b) -> 'b t + (** Infix version of {!map} *) end include module type of Infix + (** {2 Pretty printing of sequences} *) val pp_seq : ?sep:string -> (Format.formatter -> 'a -> unit) ->