on master help sign in
Implement and test lazy group and groupBy

--HG-- extra : convert_revision : eb9cc3aa131144eab6c2660dacac66f83a06a137

commit815eeee0212aa8eb6553b550c5f6fa3b8436b408 authorBryan O'Sullivan <bos@serpentine.com> dateWed 4 Mar 2009, 05:52 (18 years ago) parent20d7203
2 files changed+27−2
Data/Text/Lazy.hs+23−2 tests/Properties.hs+4
Data/Text/Lazy.hs+23−2
@@ -101,8 +101,8 @@ module Data.Text.Lazy
101101 , splitAt
102102 , span
103103 , break
104− -- , group
105− -- , groupBy
104+ , group
105+ , groupBy
106106 -- , inits
107107 -- , tails
108108
@@ -609,6 +609,27 @@ span :: (Char -> Bool) -> Text -> (Text, Text)
609609span p = break (not . p)
610610{-# INLINE span #-}
611611
612+-- | The 'group' function takes a 'Text' and returns a list of 'Text's
613+-- such that the concatenation of the result is equal to the argument.
614+-- Moreover, each sublist in the result contains only equal elements.
615+-- For example,
616+--
617+-- > group "Mississippi" = ["M","i","ss","i","ss","i","pp","i"]
618+--
619+-- It is a special case of 'groupBy', which allows the programmer to
620+-- supply their own equality test.
621+group :: Text -> [Text]
622+group = groupBy (==)
623+{-# INLINE group #-}
624+
625+-- | The 'groupBy' function is the non-overloaded version of 'group'.
626+groupBy :: (Char -> Char -> Bool) -> Text -> [Text]
627+groupBy _ Empty = []
628+groupBy eq (Chunk t ts) = cons x ys : groupBy eq zs
629+ where (ys,zs) = span (eq x) xs
630+ x = T.unsafeHead t
631+ xs = chunk (T.unsafeTail t) ts
632+
612633revChunks :: [T.Text] -> Text
613634revChunks = L.foldl' (flip chunk) Empty
614635
tests/Properties.hs+4
@@ -234,7 +234,9 @@ prop_TL_span p = L.span p `eqP` (unpack2 . TL.span p)
234234prop_T_break p = L.break p `eqP` (unpack2 . T.break p)
235235prop_TL_break p = L.break p `eqP` (unpack2 . TL.break p)
236236prop_T_group = L.group `eqP` (map unpackT . T.group)
237+prop_TL_group = L.group `eqP` (map unpackT . TL.group)
237238prop_T_groupBy p = L.groupBy p `eqP` (map unpackT . T.groupBy p)
239+prop_TL_groupBy p = L.groupBy p `eqP` (map unpackT . TL.groupBy p)
238240prop_T_inits = L.inits `eqP` (map unpackT . T.inits)
239241prop_T_tails = L.tails `eqP` (map unpackT . T.tails)
240242
@@ -434,7 +436,9 @@ tests = [
434436 ("prop_T_break", mytest prop_T_break),
435437 ("prop_TL_break", mytest prop_TL_break),
436438 ("prop_T_group", mytest prop_T_group),
439+ ("prop_TL_group", mytest prop_TL_group),
437440 ("prop_T_groupBy", mytest prop_T_groupBy),
441+ ("prop_TL_groupBy", mytest prop_TL_groupBy),
438442 ("prop_T_inits", mytest prop_T_inits),
439443 ("prop_T_tails", mytest prop_T_tails),
440444