diff --git a/src/data/CCRAL.ml b/src/data/CCRAL.ml index 812c721e..0378e9e4 100644 --- a/src/data/CCRAL.ml +++ b/src/data/CCRAL.ml @@ -392,6 +392,41 @@ let equal ?(eq=(=)) l1 l2 = equal (of_list l1) (of_list l2) = (l1=l2)) *) +(** {2 Utils} *) + +let make n x = + let rec aux n acc x = + if n<=0 then acc else aux (n-1) (cons x acc) x + in + aux n empty x + +let repeat n l = + let rec aux n l acc = + if n<=0 then acc else aux (n-1) l (append l acc) + in + aux n l empty + +let range i j = + let rec aux i j acc = + if i=j then cons i acc + else if i to_list = [0;1;2;3] + range 3 0 |> to_list = [3;2;1;0] + range 17 17 |> to_list = [17] +*) + +(*$Q + Q.(pair small_int small_int) (fun (i,j) -> \ + range i j |> to_list = CCList.(i -- j) ) +*) + (** {2 Conversions} *) type 'a sequence = ('a -> unit) -> unit @@ -516,10 +551,10 @@ let compare ?(cmp=Pervasives.compare) l1 l2 = module Infix = struct let (@+) = cons - let (>>=) l f = flat_map f l let (>|=) l f = map f l let (<*>) = app + let (--) = range end include Infix diff --git a/src/data/CCRAL.mli b/src/data/CCRAL.mli index 68fbb7eb..e5c5b56a 100644 --- a/src/data/CCRAL.mli +++ b/src/data/CCRAL.mli @@ -115,6 +115,16 @@ val equal : ?eq:('a -> 'a -> bool) -> 'a t -> 'a t -> bool val compare : ?cmp:('a -> 'a -> int) -> 'a t -> 'a t -> int (** Lexicographic comparison *) +(** {2 Utils} *) + +val make : int -> 'a -> 'a t + +val repeat : int -> 'a t -> 'a t +(** [repeat n l] is [append l (append l ... l)] [n] times *) + +val range : int -> int -> int t +(** [range i j] is [i; i+1; ... ; j] or [j; j-1; ...; i] *) + (** {2 Conversions} *) type 'a sequence = ('a -> unit) -> unit @@ -164,6 +174,9 @@ module Infix : sig val (<*>) : ('a -> 'b) t -> 'a t -> 'b t (** Alias to {!app} *) + + val (--) : int -> int -> int t + (** Alias to {!range} *) end include module type of Infix