Skip to content

Instantly share code, notes, and snippets.

@epost
Last active August 29, 2015 14:21
Show Gist options
  • Save epost/932618911d3fccbf4dd2 to your computer and use it in GitHub Desktop.
Save epost/932618911d3fccbf4dd2 to your computer and use it in GitHub Desktop.
-- | 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