From fd4fc4ce9ad45a25ca3e77a434306aa2476161bb Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Fri, 12 Jan 2024 14:12:57 +0100 Subject: feat(zipper): add function to map on cursor focus with default --- lib/zipper.ml | 5 +++++ lib/zipper.mli | 4 ++++ 2 files changed, 9 insertions(+) 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 = | None -> z | Some (h, t) -> { z with after = Sequence.shift_right t (f h) } +let map_focus_or ~default f z = + match Sequence.next z.after with + | None -> { z with after = Sequence.singleton default } + | Some (h, t) -> { z with after = Sequence.shift_right t (f h) } + let map f z = { z with before = Sequence.map ~f z.before; after = Sequence.map ~f z.after } 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 val map_focus : ('a -> 'a) -> 'a zipper -> 'a zipper (** Map a function over the element focused by the cursor, if any. *) +val map_focus_or : default:'a -> ('a -> 'a) -> 'a zipper -> 'a zipper +(** Map a function over the element focused by the cursor. Push + [default] if no element is focused. *) + val map : ('a -> 'a) -> 'a zipper -> 'a zipper (** Map a function over all elements of a zipper. *) -- cgit v1.2.3