add (--^) to CCRAl, CCFQueue, CCKlist (closes #56); add CCKList.Infix

This commit is contained in:
Simon Cruanes 2016-04-22 22:50:37 +02:00
parent 0485bc5cd9
commit bb74a33385
6 changed files with 77 additions and 0 deletions

View file

@ -485,6 +485,18 @@ let (--) a b =
0 -- 0 |> to_list = [0]
*)
let (--^) a b =
if a=b then empty
else if a<b then a -- (b-1)
else a -- (b+1)
(*$T
1 --^ 5 |> to_list = [1;2;3;4]
5 --^ 1 |> to_list = [5;4;3;2]
1 --^ 2 |> to_list = [1]
0 --^ 0 |> to_list = []
*)
let print pp_x out d =
let first = ref true in
Format.fprintf out "@[<hov2>queue {";

View file

@ -127,5 +127,9 @@ val (--) : int -> int -> int t
(** [a -- b] is the integer range from [a] to [b], both included.
@since 0.10 *)
val (--^) : int -> int -> int t
(** [a -- b] is the integer range from [a] to [b], where [b] is excluded.
@since NEXT_RELEASE *)
val print : 'a printer -> 'a t printer
(** @since 0.13 *)

View file

@ -426,6 +426,18 @@ let range i j =
range i j |> to_list = CCList.(i -- j) )
*)
let range_r_open_ i j =
if i=j then empty
else if i<j then range i (j-1)
else range i (j+1)
(*$= & ~printer:CCFormat.(to_string (hbox (list int)))
[1;2;3;4] (1 --^ 5 |> to_list)
[5;4;3;2] (5 --^ 1 |> to_list)
[1] (1 --^ 2 |> to_list)
[] (0 --^ 0 |> to_list)
*)
(** {2 Conversions} *)
type 'a sequence = ('a -> unit) -> unit
@ -554,6 +566,7 @@ module Infix = struct
let (>|=) l f = map ~f l
let (<*>) = app
let (--) = range
let (--^) = range_r_open_
end
include Infix

View file

@ -175,6 +175,10 @@ module Infix : sig
val (--) : int -> int -> int t
(** Alias to {!range} *)
val (--^) : int -> int -> int t
(** [a -- b] is the integer range from [a] to [b], where [b] is excluded.
@since NEXT_RELEASE *)
end
include module type of Infix

View file

@ -253,6 +253,18 @@ let range i j =
let (--) = range
let (--^) i j =
if i=j then empty
else if i<j then range i (j-1)
else range i (j+1)
(*$T
1 --^ 5 |> to_list = [1;2;3;4]
5 --^ 1 |> to_list = [5;4;3;2]
1 --^ 2 |> to_list = [1]
0 --^ 0 |> to_list = []
*)
let rec fold2 f acc l1 l2 = match l1(), l2() with
| `Nil, _
| _, `Nil -> acc
@ -475,6 +487,18 @@ let (<.>) f a = fair_app f a
|> to_list |> List.sort Pervasives.compare = [2; 3; 11; 30]
*)
(** {2 Infix} *)
module Infix = struct
let (>>=) = (>>=)
let (>|=) = (>|=)
let (<*>) = (<*>)
let (>>-) = (>>-)
let (<.>) = (<.>)
let (--) = (--)
let (--^) = (--^)
end
(** {2 Monadic Operations} *)
module type MONAD = sig
type 'a t

View file

@ -130,6 +130,12 @@ val flatten : 'a t t -> 'a t
val range : int -> int -> int t
val (--) : int -> int -> int t
(** [a -- b] is the range of integers containing
[a] and [b] (therefore, never empty) *)
val (--^) : int -> int -> int t
(** [a -- b] is the integer range from [a] to [b], where [b] is excluded.
@since NEXT_RELEASE *)
(** {2 Operations on two Collections} *)
@ -204,6 +210,20 @@ val (<.>) : ('a -> 'b) t -> 'a t -> 'b t
(** Infix version of {!fair_app}
@since 0.13 *)
(** {2 Infix operators}
@since NEXT_RELEASE *)
module Infix : sig
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t
val (>|=) : 'a t -> ('a -> 'b) -> 'b t
val (<*>) : ('a -> 'b) t -> 'a t -> 'b t
val (>>-) : 'a t -> ('a -> 'b t) -> 'b t
val (<.>) : ('a -> 'b) t -> 'a t -> 'b t
val (--) : int -> int -> int t
val (--^) : int -> int -> int t
end
(** {2 Monadic Operations} *)
module type MONAD = sig
type 'a t