-
Notifications
You must be signed in to change notification settings - Fork 0
/
Helpers.hs
36 lines (28 loc) · 881 Bytes
/
Helpers.hs
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
{-# Language ScopedTypeVariables #-}
module Helpers
(listToPair, slidingWindow, readLines, imap, divides)
where
import Data.Array.IArray
listToPair :: [a] -> (a, a)
listToPair [x, y] = (x, y)
listToPair _ = error "listToPair: not a pair list"
slidingWindow :: Int -> [a] -> [[a]]
slidingWindow _ [] = []
slidingWindow n xs =
if length peek < n then
[]
else
peek : slidingWindow n (drop 1 xs)
where
peek = take n xs
readLines :: FilePath -> IO [String]
readLines = fmap lines . readFile
imap :: forall a i e e'. (IArray a e, IArray a e', IArray a (i, e), Ix i)
=> (i -> e -> e') -> a i e -> a i e'
imap f = amap (uncurry f) . zipWithIndices
where
zipWithIndices :: a i e -> a i (i, e)
zipWithIndices arr = listArray (bounds arr) . assocs $ arr
infixl 1 `divides`
divides :: (Integral a) => a -> a -> Bool
divides m n = n `mod` m == 0