]> Git — Sourcephile - majurity.git/blob - hjugement-protocol/src/Voting/Protocol/Utils.hs
protocol: polish benchmarks
[majurity.git] / hjugement-protocol / src / Voting / Protocol / Utils.hs
1 module Voting.Protocol.Utils where
2
3 import Control.Applicative (Applicative(..), Alternative(..))
4 import Control.Monad (Monad(..), join, mapM, unless, zipWithM)
5 import Control.Monad.Trans.Class (MonadTrans(..))
6 import Data.Bool
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
16
17 -- | NOTE: check the lengths before applying @f@.
18 isoZipWith :: (a->b->c) -> [a]->[b]->Maybe [c]
19 isoZipWith f as bs
20 | List.length as /= List.length bs = Nothing
21 | otherwise = Just (List.zipWith f as bs)
22
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
30
31 isoZipWithM ::
32 Applicative m =>
33 m () ->
34 (a -> b -> m c) ->
35 [a] -> [b] -> m [c]
36 isoZipWithM err f as bs =
37 maybe ([] <$ err) sequenceA $
38 isoZipWith f as bs
39
40 isoZipWithM_ ::
41 Applicative m =>
42 m () ->
43 (a -> b -> m c) ->
44 [a] -> [b] -> m ()
45 isoZipWithM_ err f as bs =
46 maybe err sequenceA_ $
47 isoZipWith f as bs
48
49 isoZipWith3M ::
50 Applicative m =>
51 m () ->
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
57
58 isoZipWith3M_ ::
59 Applicative m =>
60 m () ->
61 (a -> b -> c -> m d) ->
62 [a] -> [b] -> [c] ->
63 m ()
64 isoZipWith3M_ err f as bs cs =
65 maybe err sequenceA_ $
66 isoZipWith3 f as bs cs
67