From 937c2e590280ceb146fc1a380f2f5a44b5c14a48 Mon Sep 17 00:00:00 2001 From: Calascibetta Romain Date: Sun, 21 Jul 2019 12:57:13 +0200 Subject: [PATCH 1/2] Add Fke.rev_iter --- lib/fke.ml | 15 +++++++++++++++ lib/sigs.ml | 2 ++ 2 files changed, 17 insertions(+) diff --git a/lib/fke.ml b/lib/fke.ml index cd8e391..ea7a64f 100644 --- a/lib/fke.ml +++ b/lib/fke.ml @@ -124,6 +124,21 @@ let iter : type a. (a -> unit) -> a t -> unit = in go f q +let rev_iter : type a. (a -> unit) -> a t -> unit = + fun f q -> + let rec go : type a. (a -> unit) -> a t -> unit = + fun f -> function + | Shallow Zero -> () + | Shallow (One x) -> f x + | Shallow (Two (y, x)) -> f x; f y + | Shallow (Three (z, y, x)) -> f x ; f y ; f z + | Deep {f= hd; m= (lazy q); r= tl; _} -> + go f (Shallow tl) ; + go (fun (y, x) -> f x; f y) q ; + go f (Shallow hd) + in + go f q + let fold : type acc x. (acc -> x -> acc) -> acc -> x t -> acc = fun f a q -> let rec go : type acc x. (acc -> x -> acc) -> acc -> x t -> acc = diff --git a/lib/sigs.ml b/lib/sigs.ml index cec5e79..f32e113 100644 --- a/lib/sigs.ml +++ b/lib/sigs.ml @@ -41,6 +41,8 @@ module type F = sig recently entered to the most recently entered. The queue itself is unchanged. *) + val rev_iter : ('a -> unit) -> 'a t -> unit + val fold : ('acc -> 'x -> 'acc) -> 'acc -> 'x t -> 'acc (** [fold f a q] is equivalent to [List.fold_left f a l], where [l] is the list of [q]'s elements. The queue remains unchanged. *) From 70f4391c147605add6d8bb8861d6853576db42e4 Mon Sep 17 00:00:00 2001 From: Calascibetta Romain Date: Mon, 22 Jul 2019 22:24:11 +0200 Subject: [PATCH 2/2] Add documentation --- lib/sigs.ml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/sigs.ml b/lib/sigs.ml index f32e113..18883e8 100644 --- a/lib/sigs.ml +++ b/lib/sigs.ml @@ -34,7 +34,10 @@ module type F = sig (** Same as {!pop} but it raises an exception if [q] is empty. *) val tail : 'a t -> ('a t * 'a) option + (** Get and remove the {b last} element. If [q] is empty, it returns [None]. *) + val tail_exn : 'a t -> 'a t * 'a + (** Same as {!tail} but it raises an exception if [q] is empty. *) val iter : ('a -> unit) -> 'a t -> unit (** [iter f q] applies [f] in turn to all elements of [q], from the least @@ -42,6 +45,9 @@ module type F = sig unchanged. *) val rev_iter : ('a -> unit) -> 'a t -> unit + (** [rev_iter f q] applies [f] in turn to all elements of [q], from the most + recently entered to the least recently entered. The queue itself is + unchanged. *) val fold : ('acc -> 'x -> 'acc) -> 'acc -> 'x t -> 'acc (** [fold f a q] is equivalent to [List.fold_left f a l], where [l] is the