Skip to content

Instantly share code, notes, and snippets.

@ShinNoNoir
Created May 2, 2018 19:03
Show Gist options
  • Save ShinNoNoir/6af817c648e280311ac8220bbf5f89f9 to your computer and use it in GitHub Desktop.
Save ShinNoNoir/6af817c648e280311ac8220bbf5f89f9 to your computer and use it in GitHub Desktop.
splitWhenPrev: A function somewhat similar to groupBy, but instead of using the first element of a group to determine when to start a new group, it compares adjacent values
{-# LANGUAGE LambdaCase #-}
import Data.List (unfoldr)
both :: (a -> b) -> (a,a) -> (b,b)
both f (x,y) = (f x, f y)
withPrevious :: [a] -> [(a, Maybe a)]
withPrevious xs = zip xs (Nothing : map Just xs)
splitWhenPrev :: (a -> a -> Bool) -> [a] -> [[a]]
splitWhenPrev p = unfoldr f
where f [] = Nothing
f seed = Just
. both (map fst)
. break (\case (a, Just b) -> p b a
_ -> False)
. withPrevious
$ seed
{-
-- Usage example (split when monotonicity is violated):
ghci> splitWhenPrev (>=) [1, 2, 3, 5, 4, 7, 6, 8, 9]
[[1,2,3,5],[4,7],[6,8,9]]
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment