Last active
August 29, 2015 14:21
-
-
Save epost/932618911d3fccbf4dd2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
-- | The `partition` function takes a predicate and a list, and returns the pair | |
-- | of lists of elements which do and do not satisfy the | |
-- | predicate, respectively. | |
-- | | |
-- | ```purescript | |
-- | partition (< 3) [1,2,3,4,5] == Tuple ([1,2]) ([3,4,5]) | |
-- | ``` | |
partition :: forall a. (a -> Boolean) -> [a] -> Tuple [a] [a] | |
partition p xs = foldr (select p) (Tuple [] []) xs | |
select :: forall a. (a -> Boolean) -> a -> Tuple [a] [a] -> Tuple [a] [a] | |
select p x (Tuple ts fs) | p x = Tuple (x:ts) fs | |
| otherwise = Tuple ts (x:fs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment