From: Matthias Goergens Date: Thu, 17 Jun 2010 21:16:18 +0000 (+0100) Subject: The module stdext/Listext got a safe_head function that wraps hd in an option type. X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=ab76ce2b6c92c780442b12408839e79a2bf1993a;p=xcp%2Fxen-api-libs.git The module stdext/Listext got a safe_head function that wraps hd in an option type. Signed-off-by: Matthias Goergens --- diff --git a/stdext/listext.ml b/stdext/listext.ml index 9023569..5f9e1c7 100644 --- a/stdext/listext.ml +++ b/stdext/listext.ml @@ -193,6 +193,10 @@ let rec tails = function | [] -> [[]] | (_::xs) as l -> l :: tails xs +let safe_hd = function + | a::_ -> Some a + | [] -> None + let rec replace_assoc key new_value = function | [] -> [] | (k, _) as p :: tl -> diff --git a/stdext/listext.mli b/stdext/listext.mli index d1f8b1e..66c0729 100644 --- a/stdext/listext.mli +++ b/stdext/listext.mli @@ -176,6 +176,7 @@ module List : val take : int -> 'a list -> 'a list val tails : 'a list -> ('a list) list + val safe_hd : 'a list -> 'a option (** Replace the value belonging to a key in an association list. *) val replace_assoc : 'a -> 'b -> ('a * 'b) list -> ('a * 'b) list