diff options
author | Akshay <[email protected]> | 2020-12-05 07:49:25 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2020-12-05 07:49:25 +0000 |
commit | 555da2ae6f658672cfc0d37e437ec356c0c0fa63 (patch) | |
tree | f1b1b2e411ac130373421aece0cb8e5709a0e925 /lib | |
parent | e0a5d9cbf99c2d6bbaef12f2ce5506eda0ba5bec (diff) |
factor common functions into Utils
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Utils.hs | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/Utils.hs b/lib/Utils.hs new file mode 100644 index 0000000..1eb42fc --- /dev/null +++ b/lib/Utils.hs | |||
@@ -0,0 +1,32 @@ | |||
1 | module Utils ( binaryToInt | ||
2 | , countElem | ||
3 | , xor | ||
4 | , right | ||
5 | , bet | ||
6 | , (&+) | ||
7 | ) where | ||
8 | |||
9 | import Data.Char (digitToInt) | ||
10 | import Control.Monad | ||
11 | import Data.Either | ||
12 | |||
13 | |||
14 | binaryToInt :: String -> Int | ||
15 | binaryToInt = foldl (\a x -> a * 2 + digitToInt x) 0 | ||
16 | |||
17 | countElem :: (Eq a) => a -> [a] -> Int | ||
18 | countElem c ls = sum $ map (fromEnum . (== c)) ls | ||
19 | |||
20 | xor :: Bool -> Bool -> Bool | ||
21 | xor a b = (not a && b) || (a && not b) | ||
22 | |||
23 | right :: Either a b -> b | ||
24 | right (Right b) = b | ||
25 | right _ = undefined | ||
26 | |||
27 | bet :: Int -> (Int, Int) -> Bool | ||
28 | bet k (l, u) = k >= l && k <= u | ||
29 | |||
30 | -- combine filter predicates | ||
31 | (&+) :: (a -> Bool) -> (a -> Bool) -> (a -> Bool) | ||
32 | (&+) = liftM2 (&&) | ||