1 module Voting.Protocol.Utils where
3 import Control.Applicative (Applicative(..), Alternative(..))
4 import Control.Monad (Monad(..), join, mapM, unless, zipWithM)
5 import Control.Monad.Trans.Class (MonadTrans(..))
7 import Data.Either (either)
8 import Data.Eq (Eq(..))
9 import Data.Foldable (Foldable, foldMap, and, sequenceA_)
10 import Data.Function (($), id, const)
11 import Data.Functor (Functor, (<$>), (<$))
12 import Data.Functor.Identity (Identity(..))
13 import Data.Maybe (Maybe(..), maybe)
14 import Data.Traversable (Traversable(..))
15 import qualified Data.List as List
17 -- | NOTE: check the lengths before applying @f@.
18 isoZipWith :: (a->b->c) -> [a]->[b]->Maybe [c]
20 | List.length as /= List.length bs = Nothing
21 | otherwise = Just (List.zipWith f as bs)
23 -- | NOTE: check the lengths before applying @f@.
24 isoZipWith3 :: (a->b->c->d) -> [a]->[b]->[c]->Maybe [d]
25 isoZipWith3 f as bs cs
26 | al /= List.length bs = Nothing
27 | al /= List.length cs = Nothing
28 | otherwise = Just (List.zipWith3 f as bs cs)
29 where al = List.length as
36 isoZipWithM err f as bs =
37 maybe ([] <$ err) sequenceA $
45 isoZipWithM_ err f as bs =
46 maybe err sequenceA_ $
52 (a -> b -> c -> m d) ->
53 [a] -> [b] -> [c] -> m [d]
54 isoZipWith3M err f as bs cs =
55 maybe ([] <$ err) sequenceA $
56 isoZipWith3 f as bs cs
61 (a -> b -> c -> m d) ->
64 isoZipWith3M_ err f as bs cs =
65 maybe err sequenceA_ $
66 isoZipWith3 f as bs cs