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 <[email protected]>
Reviewed-by: Petr Pudlak <[email protected]>

Cherry-picked-from: a1da8a503ba
Signed-off-by: Klaus Aehlig <[email protected]>
---
 src/Ganeti/BasicTypes.hs | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/src/Ganeti/BasicTypes.hs b/src/Ganeti/BasicTypes.hs
index 87899f5..aee67ad 100644
--- a/src/Ganeti/BasicTypes.hs
+++ b/src/Ganeti/BasicTypes.hs
@@ -43,6 +43,7 @@ module Ganeti.BasicTypes
   , annotateResult
   , iterateOk
   , select
+  , runListHead
   , LookupResult(..)
   , MatchPriority(..)
   , lookupName
@@ -59,6 +60,7 @@ import Control.Monad
 import Control.Monad.Trans
 import Data.Function
 import Data.List
+import Data.Maybe (listToMaybe)
 import Data.Set (Set)
 import qualified Data.Set as Set (empty)
 import Text.JSON (JSON)
@@ -166,6 +168,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.
-- 
2.2.0.rc0.207.ga3a616c

Reply via email to