From a1da8a503baf59ca4dc1a8074db08c1c97e125ce Mon Sep 17 00:00:00 2001 From: Klaus Aehlig Date: Sat, 15 Feb 2014 00:12:23 +0100 Subject: [PATCH] Add a function to leave the list monad The list monad provides convenient syntax for non-deterministic algorithms. Add a function leaving that monad with this intuition in mind. Signed-off-by: Klaus Aehlig Reviewed-by: Petr Pudlak --- src/Ganeti/BasicTypes.hs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Ganeti/BasicTypes.hs b/src/Ganeti/BasicTypes.hs index 30ebc8b6d..22bbcd4b5 100644 --- a/src/Ganeti/BasicTypes.hs +++ b/src/Ganeti/BasicTypes.hs @@ -48,6 +48,7 @@ module Ganeti.BasicTypes , handleErrorT , iterateOk , select + , runListHead , LookupResult(..) , MatchPriority(..) , lookupName @@ -312,6 +313,12 @@ select :: a -- ^ default result -> a -- ^ first result which has a True condition, or default select def = maybe def snd . find fst +-- | Apply a function to the first element of a list, return the default +-- value, if the list is empty. This is just a convenient combination of +-- maybe and listToMaybe. +runListHead :: a -> (b -> a) -> [b] -> a +runListHead a f = maybe a f . listToMaybe + -- * Lookup of partial names functionality -- | The priority of a match in a lookup result. -- GitLab