on master help sign in
Implement lazy chunksOf.
41d8407Bryan O'Sullivan
Data/Text/Lazy.hs+15
@@ -115,6 +115,7 @@ module Data.Text.Lazy
115 -- ** Breaking into many substrings
115 -- ** Breaking into many substrings
116 , split
116 , split
117 , splitWith
117 , splitWith
118+ , chunksOf
118 -- , breakSubstring
119 -- , breakSubstring
119
120
120 -- ** Breaking into lines and words
121 -- ** Breaking into lines and words
@@ -775,6 +776,20 @@ splitWith p (Chunk t0 ts0) = comb [] (T.splitWith p t0) ts0
775 comb _ [] _ = impossibleError "splitWith"
776 comb _ [] _ = impossibleError "splitWith"
776{-# INLINE splitWith #-}
777{-# INLINE splitWith #-}
777
778
779+-- | /O(n)/ Splits a 'Text' into components of length @k@. The last
780+-- element may be shorter than the other chunks, depending on the
781+-- length of the input. Examples:
782+--
783+-- > chunksOf 3 "foobarbaz" == ["foo","bar","baz"]
784+-- > chunksOf 4 "haskell.org" == ["hask","ell.","org"]
785+chunksOf :: Int64 -> Text -> [Text]
786+chunksOf k = go
787+ where
788+ go t = case splitAt k t of
789+ (a,b) | null a -> []
790+ | otherwise -> a : go b
791+{-# INLINE chunksOf #-}
792+
778-- | /O(n)/ Breaks a 'Text' up into a list of 'Text's at
793-- | /O(n)/ Breaks a 'Text' up into a list of 'Text's at
779-- newline 'Char's. The resulting strings do not contain newlines.
794-- newline 'Char's. The resulting strings do not contain newlines.
780lines :: Text -> [Text]
795lines :: Text -> [Text]
tests/Properties.hs+3
@@ -390,6 +390,8 @@ t_chunksOf_same_lengths k = all ((==k) . T.length) . ini . T.chunksOf k
390t_chunksOf_length k t = len == T.length t || (k <= 0 && len == 0)
390t_chunksOf_length k t = len == T.length t || (k <= 0 && len == 0)
391 where len = L.sum . L.map T.length $ T.chunksOf k t
391 where len = L.sum . L.map T.length $ T.chunksOf k t
392
392
393+tl_chunksOf k = T.chunksOf k `eq` (map (T.concat . TL.toChunks) . TL.chunksOf (fromIntegral k) . TL.fromChunks . (:[]))
394+
393t_breakSubstring_isInfixOf s l
395t_breakSubstring_isInfixOf s l
394 = T.isInfixOf s l ==
396 = T.isInfixOf s l ==
395 T.null s || (not . T.null . snd $ C.breakSubstring s l)
397 T.null s || (not . T.null . snd $ C.breakSubstring s l)
@@ -743,6 +745,7 @@ tests = [
743 testProperty "tl_splitWith" tl_splitWith,
745 testProperty "tl_splitWith" tl_splitWith,
744 testProperty "t_chunksOf_same_lengths" t_chunksOf_same_lengths,
746 testProperty "t_chunksOf_same_lengths" t_chunksOf_same_lengths,
745 testProperty "t_chunksOf_length" t_chunksOf_length,
747 testProperty "t_chunksOf_length" t_chunksOf_length,
748+ testProperty "tl_chunksOf" tl_chunksOf,
746 testProperty "t_breakSubstringC" t_breakSubstringC,
749 testProperty "t_breakSubstringC" t_breakSubstringC,
747 testProperty "t_breakSubstring_isInfixOf" t_breakSubstring_isInfixOf
750 testProperty "t_breakSubstring_isInfixOf" t_breakSubstring_isInfixOf
748 ],
751 ],