on master help sign in
Implement partition.

--HG-- extra : convert_revision : 52b4adec570d64fb5c3410c4d1f00b5b9bebbc3e

commitb216a257d15cd47cf471cc4e08695c90c2e8ac08 authorBryan O'Sullivan <bos@serpentine.com> dateThu 12 Feb 2009, 08:59 (18 years ago) parent82674d9
2 files changed+13−1
Data/Text.hs+11−1 tests/Properties.hs+2
Data/Text.hs+11−1
@@ -123,7 +123,7 @@ module Data.Text
123123 , elem
124124 , filter
125125 , find
126− -- , partition
126+ , partition
127127
128128 -- , findSubstring
129129
@@ -758,6 +758,15 @@ find :: (Char -> Bool) -> Text -> Maybe Char
758758find p t = S.find p (stream t)
759759{-# INLINE find #-}
760760
761+-- | /O(n)/ The 'partition' function takes a predicate and a 'Text',
762+-- and returns the pair of 'Text's with elements which do and do not
763+-- satisfy the predicate, respectively; i.e.
764+--
765+-- > partition p t == (filter p t, filter (not . p) t)
766+partition :: (Char -> Bool) -> Text -> (Text, Text)
767+partition p t = (filter p t, filter (not . p) t)
768+{-# INLINE partition #-}
769+
761770-- | Break a string on a substring, returning a pair of the part of the
762771-- string prior to the match, and the rest of the string.
763772--
@@ -796,6 +805,7 @@ breakSubstring pat src = search 0 src
796805 | null s = (src,empty) -- not found
797806 | pat `isPrefixOf` s = (take n src,s)
798807 | otherwise = search (n+1) (tail s)
808+{-# INLINE breakSubstring #-}
799809
800810-- | /O(n)/ 'filter', applied to a predicate and a 'Text',
801811-- returns a 'Text' containing those characters that satisfy the
tests/Properties.hs+2
@@ -157,6 +157,7 @@ prop_isInfixOf s = L.isInfixOf s `eqP` T.isInfixOf (pack s)
157157prop_elem c = L.elem c `eqP` T.elem c
158158prop_filter p = L.filter p `eqP` (unpack . T.filter p)
159159prop_find p = L.find p `eqP` T.find p
160+prop_partition p = L.partition p `eqP` (unpack2 . T.partition p)
160161
161162prop_index x s = x < L.length s && x >= 0 ==>
162163 (L.!!) s x == T.index (pack s) x
@@ -259,6 +260,7 @@ tests = [
259260 ("prop_elem", mytest prop_elem),
260261 ("prop_filter", mytest prop_filter),
261262 ("prop_find", mytest prop_find),
263+ ("prop_partition", mytest prop_partition),
262264
263265 ("prop_index", mytest prop_index),
264266 ("prop_findIndex", mytest prop_findIndex),