[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-API] [PATCH] CP-1622: Listext function to replace the value for a key in an assoc list


  • To: xen-api <xen-api@xxxxxxxxxxxxxxxxxxx>
  • From: Rob Hoes <rob.hoes@xxxxxxxxxx>
  • Date: Tue, 16 Feb 2010 23:09:04 +0000
  • Delivery-date: Tue, 16 Feb 2010 15:09:36 -0800
  • List-id: Discussion of API issues surrounding Xen <xen-api.lists.xensource.com>

# HG changeset patch
# User Rob Hoes <rob.hoes@xxxxxxxxxx>
CP-1622: Listext function to replace the value for a key in an assoc list

Signed-off-by: Rob Hoes <rob.hoes@xxxxxxxxxx>

diff -r 3e1beafe76b5 stdext/listext.ml
--- a/stdext/listext.ml Tue Feb 16 22:40:11 2010 +0000
+++ b/stdext/listext.ml Tue Feb 16 22:47:58 2010 +0000
@@ -192,4 +192,13 @@
 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 -r 3e1beafe76b5 stdext/listext.mli
--- a/stdext/listext.mli        Tue Feb 16 22:40:11 2010 +0000
+++ b/stdext/listext.mli        Tue Feb 16 22:47:58 2010 +0000
@@ -177,4 +177,6 @@
        
     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

Attachment: list-replace-assoc
Description: Text document

_______________________________________________
xen-api mailing list
xen-api@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/mailman/listinfo/xen-api

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.