Bryan O'Sullivan avatar Bryan O'Sullivan committed 1d2cce9

Implement and test lazy group and groupBy

Comments (0)

Files changed (2)

Data/Text/Lazy.hs

     , splitAt
     , span
     , break
-    -- , group
-    -- , groupBy
+    , group
+    , groupBy
     -- , inits
     -- , tails
 
 span p = break (not . p)
 {-# INLINE span #-}
 
+-- | The 'group' function takes a 'Text' and returns a list of 'Text's
+-- such that the concatenation of the result is equal to the argument.
+-- Moreover, each sublist in the result contains only equal elements.
+-- For example,
+--
+-- > group "Mississippi" = ["M","i","ss","i","ss","i","pp","i"]
+--
+-- It is a special case of 'groupBy', which allows the programmer to
+-- supply their own equality test.
+group :: Text -> [Text]
+group =  groupBy (==)
+{-# INLINE group #-}
+
+-- | The 'groupBy' function is the non-overloaded version of 'group'.
+groupBy :: (Char -> Char -> Bool) -> Text -> [Text]
+groupBy _  Empty        = []
+groupBy eq (Chunk t ts) = cons x ys : groupBy eq zs
+                          where (ys,zs) = span (eq x) xs
+                                x  = T.unsafeHead t
+                                xs = chunk (T.unsafeTail t) ts
+
 revChunks :: [T.Text] -> Text
 revChunks = L.foldl' (flip chunk) Empty
 

tests/Properties.hs

 prop_T_break p         = L.break p     `eqP` (unpack2 . T.break p)
 prop_TL_break p        = L.break p     `eqP` (unpack2 . TL.break p)
 prop_T_group           = L.group       `eqP` (map unpackT . T.group)
+prop_TL_group          = L.group       `eqP` (map unpackT . TL.group)
 prop_T_groupBy p       = L.groupBy p   `eqP` (map unpackT . T.groupBy p)
+prop_TL_groupBy p      = L.groupBy p   `eqP` (map unpackT . TL.groupBy p)
 prop_T_inits           = L.inits       `eqP` (map unpackT . T.inits)
 prop_T_tails           = L.tails       `eqP` (map unpackT . T.tails)
 
   ("prop_T_break", mytest prop_T_break),
   ("prop_TL_break", mytest prop_TL_break),
   ("prop_T_group", mytest prop_T_group),
+  ("prop_TL_group", mytest prop_TL_group),
   ("prop_T_groupBy", mytest prop_T_groupBy),
+  ("prop_TL_groupBy", mytest prop_TL_groupBy),
   ("prop_T_inits", mytest prop_T_inits),
   ("prop_T_tails", mytest prop_T_tails),
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.