on master help sign in
Implement partition.
b216a25Bryan O'Sullivan
Data/Text.hs+11−1
@@ -123,7 +123,7 @@ module Data.Text
123 , elem
123 , elem
124 , filter
124 , filter
125 , find
125 , find
126− -- , partition
126+ , partition
127
127
128 -- , findSubstring
128 -- , findSubstring
129
129
@@ -758,6 +758,15 @@ find :: (Char -> Bool) -> Text -> Maybe Char
758find p t = S.find p (stream t)
758find p t = S.find p (stream t)
759{-# INLINE find #-}
759{-# INLINE find #-}
760
760
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+
761-- | Break a string on a substring, returning a pair of the part of the
770-- | Break a string on a substring, returning a pair of the part of the
762-- string prior to the match, and the rest of the string.
771-- string prior to the match, and the rest of the string.
763--
772--
@@ -796,6 +805,7 @@ breakSubstring pat src = search 0 src
796 | null s = (src,empty) -- not found
805 | null s = (src,empty) -- not found
797 | pat `isPrefixOf` s = (take n src,s)
806 | pat `isPrefixOf` s = (take n src,s)
798 | otherwise = search (n+1) (tail s)
807 | otherwise = search (n+1) (tail s)
808+{-# INLINE breakSubstring #-}
799
809
800-- | /O(n)/ 'filter', applied to a predicate and a 'Text',
810-- | /O(n)/ 'filter', applied to a predicate and a 'Text',
801-- returns a 'Text' containing those characters that satisfy the
811-- 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)
157prop_elem c = L.elem c `eqP` T.elem c
157prop_elem c = L.elem c `eqP` T.elem c
158prop_filter p = L.filter p `eqP` (unpack . T.filter p)
158prop_filter p = L.filter p `eqP` (unpack . T.filter p)
159prop_find p = L.find p `eqP` T.find p
159prop_find p = L.find p `eqP` T.find p
160+prop_partition p = L.partition p `eqP` (unpack2 . T.partition p)
160
161
161prop_index x s = x < L.length s && x >= 0 ==>
162prop_index x s = x < L.length s && x >= 0 ==>
162 (L.!!) s x == T.index (pack s) x
163 (L.!!) s x == T.index (pack s) x
@@ -259,6 +260,7 @@ tests = [
259 ("prop_elem", mytest prop_elem),
260 ("prop_elem", mytest prop_elem),
260 ("prop_filter", mytest prop_filter),
261 ("prop_filter", mytest prop_filter),
261 ("prop_find", mytest prop_find),
262 ("prop_find", mytest prop_find),
263+ ("prop_partition", mytest prop_partition),
262
264
263 ("prop_index", mytest prop_index),
265 ("prop_index", mytest prop_index),
264 ("prop_findIndex", mytest prop_findIndex),
266 ("prop_findIndex", mytest prop_findIndex),