From 1154021614181f2b62ced31cde8b9ac761f91312 Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Fri, 26 Jan 2024 20:35:57 +0100 Subject: refactor: make zippers return 'pop'ped elements --- lib/zipper.ml | 9 +++++++-- lib/zipper.mli | 26 +++++++------------------- 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/lib/zipper.ml b/lib/zipper.ml index 413f365..5f3b37a 100644 --- a/lib/zipper.ml +++ b/lib/zipper.ml @@ -49,7 +49,9 @@ let goto n z = let step = if n < 0 then left else right in Fn.apply_n_times ~n:(abs n) step z -let pop ?(n = 1) z = { z with after = Sequence.drop_eagerly z.after n } +let pop ?(n = 1) z = + let a, b = Sequence.split_n z.after n in + (Sequence.of_list a, { z with after = b }) let pop_before ?(n = 1) = let rec aux m z = @@ -58,7 +60,10 @@ let pop_before ?(n = 1) = aux 0 let pop_after ?(n = 1) z = - if right_length z < 2 then z else right z |> pop ~n |> left + if right_length z < 2 then (Sequence.empty, z) + else + let a, b = right z |> pop ~n in + (a, left b) let push x z = { z with after = Sequence.shift_right z.after x } let push_seq s z = { z with after = Sequence.append s z.after } diff --git a/lib/zipper.mli b/lib/zipper.mli index 0bbdf27..a300b12 100644 --- a/lib/zipper.mli +++ b/lib/zipper.mli @@ -106,30 +106,18 @@ val goto : int -> 'a zipper -> 'a zipper This involve [push]ing and [pop]ping elements before and after the cursor. *) -val pop : ?n:int -> 'a zipper -> 'a zipper +val pop : ?n:int -> 'a zipper -> 'a Sequence.t * 'a zipper (** Remove [n] elements at the cursor position (1 by default), if any, - and return the modified zipper. Calling [pop z], + and return them alongside the modified zipper. *) - - if [z] is [([3; 2; 1], [4; 5])], the result is [([3; 2; 1], [5])], - - if [z] is [([1; 2; 3], [])], the result is [([1; 2; 3], [])]. - *) - -val pop_before : ?n:int -> 'a zipper -> 'a zipper +val pop_before : ?n:int -> 'a zipper -> 'a Sequence.t * 'a zipper (** Remove [n] elements before the cursor (1 by default), if any, and - return the modified zipper. Calling [pop_before z], - - - if [z] is [([3; 2; 1], [4; 5])], the result is [([2; 1], [4, 5])], - - if [z] is [([], [1; 2; 3])], the result is [([], [1; 2; 3])]. - *) + return them alongside the modified zipper. *) -val pop_after : ?n:int -> 'a zipper -> 'a zipper +val pop_after : ?n:int -> 'a zipper -> 'a Sequence.t * 'a zipper (** Remove [n] elements after (and {b not} including) the cursor - (1 by default), if any, and return the modified zipper. - Calling [pop_after z], - - - if [z] is [([3; 2; 1], [4; 5])], the result is [([3; 2; 1], [4])], - - if [z] is [([1; 2; 3], [4])], the result is [([1; 2; 3], [4])]. - *) + (1 by default), if any, and return them alongside the modified + zipper. *) val push : 'a -> 'a zipper -> 'a zipper (** Insert an element at the cursor position. -- cgit v1.2.3