Commit 69a8f4f0 authored by Klaus Aehlig's avatar Klaus Aehlig

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: default avatarKlaus Aehlig <aehlig@google.com>
Reviewed-by: default avatarPetr Pudlak <pudlak@google.com>

Cherry-picked-from: a1da8a50Signed-off-by: default avatarKlaus Aehlig <aehlig@google.com>
Reviewed-by: default avatarHrvoje Ribicic <riba@google.com>
parent f879621b
...@@ -43,6 +43,7 @@ module Ganeti.BasicTypes ...@@ -43,6 +43,7 @@ module Ganeti.BasicTypes
, annotateResult , annotateResult
, iterateOk , iterateOk
, select , select
, runListHead
, LookupResult(..) , LookupResult(..)
, MatchPriority(..) , MatchPriority(..)
, lookupName , lookupName
...@@ -59,6 +60,7 @@ import Control.Monad ...@@ -59,6 +60,7 @@ import Control.Monad
import Control.Monad.Trans import Control.Monad.Trans
import Data.Function import Data.Function
import Data.List import Data.List
import Data.Maybe (listToMaybe)
import Data.Set (Set) import Data.Set (Set)
import qualified Data.Set as Set (empty) import qualified Data.Set as Set (empty)
import Text.JSON (JSON) import Text.JSON (JSON)
...@@ -166,6 +168,12 @@ select :: a -- ^ default result ...@@ -166,6 +168,12 @@ select :: a -- ^ default result
-> a -- ^ first result which has a True condition, or default -> a -- ^ first result which has a True condition, or default
select def = maybe def snd . find fst 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 -- * Lookup of partial names functionality
-- | The priority of a match in a lookup result. -- | The priority of a match in a lookup result.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment