From 61a4f64e75bdf0121b109728e923958b7bf65b9b Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Mon, 18 Nov 2013 15:10:07 +0100 Subject: [PATCH] decreasing int range operator --- sequence.ml | 10 ++++++++++ sequence.mli | 18 +++++++++++++++++- tests/test_sequence.ml | 8 ++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/sequence.ml b/sequence.ml index e5ddf09..f33ac57 100644 --- a/sequence.ml +++ b/sequence.ml @@ -541,6 +541,10 @@ let int_range ~start ~stop = fun k -> for i = start to stop do k i done +let int_range_dec ~start ~stop = + fun k -> + for i = start downto stop do k i done + (** Convert the given set to a sequence. The set module must be provided. *) let of_set (type s) (type v) m set = let module S = (val m : Set.S with type t = s and type elt = v) in @@ -681,6 +685,8 @@ end 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 = f x let (@@) a b = append a b @@ -688,6 +694,10 @@ module Infix = struct let (>>=) x f = flatMap f x end +let (--) = Infix.(--) + +let (--^) = Infix.(--^) + (** {2 Pretty printing of sequences} *) (** Pretty print a sequence of ['a], using the given pretty printer diff --git a/sequence.mli b/sequence.mli index a2c84e7..97aa638 100644 --- a/sequence.mli +++ b/sequence.mli @@ -318,7 +318,12 @@ val to_buffer : char t -> Buffer.t -> unit (** Copy content of the sequence into the buffer *) val int_range : start:int -> stop:int -> int t - (** Iterator on integers in [start...stop] by steps 1 *) + (** Iterator on integers in [start...stop] by steps 1. Also see + {!(--)} for an infix version. *) + +val int_range_dec : start:int -> stop:int -> int t + (** Iterator on decreasing integers in [stop...start] by steps -1. + See {!(--^)} for an infix version *) val of_set : (module Set.S with type elt = 'a and type t = 'b) -> 'b -> 'a t (** Convert the given set to a sequence. The set module must be provided. *) @@ -414,12 +419,23 @@ end module Infix : sig val (--) : int -> int -> int t + (** [a -- b] is the range of integers from [a] to [b], both included, + in increasing order. It will therefore be empty if [a > b]. *) + + val (--^) : int -> int -> int t + (** [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 -> ('a -> 'b) -> 'b val (@@) : 'a t -> 'a t -> 'a t end +val (--) : int -> int -> int t + +val (--^) : int -> int -> int t + (** {2 Pretty printing of sequences} *) val pp_seq : ?sep:string -> (Format.formatter -> 'a -> unit) -> diff --git a/tests/test_sequence.ml b/tests/test_sequence.ml index 9281e70..0b71ecf 100644 --- a/tests/test_sequence.ml +++ b/tests/test_sequence.ml @@ -182,6 +182,13 @@ let test_buff () = OUnit.assert_equal "DLROW OLLEH" (Buffer.contents b); () +let test_int_range () = + OUnit.assert_equal ~printer:pp_ilist [1;2;3;4] S.(to_list (1--4)); + OUnit.assert_equal ~printer:pp_ilist [10;9;8;7;6] S.(to_list (10 --^ 6)); + OUnit.assert_equal ~printer:pp_ilist [] S.(to_list (10--4)); + OUnit.assert_equal ~printer:pp_ilist [] S.(to_list (10 --^ 60)); + () + let suite = "test_sequence" >::: [ "test_empty" >:: test_empty; @@ -208,4 +215,5 @@ let suite = "test_rev" >:: test_rev; "test_unfoldr" >:: test_unfoldr; "test_hashtbl" >:: test_hashtbl; + "test_int_range" >:: test_int_range; ]