on master help sign in
Implement and test lazy elem, filter, find, and partition
27ac255Bryan O'Sullivan
Data/Text/Lazy.hs+32−4
@@ -125,10 +125,10 @@ module Data.Text.Lazy
125 , isInfixOf
125 , isInfixOf
126
126
127 -- * Searching
127 -- * Searching
128− -- , elem
128+ , elem
129− -- , filter
129+ , filter
130− -- , find
130+ , find
131− -- , partition
131+ , partition
132
132
133 -- , findSubstring
133 -- , findSubstring
134
134
@@ -781,6 +781,34 @@ isInfixOf needle haystack = L.any (isPrefixOf needle) (tails haystack)
781{-# INLINE isInfixOf #-}
781{-# INLINE isInfixOf #-}
782-- TODO: a better implementation
782-- TODO: a better implementation
783
783
784+-- | /O(n)/ 'elem' is the 'Text' membership predicate.
785+elem :: Char -> Text -> Bool
786+elem c t = S.elem c (stream t)
787+{-# INLINE elem #-}
788+
789+-- | /O(n)/ 'filter', applied to a predicate and a 'Text',
790+-- returns a 'Text' containing those characters that satisfy the
791+-- predicate.
792+filter :: (Char -> Bool) -> Text -> Text
793+filter p t = unstream (S.filter p (stream t))
794+{-# INLINE filter #-}
795+
796+-- | /O(n)/ The 'find' function takes a predicate and a 'Text',
797+-- and returns the first element in matching the predicate, or 'Nothing'
798+-- if there is no such element.
799+find :: (Char -> Bool) -> Text -> Maybe Char
800+find p t = S.find p (stream t)
801+{-# INLINE find #-}
802+
803+-- | /O(n)/ The 'partition' function takes a predicate and a 'Text',
804+-- and returns the pair of 'Text's with elements which do and do not
805+-- satisfy the predicate, respectively; i.e.
806+--
807+-- > partition p t == (filter p t, filter (not . p) t)
808+partition :: (Char -> Bool) -> Text -> (Text, Text)
809+partition p t = (filter p t, filter (not . p) t)
810+{-# INLINE partition #-}
811+
784revChunks :: [T.Text] -> Text
812revChunks :: [T.Text] -> Text
785revChunks = L.foldl' (flip chunk) Empty
813revChunks = L.foldl' (flip chunk) Empty
786
814
tests/Properties.hs+8
@@ -309,9 +309,13 @@ prop_T_isInfixOf s = L.isInfixOf s `eqP` T.isInfixOf (packT s)
309prop_TL_isInfixOf s = L.isInfixOf s `eqP` TL.isInfixOf (packT s)
309prop_TL_isInfixOf s = L.isInfixOf s `eqP` TL.isInfixOf (packT s)
310
310
311prop_T_elem c = L.elem c `eqP` T.elem c
311prop_T_elem c = L.elem c `eqP` T.elem c
312+prop_TL_elem c = L.elem c `eqP` TL.elem c
312prop_T_filter p = L.filter p `eqP` (unpackT . T.filter p)
313prop_T_filter p = L.filter p `eqP` (unpackT . T.filter p)
314+prop_TL_filter p = L.filter p `eqP` (unpackT . TL.filter p)
313prop_T_find p = L.find p `eqP` T.find p
315prop_T_find p = L.find p `eqP` T.find p
316+prop_TL_find p = L.find p `eqP` TL.find p
314prop_T_partition p = L.partition p `eqP` (unpack2 . T.partition p)
317prop_T_partition p = L.partition p `eqP` (unpack2 . T.partition p)
318+prop_TL_partition p = L.partition p `eqP` (unpack2 . TL.partition p)
315
319
316prop_T_index x s = x < L.length s && x >= 0 ==>
320prop_T_index x s = x < L.length s && x >= 0 ==>
317 (L.!!) s x == T.index (packT s) x
321 (L.!!) s x == T.index (packT s) x
@@ -508,9 +512,13 @@ tests = [
508 ("prop_TL_isInfixOf", mytest prop_TL_isInfixOf),
512 ("prop_TL_isInfixOf", mytest prop_TL_isInfixOf),
509
513
510 ("prop_T_elem", mytest prop_T_elem),
514 ("prop_T_elem", mytest prop_T_elem),
515+ ("prop_TL_elem", mytest prop_TL_elem),
511 ("prop_T_filter", mytest prop_T_filter),
516 ("prop_T_filter", mytest prop_T_filter),
517+ ("prop_TL_filter", mytest prop_TL_filter),
512 ("prop_T_find", mytest prop_T_find),
518 ("prop_T_find", mytest prop_T_find),
519+ ("prop_TL_find", mytest prop_TL_find),
513 ("prop_T_partition", mytest prop_T_partition),
520 ("prop_T_partition", mytest prop_T_partition),
521+ ("prop_TL_partition", mytest prop_TL_partition),
514
522
515 ("prop_T_index", mytest prop_T_index),
523 ("prop_T_index", mytest prop_T_index),
516 ("prop_T_findIndex", mytest prop_T_findIndex),
524 ("prop_T_findIndex", mytest prop_T_findIndex),