]> Git — Sourcephile - majurity.git/blob - hjugement-web/src/Voting/Protocol/Utils.purs
web: impl: continue to transcode Voting.Protocol.Cryptography
[majurity.git] / hjugement-web / src / Voting / Protocol / Utils.purs
1 module Voting.Protocol.Utils where
2
3 import Control.Applicative (class Applicative, (<$))
4 import Data.Boolean (otherwise)
5 import Data.Eq (class Eq, (==), (/=))
6 import Data.Function (($))
7 import Data.List (List, (:))
8 import Data.List as List
9 import Data.List.Lazy as LL
10 import Data.Maybe (Maybe(..), maybe)
11 import Data.Traversable (sequence)
12 import Data.Unit (Unit)
13
14 -- | The 'zipWith3' function takes a function which combines three
15 -- elements, as well as three lists and returns a list of their point-wise
16 -- combination, analogous to 'zipWith'.
17 zipWith3 :: forall a b c d. (a->b->c->d) -> List a->List b->List c->List d
18 zipWith3 z = go
19 where
20 go (a:as) (b:bs) (c:cs) = z a b c : go as bs cs
21 go _ _ _ = List.Nil
22
23 -- | NOTE: check the lengths before applying @f@.
24 isoZipWith :: forall a b c. (a->b->c) -> List a->List b->Maybe (List c)
25 isoZipWith f as bs
26 | List.length as /= List.length bs = Nothing
27 | otherwise = Just (List.zipWith f as bs)
28
29 -- | NOTE: check the lengths before applying @f@.
30 isoZipWith3 :: forall a b c d. (a->b->c->d) -> List a->List b->List c->Maybe (List d)
31 isoZipWith3 f as bs cs
32 | List.length as /= List.length bs = Nothing
33 | List.length as /= List.length cs = Nothing
34 | otherwise = Just (zipWith3 f as bs cs)
35
36 isoZipWithM :: forall f a b c.
37 Applicative f =>
38 f Unit -> (a->b->f c) -> List a->List b->f (List c)
39 isoZipWithM err f as bs =
40 maybe (List.Nil <$ err) sequence $
41 isoZipWith f as bs