]> gitweb.michael.orlitzky.com - hath.git/blob - src/ListUtils.hs
Use Cabal test integration.
[hath.git] / src / ListUtils.hs
1 module ListUtils
2 ( pad_left_to,
3 pad_right_to,
4 splitWith
5 ) where
6
7
8 -- Stolen from ByteString. Splits a list at each element satisfying
9 -- the predicate p.
10 splitWith :: (a -> Bool) -> [a] -> [[a]]
11 splitWith p xs =
12 ys : case zs of
13 [] -> []
14 _:ws -> splitWith p ws
15 where (ys,zs) = break p xs
16
17
18 -- Pads a list (on the left) to length len by prepending pad_elem.
19 pad_left_to :: Int -> a -> [a] -> [a]
20 pad_left_to len pad_elem xs =
21 if (length xs) >= len then
22 xs
23 else
24 (replicate padcount pad_elem) ++ xs
25 where
26 padcount = len - (length xs)
27
28
29 -- Pads a list (on the right) to length len by appending pad_elem.
30 pad_right_to :: Int -> a -> [a] -> [a]
31 pad_right_to len pad_elem xs =
32 if (length xs) >= len then
33 xs
34 else
35 xs ++ (replicate padcount pad_elem)
36 where
37 padcount = len - (length xs)