]> xenbits.xensource.com Git - xcp/xen-api-libs.git/commitdiff
CP-1622: Listext function to replace the value for a key in an assoc list
authorRob Hoes <rob.hoes@citrix.com>
Wed, 17 Feb 2010 10:14:31 +0000 (10:14 +0000)
committerRob Hoes <rob.hoes@citrix.com>
Wed, 17 Feb 2010 10:14:31 +0000 (10:14 +0000)
Signed-off-by: Rob Hoes <rob.hoes@citrix.com>
stdext/listext.ml
stdext/listext.mli

index 29ad0eeb349a141ac69a9d4eb73dd02e3f6a9f6b..9023569abc2bf2085fe2d099cf7b7409863b4ef2 100644 (file)
@@ -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
index c8b4ced3ddb683924a17b94a66aa5e0cb0ed5648..d1f8b1e8b193bda7301000c37d7093cc0887d71a 100644 (file)
@@ -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