On Tue, Sep 25, 2012 at 06:43:50PM +0200, Agata Murawska wrote:
> This is a helper function that gets the JSON encoding of the first
> element of the list (if possible).
> 
> Signed-off-by: Agata Murawska <[email protected]>
> ---
>  htools/Ganeti/JSON.hs |    6 ++++++
>  1 files changed, 6 insertions(+), 0 deletions(-)
> 
> diff --git a/htools/Ganeti/JSON.hs b/htools/Ganeti/JSON.hs
> index 132c0b2..bfeb48a 100644
> --- a/htools/Ganeti/JSON.hs
> +++ b/htools/Ganeti/JSON.hs
> @@ -31,6 +31,7 @@ module Ganeti.JSON
>    , fromObjWithDefault
>    , fromKeyValue
>    , fromJVal
> +  , jsonNullOrFirst
>    , asJSObject
>    , asObjectList
>    , tryFromObj
> @@ -125,6 +126,11 @@ fromJVal v =
>                         "', error: " ++ s)
>      J.Ok x -> return x
>  
> +-- | Helper function that returns Null or first element of the list.
> +jsonNullOrFirst :: (J.JSON b) => [a] -> (a -> b) -> J.JSValue
> +jsonNullOrFirst [] _ = J.JSNull
> +jsonNullOrFirst (x:_) f = J.showJSON $ f x

The name is confusing… I would have chosen jsonHead (since you're doing
basically (showJSON . head), with optional JSNull as failure mode). LGTM
either way.

thanks,
iustin

Reply via email to