On Thu, Nov 7, 2013 at 1:20 PM, Klaus Aehlig <[email protected]> wrote:
> This version of 'the' properly lives in the 'Result' monad,
> as opposed to traditional one calling 'error'. The reason
> why it is 'Bad' that not precisely one element is returned
> is given as argument.
>
> Signed-off-by: Klaus Aehlig <[email protected]>
> ---
>  src/Ganeti/Utils.hs | 6 ++++++
>  1 file changed, 6 insertions(+)
>
> diff --git a/src/Ganeti/Utils.hs b/src/Ganeti/Utils.hs
> index 5d5194c..8658889 100644
> --- a/src/Ganeti/Utils.hs
> +++ b/src/Ganeti/Utils.hs
> @@ -58,6 +58,7 @@ module Ganeti.Utils
>    , splitEithers
>    , recombineEithers
>    , resolveAddr
> +  , resultThe
>    , setOwnerAndGroupFromNames
>    , formatOrdinal
>    , atomicWriteFile
> @@ -423,6 +424,11 @@ exitIfEmpty :: String -> [a] -> IO a
>  exitIfEmpty _ (x:_) = return x
>  exitIfEmpty s []    = exitErr s
>
> +-- | Obtain the only element of a list in the Result monad.
> +resultThe :: String -> [a] -> Result a
> +resultThe _ [x] = Ok x
> +resultThe s _ = Bad s
> +

I guess the function name comes from this one:
http://hackage.haskell.org/package/base-4.6.0.1/docs/GHC-Exts.html#v:the
But it seems to me that the semantics is a bit different: "the" checks
that all the elements in the list are identical, and then return one
of them, whereas "resultThe" checks that the list only has one
element.

If this is correct, I think that either "resultThe" should work on
lists with multiple identical elements, or it should have a different
name not to cause misunderstandings.

Thanks,
Michele

-- 
Google Germany GmbH
Dienerstr. 12
80331 München

Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg
Geschäftsführer: Graham Law, Christine Elizabeth Flores

Reply via email to