Created
May 2, 2018 19:03
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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