From: Rob Hoes Date: Wed, 17 Feb 2010 10:14:31 +0000 (+0000) Subject: CP-1622: Listext function to replace the value for a key in an assoc list X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=7116aeedecf657e672343fdd84b99e02d7d403db;p=xcp%2Fxen-api-libs.git CP-1622: Listext function to replace the value for a key in an assoc list Signed-off-by: Rob Hoes --- diff --git a/stdext/listext.ml b/stdext/listext.ml index 29ad0ee..9023569 100644 --- a/stdext/listext.ml +++ b/stdext/listext.ml @@ -192,4 +192,13 @@ let take n list = let rec tails = function | [] -> [[]] | (_::xs) as l -> l :: tails xs + +let rec replace_assoc key new_value = function + | [] -> [] + | (k, _) as p :: tl -> + if k = key then + (key, new_value) :: tl + else + p :: replace_assoc key new_value tl + end diff --git a/stdext/listext.mli b/stdext/listext.mli index c8b4ced..d1f8b1e 100644 --- a/stdext/listext.mli +++ b/stdext/listext.mli @@ -177,4 +177,6 @@ module List : val tails : 'a list -> ('a list) list + (** Replace the value belonging to a key in an association list. *) + val replace_assoc : 'a -> 'b -> ('a * 'b) list -> ('a * 'b) list end