summaryrefslogtreecommitdiff
path: root/2020/Util.hs
blob: 7cd674d834af4d3ad3eb2f35900a874c5b135c21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
module Util where

import Data.List
import Data.List.NonEmpty (NonEmpty(..), (<|))


splitOn :: (a -> Bool) -> [a] -> NonEmpty [a]
splitOn _ [] = [] :| []
splitOn f (x:xs) | f x = [] <| splitOn f xs
                 | otherwise = let l :| ls = splitOn f xs
                               in (x : l) :| ls

splits' :: [a] -> [(a, [a])]
splits' l = zip l (zipWith (++) (inits l) (tail (tails l)))