Simple `count` function.
[advlib.git] / src / Advent / List.hs
blob443a08cd599667c28b07a81fa59f88e957405edd
1 module Advent.List where
3 breakBy :: ([a] -> Bool) -> [a] -> ([a], [a])
4 breakBy _ [] = ([], [])
5 breakBy p xs@(x:xs')
6 | p xs = ([], xs)
7 | otherwise =
8 let (ys, zs) = breakBy p xs'
9 in (x:ys, zs)
11 split :: Eq a => [a] -> [a] -> [[a]]
12 split delim [] = []
13 split delim list =
14 let (x, xs) = breakBy (\x -> take (length delim) x == delim) list
15 in x : split delim (drop (length delim) xs)
17 splitOne :: Eq a => a -> [a] -> [[a]]
18 splitOne delim [] = []
19 splitOne delim list =
20 let (x, xs) = break (==delim) list
21 in x : splitOne delim (drop 1 xs)
23 chunks :: Int -> [a] -> [[a]]
24 chunks n [] = []
25 chunks n xs = take n xs : chunks n (drop n xs)
27 count :: (a -> Bool) -> [a] -> Int
28 count p = length . filter p