Commits

Bryan O'Sullivan committed 532de04

Implement splitTimesEnd.

  • Participants
  • Parent commits 47b3cc0

Comments (0)

Files changed (2)

     -- $split
     , split
     , splitTimes
+    , splitTimesEnd
     , splitChar
     , splitWith
     , chunksOf
             | otherwise          = search (n+1) (unsafeTail s)
 {-# INLINE splitTimes #-}
 
+-- | /O(m)*O(n)/ Break a 'Text' into pieces at most @k@ times, like
+-- 'splitTimes', but start from the end of the input and work towards
+-- the start.
+--
+-- Examples:
+--
+-- > splitTimes 2    "::" "a::b::c::d::e" == ["a","b","c::d::e"]
+-- > splitTimesEnd 2 "::" "a::b::c::d::e" == ["a::b::c","d","e"]
+splitTimesEnd :: Int               -- ^ Maximum number of times to split
+              -> Text              -- ^ Text to split on
+              -> Text              -- ^ Input text
+              -> [Text]
+splitTimesEnd k pat src =
+    L.reverse . L.map reverse $ splitTimes k (reverse pat) (reverse src)
+{-# INLINE splitTimesEnd #-}
+
 -- | /O(n)/ Splits a 'Text' into components delimited by separators,
 -- where the predicate returns True for a separator element.  The
 -- resulting components do not contain the separators.  Two adjacent

tests/Properties.hs

 prop_TL_tails          = L.tails       `eqP` (map unpackS . TL.tails)
 
 prop_T_split_i t       = id `eq` (T.intercalate t . T.split t)
+prop_T_splitTimes_i k t= id `eq` (T.intercalate t . T.splitTimes k t)
+prop_T_splitTimesEnd_i k t = id `eq` (T.intercalate t . T.splitTimesEnd k t)
 prop_T_splitChar_i c   = id `eq` (T.intercalate (T.singleton c) . T.splitChar c)
 prop_TL_split_i c      = id `eq` (TL.intercalate (TL.singleton c) . TL.split c)
 
   ("prop_TL_tails", mytest prop_TL_tails),
 
   ("prop_T_split_i", mytest prop_T_split_i),
+  ("prop_T_splitTimes_i", mytest prop_T_splitTimes_i),
+  ("prop_T_splitTimesEnd_i", mytest prop_T_splitTimesEnd_i),
   ("prop_T_splitChar_i", mytest prop_T_splitChar_i),
   ("prop_TL_split_i", mytest prop_TL_split_i),
   ("prop_T_splitWith", mytest prop_T_splitWith),