diff options
| author | John Estabrook <jestabro@vyos.io> | 2025-08-26 16:58:17 -0500 |
|---|---|---|
| committer | John Estabrook <jestabro@vyos.io> | 2025-09-15 08:14:59 -0500 |
| commit | 0dfb238da1331c7d5d3152df4de0a17fe9562971 (patch) | |
| tree | 0f3566420c1f59a7ee85cb56bedd04b2b92b9243 /src | |
| parent | 057dbfa3003ce842179f10e08a3aa757b36b17a2 (diff) | |
| download | vyos1x-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.ml | 14 | ||||
| -rw-r--r-- | src/vylist.mli | 2 |
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 |
