From c1704d71ff121e62ab5c45d15005f549232ed1a6 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Tue, 29 Oct 2019 21:50:22 -0500 Subject: [PATCH] style: improve new code --- src/data/CCDeque.ml | 58 +++++++++++++++++++++++--------------------- src/data/CCDeque.mli | 3 +++ 2 files changed, 33 insertions(+), 28 deletions(-) diff --git a/src/data/CCDeque.ml b/src/data/CCDeque.ml index 45b8901a..065fcb64 100644 --- a/src/data/CCDeque.ml +++ b/src/data/CCDeque.ml @@ -249,24 +249,25 @@ let update_front d f = match d.cur.cell with | Zero -> () | One x -> - begin match f x with - | None -> if Stdlib.(!=) d.cur.prev d.cur then ( - d.cur.prev.next <- d.cur.next; - d.cur.next.prev <- d.cur.prev; - d.cur <- d.cur.next; - ) - else d.cur.cell <- Zero - | Some x -> d.cur.cell <- One x - end + begin match f x with + | None -> if Stdlib.(!=) d.cur.prev d.cur then ( + d.cur.prev.next <- d.cur.next; + d.cur.next.prev <- d.cur.prev; + d.cur <- d.cur.next; + ) + else d.cur.cell <- Zero + | Some x -> d.cur.cell <- One x + end | Two (x, y) -> - begin match f x with - | None -> d.cur.cell <- One (y) - | Some x -> d.cur.cell <- Two (x,y) - end + begin match f x with + | None -> d.cur.cell <- One (y) + | Some x -> d.cur.cell <- Two (x,y) + end | Three (x,y,z) -> - match f x with - | None -> d.cur.cell <- Two (y,z) - | Some x -> d.cur.cell <- Three (x,y,z) + begin match f x with + | None -> d.cur.cell <- Two (y,z) + | Some x -> d.cur.cell <- Three (x,y,z) + end (*$T update_front let q = of_list [1;2;3;4;5;6;7] in update_front q (fun _ -> None); to_list q = [2;3;4;5;6;7] @@ -290,20 +291,21 @@ let update_back d f = match n.cell with | Zero -> () | One x -> - begin match f x with - | None -> if Stdlib.(!=) d.cur.prev d.cur then remove_node_ n - else n.cell <- Zero - | Some x -> n.cell <- One x - end + begin match f x with + | None -> if Stdlib.(!=) d.cur.prev d.cur then remove_node_ n + else n.cell <- Zero + | Some x -> n.cell <- One x + end | Two (x, y) -> - begin match f y with - | None -> n.cell <- One (x) - | Some y -> n.cell <- Two (x,y) - end + begin match f y with + | None -> n.cell <- One (x) + | Some y -> n.cell <- Two (x,y) + end | Three (x,y,z) -> - match f z with - | None -> n.cell <- Two (x,y) - | Some z -> n.cell <- Three (x,y,z) + begin match f z with + | None -> n.cell <- Two (x,y) + | Some z -> n.cell <- Three (x,y,z) + end (*$T update_back let q = of_list [1;2;3;4;5;6;7] in update_back q (fun _ -> None); to_list q = [1;2;3;4;5;6] diff --git a/src/data/CCDeque.mli b/src/data/CCDeque.mli index beb34b47..3acec9a7 100644 --- a/src/data/CCDeque.mli +++ b/src/data/CCDeque.mli @@ -83,10 +83,13 @@ val take_front_opt : 'a t -> 'a option val update_back : 'a t -> ('a -> 'a option) -> unit (** Update last value. If the deque is empty do nothing. + If the function returns [None], remove last element; + if it returns [Some x], replace last element with [x]. @since NEXT_RELEASE *) val update_front : 'a t -> ('a -> 'a option) -> unit (** Update first value. If the deque is empty do nothing. + Similar to {!update_back} but for the first value. @since NEXT_RELEASE *) val append_front : into:'a t -> 'a t -> unit