foundation icon indicating copy to clipboard operation
foundation copied to clipboard

Add a method to the Sequential class to split on an element

Open lortabac opened this issue 7 years ago • 4 comments

The Sequential class doesn't have a method to split a collection on a specific element (f c = splitOn (== c)). This function is called splitOn in Data.Text and split in Data.ByteString. Should we call it split?

lortabac avatar Jun 10 '17 12:06 lortabac

I believe there is such function already

Foundation

Foundation actually provides the function to split on a specific element.

splitOn :: (Element c -> Bool) -> c -> [c]

let csv = "col1,col2,col3" :: String
splitOn (== ',') csv === ["col1", "col2", "col3"]

ByteString

ByteString provide a nearly similar one:

split :: Word8 -> ByteString -> [ByteString]

let csv = "col1,col2,col3" :: ByteString -- ignoring the Word8 to Char8 conversion stuff too...
split ',' csv === ["col1", "col2", "col3"]

Text

While it might look similar to ByteString's or Foundation's, there is a slight difference where here it is using an other Text to split on.

splitOn :: Text-> Text -> [Text]

let csv = "col1,col2,col3" :: Text
splitOn (singleton ',') csv === ["col1", "col2", "col3"]

NicolasDP avatar Jun 10 '17 13:06 NicolasDP

or did you mean you think we should provide a specialised function with Eq (Element col) as a constraint ?

NicolasDP avatar Jun 10 '17 13:06 NicolasDP

Yes, I mean a function with the Eq (Element col) constraint to split on a specific element instead of a predicate. I think this is common enough to be worth including.

lortabac avatar Jun 10 '17 13:06 lortabac

fair enough, do split is a good name indeed.

NicolasDP avatar Jun 10 '17 13:06 NicolasDP