mirror of
https://github.com/c-cube/iter.git
synced 2025-12-06 11:15:32 -05:00
decreasing int range operator
This commit is contained in:
parent
b54714957a
commit
61a4f64e75
3 changed files with 35 additions and 1 deletions
10
sequence.ml
10
sequence.ml
|
|
@ -541,6 +541,10 @@ let int_range ~start ~stop =
|
||||||
fun k ->
|
fun k ->
|
||||||
for i = start to stop do k i done
|
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. *)
|
(** Convert the given set to a sequence. The set module must be provided. *)
|
||||||
let of_set (type s) (type v) m set =
|
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
|
let module S = (val m : Set.S with type t = s and type elt = v) in
|
||||||
|
|
@ -681,6 +685,8 @@ end
|
||||||
module Infix = struct
|
module Infix = struct
|
||||||
let (--) i j = int_range ~start:i ~stop:j
|
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 (|>) x f = f x
|
||||||
|
|
||||||
let (@@) a b = append a b
|
let (@@) a b = append a b
|
||||||
|
|
@ -688,6 +694,10 @@ module Infix = struct
|
||||||
let (>>=) x f = flatMap f x
|
let (>>=) x f = flatMap f x
|
||||||
end
|
end
|
||||||
|
|
||||||
|
let (--) = Infix.(--)
|
||||||
|
|
||||||
|
let (--^) = Infix.(--^)
|
||||||
|
|
||||||
(** {2 Pretty printing of sequences} *)
|
(** {2 Pretty printing of sequences} *)
|
||||||
|
|
||||||
(** Pretty print a sequence of ['a], using the given pretty printer
|
(** Pretty print a sequence of ['a], using the given pretty printer
|
||||||
|
|
|
||||||
18
sequence.mli
18
sequence.mli
|
|
@ -318,7 +318,12 @@ val to_buffer : char t -> Buffer.t -> unit
|
||||||
(** Copy content of the sequence into the buffer *)
|
(** Copy content of the sequence into the buffer *)
|
||||||
|
|
||||||
val int_range : start:int -> stop:int -> int t
|
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
|
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. *)
|
(** Convert the given set to a sequence. The set module must be provided. *)
|
||||||
|
|
@ -414,12 +419,23 @@ end
|
||||||
|
|
||||||
module Infix : sig
|
module Infix : sig
|
||||||
val (--) : int -> int -> int t
|
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 -> ('a -> 'b) -> 'b
|
||||||
|
|
||||||
val (@@) : 'a t -> 'a t -> 'a t
|
val (@@) : 'a t -> 'a t -> 'a t
|
||||||
end
|
end
|
||||||
|
|
||||||
|
val (--) : int -> int -> int t
|
||||||
|
|
||||||
|
val (--^) : int -> int -> int t
|
||||||
|
|
||||||
(** {2 Pretty printing of sequences} *)
|
(** {2 Pretty printing of sequences} *)
|
||||||
|
|
||||||
val pp_seq : ?sep:string -> (Format.formatter -> 'a -> unit) ->
|
val pp_seq : ?sep:string -> (Format.formatter -> 'a -> unit) ->
|
||||||
|
|
|
||||||
|
|
@ -182,6 +182,13 @@ let test_buff () =
|
||||||
OUnit.assert_equal "DLROW OLLEH" (Buffer.contents b);
|
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 =
|
let suite =
|
||||||
"test_sequence" >:::
|
"test_sequence" >:::
|
||||||
[ "test_empty" >:: test_empty;
|
[ "test_empty" >:: test_empty;
|
||||||
|
|
@ -208,4 +215,5 @@ let suite =
|
||||||
"test_rev" >:: test_rev;
|
"test_rev" >:: test_rev;
|
||||||
"test_unfoldr" >:: test_unfoldr;
|
"test_unfoldr" >:: test_unfoldr;
|
||||||
"test_hashtbl" >:: test_hashtbl;
|
"test_hashtbl" >:: test_hashtbl;
|
||||||
|
"test_int_range" >:: test_int_range;
|
||||||
]
|
]
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue