1 module Voting.Protocol.Utils where
3 import Control.Applicative (Applicative(..))
5 import Data.Eq (Eq(..))
6 import Data.Foldable (sequenceA_)
7 import Data.Function (($))
8 import Data.Functor ((<$))
9 import Data.Maybe (Maybe(..), maybe)
10 import Data.Traversable (Traversable(..))
11 import qualified Data.List as List
13 -- | Like ('.') but with two arguments.
14 o2 :: (c -> d) -> (a -> b -> c) -> a -> b -> d
15 o2 f g = \x y -> f (g x y)
19 -- | NOTE: check the lengths before applying @f@.
20 isoZipWith :: (a->b->c) -> [a]->[b]->Maybe [c]
22 | List.length as /= List.length bs = Nothing
23 | otherwise = Just (List.zipWith f as bs)
25 -- | NOTE: check the lengths before applying @f@.
26 isoZipWith3 :: (a->b->c->d) -> [a]->[b]->[c]->Maybe [d]
27 isoZipWith3 f as bs cs
28 | al /= List.length bs = Nothing
29 | al /= List.length cs = Nothing
30 | otherwise = Just (List.zipWith3 f as bs cs)
31 where al = List.length as
35 f () -> (a->b->f c) -> [a]->[b]->f [c]
36 isoZipWithM err f as bs =
37 maybe ([] <$ err) sequenceA $
42 f () -> (a->b->f c) -> [a]->[b]->f ()
43 isoZipWithM_ err f as bs =
44 maybe err sequenceA_ $
49 f () -> (a->b->c->f d) -> [a]->[b]->[c]->f [d]
50 isoZipWith3M err f as bs cs =
51 maybe ([] <$ err) sequenceA $
52 isoZipWith3 f as bs cs
56 f () -> (a->b->c->f d) -> [a]->[b]->[c]->f ()
57 isoZipWith3M_ err f as bs cs =
58 maybe err sequenceA_ $
59 isoZipWith3 f as bs cs