summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn Estabrook <jestabro@vyos.io>2025-08-26 16:58:17 -0500
committerJohn Estabrook <jestabro@vyos.io>2025-09-15 08:14:59 -0500
commit0dfb238da1331c7d5d3152df4de0a17fe9562971 (patch)
tree0f3566420c1f59a7ee85cb56bedd04b2b92b9243 /src
parent057dbfa3003ce842179f10e08a3aa757b36b17a2 (diff)
downloadvyos1x-config-0dfb238da1331c7d5d3152df4de0a17fe9562971.tar.gz
vyos1x-config-0dfb238da1331c7d5d3152df4de0a17fe9562971.zip
T7737: add util replace_or_cons for prepending to list if not element
Diffstat (limited to 'src')
-rw-r--r--src/vylist.ml14
-rw-r--r--src/vylist.mli2
2 files changed, 10 insertions, 6 deletions
diff --git a/src/vylist.ml b/src/vylist.ml
index c64dd89..9c1085d 100644
--- a/src/vylist.ml
+++ b/src/vylist.ml
@@ -10,11 +10,15 @@ let rec remove p xs =
| y :: ys -> if (p y) then ys
else y :: (remove p ys)
-let rec replace p x xs =
- match xs with
- | [] -> raise Not_found
- | y :: ys -> if (p y) then x :: ys
- else y :: (replace p x ys)
+let rec replace ?(force=false) p x xs =
+ try
+ match xs with
+ | [] -> raise_notrace Not_found
+ | y :: ys -> if (p y) then x :: ys
+ else y :: (replace p x ys)
+ with Not_found ->
+ if force then (x :: xs)
+ else raise Not_found
let rec insert_before p x xs =
match xs with
diff --git a/src/vylist.mli b/src/vylist.mli
index b74a537..54c0806 100644
--- a/src/vylist.mli
+++ b/src/vylist.mli
@@ -1,6 +1,6 @@
val find : ('a -> bool) -> 'a list -> 'a option
val remove : ('a -> bool) -> 'a list -> 'a list
-val replace : ('a -> bool) -> 'a -> 'a list -> 'a list
+val replace : ?force:bool -> ('a -> bool) -> 'a -> 'a list -> 'a list
val insert_before : ('a -> bool) -> 'a -> 'a list -> 'a list
val insert_after : ('a -> bool) -> 'a -> 'a list -> 'a list
val insert_compare : ('a -> 'a -> int) -> 'a -> 'a list -> 'a list