Proposal: Add split and splitWith (trac #2048)

Christian Maeder Christian.Maeder at dfki.de
Fri Jan 18 04:48:44 EST 2008


Duncan Coutts wrote:
> If everyone thinks this is the right behaviour we can certainly change
> Data.ByteString to follow. It is supposed to follow the Data.List api.

Good, lines can be best shortly expressed in terms of split as follows:

mylines s = if null (last l) then init l else l
            where l = split '\n' s

This is safe (though inefficient), because split always returns a
non-empty list (in contrast to Data.ByteString.Char8.split)!

Furthermore Data.PackedString.splitPS corresponds to lines.

Below are some test case.

Cheers Christian

import qualified Data.ByteString.Char8 as B
import qualified Data.PackedString as P

*GHCI> l
["","a","\n","aa","a\n","\na","\n\n"]
*GHCI> map (intercalate "\n" . split '\n') l
["","a","\n","aa","a\n","\na","\n\n"]
*GHCI> map (B.intercalate (B.pack "\n") . B.split '\n' . B.pack) l
["","a","\n","aa","a\n","\na","\n\n"]
*GHCI> map (unlines . lines) l
["","a\n","\n","aa\n","a\n","\na\n","\n\n"]
*GHCI> map lines l
[[],["a"],[""],["aa"],["a"],["","a"],["",""]]
*GHCI> map (lines . unlines . lines) l
[[],["a"],[""],["aa"],["a"],["","a"],["",""]]
*GHCI> map mylines l
[[],["a"],[""],["aa"],["a"],["","a"],["",""]]
*GHCI> map (P.splitPS '\n' . P.packString) l
[[],["a"],[""],["aa"],["a"],["","a"],["",""]]
*GHCI> map (B.split '\n' . B.pack) l
[[],["a"],["",""],["aa"],["a",""],["","a"],["","",""]]
*GHCI> map (split '\n') l
[[""],["a"],["",""],["aa"],["a",""],["","a"],["","",""]]



More information about the Libraries mailing list