diff options
| author | Federico Igne <undyamon@disroot.org> | 2024-01-26 20:35:57 +0100 |
|---|---|---|
| committer | Federico Igne <undyamon@disroot.org> | 2024-01-26 20:35:57 +0100 |
| commit | 1154021614181f2b62ced31cde8b9ac761f91312 (patch) | |
| tree | 1f0cd0b52e21d0aaf7ebbd6d48d9abeb88cc356e /lib | |
| parent | 477dd3d2a9f452d06912f9f56c6eee831a15aead (diff) | |
| download | sandy-1154021614181f2b62ced31cde8b9ac761f91312.tar.gz sandy-1154021614181f2b62ced31cde8b9ac761f91312.zip | |
refactor: make zippers return 'pop'ped elements
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/zipper.ml | 9 | ||||
| -rw-r--r-- | 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 = | |||
| 49 | let step = if n < 0 then left else right in | 49 | let step = if n < 0 then left else right in |
| 50 | Fn.apply_n_times ~n:(abs n) step z | 50 | Fn.apply_n_times ~n:(abs n) step z |
| 51 | 51 | ||
| 52 | let pop ?(n = 1) z = { z with after = Sequence.drop_eagerly z.after n } | 52 | let pop ?(n = 1) z = |
| 53 | let a, b = Sequence.split_n z.after n in | ||
| 54 | (Sequence.of_list a, { z with after = b }) | ||
| 53 | 55 | ||
| 54 | let pop_before ?(n = 1) = | 56 | let pop_before ?(n = 1) = |
| 55 | let rec aux m z = | 57 | let rec aux m z = |
| @@ -58,7 +60,10 @@ let pop_before ?(n = 1) = | |||
| 58 | aux 0 | 60 | aux 0 |
| 59 | 61 | ||
| 60 | let pop_after ?(n = 1) z = | 62 | let pop_after ?(n = 1) z = |
| 61 | if right_length z < 2 then z else right z |> pop ~n |> left | 63 | if right_length z < 2 then (Sequence.empty, z) |
| 64 | else | ||
| 65 | let a, b = right z |> pop ~n in | ||
| 66 | (a, left b) | ||
| 62 | 67 | ||
| 63 | let push x z = { z with after = Sequence.shift_right z.after x } | 68 | let push x z = { z with after = Sequence.shift_right z.after x } |
| 64 | let push_seq s z = { z with after = Sequence.append s z.after } | 69 | 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 | |||
| 106 | This involve [push]ing and [pop]ping elements before and after the | 106 | This involve [push]ing and [pop]ping elements before and after the |
| 107 | cursor. *) | 107 | cursor. *) |
| 108 | 108 | ||
| 109 | val pop : ?n:int -> 'a zipper -> 'a zipper | 109 | val pop : ?n:int -> 'a zipper -> 'a Sequence.t * 'a zipper |
| 110 | (** Remove [n] elements at the cursor position (1 by default), if any, | 110 | (** Remove [n] elements at the cursor position (1 by default), if any, |
| 111 | and return the modified zipper. Calling [pop z], | 111 | and return them alongside the modified zipper. *) |
| 112 | 112 | ||
| 113 | - if [z] is [([3; 2; 1], [4; 5])], the result is [([3; 2; 1], [5])], | 113 | val pop_before : ?n:int -> 'a zipper -> 'a Sequence.t * 'a zipper |
| 114 | - if [z] is [([1; 2; 3], [])], the result is [([1; 2; 3], [])]. | ||
| 115 | *) | ||
| 116 | |||
| 117 | val pop_before : ?n:int -> 'a zipper -> 'a zipper | ||
| 118 | (** Remove [n] elements before the cursor (1 by default), if any, and | 114 | (** Remove [n] elements before the cursor (1 by default), if any, and |
| 119 | return the modified zipper. Calling [pop_before z], | 115 | return them alongside the modified zipper. *) |
| 120 | |||
| 121 | - if [z] is [([3; 2; 1], [4; 5])], the result is [([2; 1], [4, 5])], | ||
| 122 | - if [z] is [([], [1; 2; 3])], the result is [([], [1; 2; 3])]. | ||
| 123 | *) | ||
| 124 | 116 | ||
| 125 | val pop_after : ?n:int -> 'a zipper -> 'a zipper | 117 | val pop_after : ?n:int -> 'a zipper -> 'a Sequence.t * 'a zipper |
| 126 | (** Remove [n] elements after (and {b not} including) the cursor | 118 | (** Remove [n] elements after (and {b not} including) the cursor |
| 127 | (1 by default), if any, and return the modified zipper. | 119 | (1 by default), if any, and return them alongside the modified |
| 128 | Calling [pop_after z], | 120 | zipper. *) |
| 129 | |||
| 130 | - if [z] is [([3; 2; 1], [4; 5])], the result is [([3; 2; 1], [4])], | ||
| 131 | - if [z] is [([1; 2; 3], [4])], the result is [([1; 2; 3], [4])]. | ||
| 132 | *) | ||
| 133 | 121 | ||
| 134 | val push : 'a -> 'a zipper -> 'a zipper | 122 | val push : 'a -> 'a zipper -> 'a zipper |
| 135 | (** Insert an element at the cursor position. | 123 | (** Insert an element at the cursor position. |
