aboutsummaryrefslogtreecommitdiff
path: root/lib/Utils.hs
blob: 4c9a5816c3722d84dcffd58df4f655533c3f33a0 (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
module Utils ( binaryToInt
             , countElem
             , xor
             , right
             , bet
             , (&+)
             , howMany
             ) where 

import Data.Char (digitToInt)
import Control.Monad
import Data.Either


binaryToInt :: String -> Int
binaryToInt = foldl (\a x -> a * 2 + digitToInt x) 0

countElem :: (Eq a) => a -> [a] -> Int
countElem c ls = sum $ map (fromEnum . (== c)) ls

xor :: Bool -> Bool -> Bool
xor a b = (not a && b) || (a && not b)

right :: Either a b -> b
right (Right b) = b
right _ = undefined

bet :: Int -> (Int, Int) -> Bool
bet k (l, u) = k >= l && k <= u

-- combine filter predicates
(&+) :: (a -> Bool) -> (a -> Bool) -> (a -> Bool)
(&+) = liftM2 (&&)

howMany :: (a -> Bool) -> [a] -> Int
howMany predicate = length . filter ((== True) . predicate)