on master help sign in
Implement and test lazy group and groupBy
815eeeeBryan O'Sullivan
Data/Text/Lazy.hs+23−2
@@ -101,8 +101,8 @@ module Data.Text.Lazy
101 , splitAt
101 , splitAt
102 , span
102 , span
103 , break
103 , break
104− -- , group
104+ , group
105− -- , groupBy
105+ , groupBy
106 -- , inits
106 -- , inits
107 -- , tails
107 -- , tails
108
108
@@ -609,6 +609,27 @@ span :: (Char -> Bool) -> Text -> (Text, Text)
609span p = break (not . p)
609span p = break (not . p)
610{-# INLINE span #-}
610{-# INLINE span #-}
611
611
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+
612revChunks :: [T.Text] -> Text
633revChunks :: [T.Text] -> Text
613revChunks = L.foldl' (flip chunk) Empty
634revChunks = L.foldl' (flip chunk) Empty
614
635
tests/Properties.hs+4
@@ -234,7 +234,9 @@ prop_TL_span p = L.span p `eqP` (unpack2 . TL.span p)
234prop_T_break p = L.break p `eqP` (unpack2 . T.break p)
234prop_T_break p = L.break p `eqP` (unpack2 . T.break p)
235prop_TL_break p = L.break p `eqP` (unpack2 . TL.break p)
235prop_TL_break p = L.break p `eqP` (unpack2 . TL.break p)
236prop_T_group = L.group `eqP` (map unpackT . T.group)
236prop_T_group = L.group `eqP` (map unpackT . T.group)
237+prop_TL_group = L.group `eqP` (map unpackT . TL.group)
237prop_T_groupBy p = L.groupBy p `eqP` (map unpackT . T.groupBy p)
238prop_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)
238prop_T_inits = L.inits `eqP` (map unpackT . T.inits)
240prop_T_inits = L.inits `eqP` (map unpackT . T.inits)
239prop_T_tails = L.tails `eqP` (map unpackT . T.tails)
241prop_T_tails = L.tails `eqP` (map unpackT . T.tails)
240
242
@@ -434,7 +436,9 @@ tests = [
434 ("prop_T_break", mytest prop_T_break),
436 ("prop_T_break", mytest prop_T_break),
435 ("prop_TL_break", mytest prop_TL_break),
437 ("prop_TL_break", mytest prop_TL_break),
436 ("prop_T_group", mytest prop_T_group),
438 ("prop_T_group", mytest prop_T_group),
439+ ("prop_TL_group", mytest prop_TL_group),
437 ("prop_T_groupBy", mytest prop_T_groupBy),
440 ("prop_T_groupBy", mytest prop_T_groupBy),
441+ ("prop_TL_groupBy", mytest prop_TL_groupBy),
438 ("prop_T_inits", mytest prop_T_inits),
442 ("prop_T_inits", mytest prop_T_inits),
439 ("prop_T_tails", mytest prop_T_tails),
443 ("prop_T_tails", mytest prop_T_tails),
440
444