diff options
author | Federico Igne <undyamon@disroot.org> | 2024-01-12 14:12:57 +0100 |
---|---|---|
committer | Federico Igne <undyamon@disroot.org> | 2024-01-12 14:12:57 +0100 |
commit | fd4fc4ce9ad45a25ca3e77a434306aa2476161bb (patch) | |
tree | accd4a498d1f1276c3d70d044a7eb18ba6848473 | |
parent | 5b32d8115b7ea81dffc3fe48a0291569dba99e3b (diff) | |
download | sandy-fd4fc4ce9ad45a25ca3e77a434306aa2476161bb.tar.gz sandy-fd4fc4ce9ad45a25ca3e77a434306aa2476161bb.zip |
feat(zipper): add function to map on cursor focus with default
-rw-r--r-- | lib/zipper.ml | 5 | ||||
-rw-r--r-- | lib/zipper.mli | 4 |
2 files changed, 9 insertions, 0 deletions
diff --git a/lib/zipper.ml b/lib/zipper.ml index 30be8eb..9313518 100644 --- a/lib/zipper.ml +++ b/lib/zipper.ml | |||
@@ -107,6 +107,11 @@ let map_focus f z = | |||
107 | | None -> z | 107 | | None -> z |
108 | | Some (h, t) -> { z with after = Sequence.shift_right t (f h) } | 108 | | Some (h, t) -> { z with after = Sequence.shift_right t (f h) } |
109 | 109 | ||
110 | let map_focus_or ~default f z = | ||
111 | match Sequence.next z.after with | ||
112 | | None -> { z with after = Sequence.singleton default } | ||
113 | | Some (h, t) -> { z with after = Sequence.shift_right t (f h) } | ||
114 | |||
110 | let map f z = | 115 | let map f z = |
111 | { z with before = Sequence.map ~f z.before; after = Sequence.map ~f z.after } | 116 | { z with before = Sequence.map ~f z.before; after = Sequence.map ~f z.after } |
112 | 117 | ||
diff --git a/lib/zipper.mli b/lib/zipper.mli index beeb181..e25d57b 100644 --- a/lib/zipper.mli +++ b/lib/zipper.mli | |||
@@ -198,6 +198,10 @@ val map_after : ('a -> 'a) -> 'a zipper -> 'a zipper | |||
198 | val map_focus : ('a -> 'a) -> 'a zipper -> 'a zipper | 198 | val map_focus : ('a -> 'a) -> 'a zipper -> 'a zipper |
199 | (** Map a function over the element focused by the cursor, if any. *) | 199 | (** Map a function over the element focused by the cursor, if any. *) |
200 | 200 | ||
201 | val map_focus_or : default:'a -> ('a -> 'a) -> 'a zipper -> 'a zipper | ||
202 | (** Map a function over the element focused by the cursor. Push | ||
203 | [default] if no element is focused. *) | ||
204 | |||
201 | val map : ('a -> 'a) -> 'a zipper -> 'a zipper | 205 | val map : ('a -> 'a) -> 'a zipper -> 'a zipper |
202 | (** Map a function over all elements of a zipper. *) | 206 | (** Map a function over all elements of a zipper. *) |
203 | 207 | ||