summaryrefslogtreecommitdiff
path: root/src/Voting/Protocol/Utils.hs
blob: 1cc30f07bd2462acc446f165f20e15c4f17bd877 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
module Voting.Protocol.Utils where

import Control.Applicative (Applicative(..))
import Data.Bool
import Data.Eq (Eq(..))
import Data.Foldable (sequenceA_)
import Data.Function (($))
import Data.Functor ((<$))
import Data.Maybe (Maybe(..), maybe)
import Data.Traversable (Traversable(..))
import qualified Data.List as List

-- | Like ('.') but with two arguments.
o2 :: (c -> d) -> (a -> b -> c) -> a -> b -> d
o2 f g = \x y -> f (g x y)
infixr 9 `o2`
{-# INLINE o2 #-}

-- | NOTE: check the lengths before applying @f@.
isoZipWith :: (a->b->c) -> [a]->[b]->Maybe [c]
isoZipWith f as bs
 | List.length as /= List.length bs = Nothing
 | otherwise = Just (List.zipWith f as bs)

-- | NOTE: check the lengths before applying @f@.
isoZipWith3 :: (a->b->c->d) -> [a]->[b]->[c]->Maybe [d]
isoZipWith3 f as bs cs
 | al /= List.length bs = Nothing
 | al /= List.length cs = Nothing
 | otherwise = Just (List.zipWith3 f as bs cs)
 where al = List.length as

isoZipWithM ::
 Applicative f =>
 f () -> (a->b->f c) -> [a]->[b]->f [c]
isoZipWithM err f as bs =
	maybe ([] <$ err) sequenceA $
		isoZipWith f as bs

isoZipWithM_ ::
 Applicative f =>
 f () -> (a->b->f c) -> [a]->[b]->f ()
isoZipWithM_ err f as bs =
	maybe err sequenceA_ $
		isoZipWith f as bs

isoZipWith3M ::
 Applicative f =>
 f () -> (a->b->c->f d) -> [a]->[b]->[c]->f [d]
isoZipWith3M err f as bs cs =
	maybe ([] <$ err) sequenceA $
		isoZipWith3 f as bs cs

isoZipWith3M_ ::
 Applicative f =>
 f () -> (a->b->c->f d) -> [a]->[b]->[c]->f ()
isoZipWith3M_ err f as bs cs =
	maybe err sequenceA_ $
		isoZipWith3 f as bs cs