List function suggestions

From HaskellWiki
Revision as of 18:53, 18 August 2006 by JaredUpdike (talk | contribs) (added some category tags)
Jump to navigation Jump to search

Let's fix this

We need useful functions, with working names I'll call 'replace' and 'splitBy' in Data.List. These are easily implemented but everyone always reinvents them. The goal is clarity/uniformity (everyone uses them widely and recognizes them) and portability (I don't have to keep reimplementing these or copying that one file UsefulMissingFunctions.hs).

Use this page to record consensus as reached on the Talk Page. (Use four tildes to sign your post automatically with your name/timestamp.) Diverging opinions welcome!

Summary

Hacking up your own custom split (or a tokens/splitOnGlue) must be one of the most common questions from beginners on the irc channel.

Anyone rememeber what the result of the "let's get split into the base library" movement's work was?

ISTR there wasn't a concensus, so nothing happened. Which is silly, really - I agree we should definitely have a Data.List.split.

A thread July 2006

http://www.haskell.org/pipermail/haskell-cafe/2006-July/thread.html#16559

A thread July 2004

http://www.haskell.org/pipermail/libraries/2004-July/thread.html#2362

Goal:

Reach a consensus, on naming and semantics. Even if we need pairs of functions to satisfy various usage and algebraic needs.

Note: I (Jared Updike) am working from the belief that efficiency should not be a valid argument to bar these otherwise universally useful functions from the libraries; regexes are overkill for 'splitBy' and 'replace' for common simple situations. Let's assume people will learn when they need heavier machinery and will use it when the time is ripe. We can facilitate this by reusing any names from FastPackedString and/or ByteString, etc.

splitOn (working name)

We need at leat two of these:

splitOn :: Eq a => [a] -> [a] -> a splitOn' :: Eq a => [a] -> [a] -> a

1. One that preserves

concat $ intersperse sep $ splitOn sep x === x

2. One that uses the above splitOn and does a filter to remove empty elements but does not preserve above property. Easy enough:

splitOn' sep x = filter (/=[]) (splitOn sep x)

Maybe two more:

3. splitOnBy :: (a -> Bool) -> [a] -> [a] -> a 4. splitOnBy' :: (a -> Bool) -> [a] -> [a] -> a

mirroring groupBy, sortBy, etc.

TODO: give code, copy-paste from threads mentioned above TODO: list names and reasons for/against

replace (working name)

like Python replace

replace "the" "a" "the quick brown fox jumped over the lazy black dog" ===
"a quick brown fox jumped over a lazy black dog"

TODO: give code, copy-paste from threads mentioned above TODO: list names and reasons for/against

join (working name)

like Python join:

join sep = concat . intersperse sep

TODO: give code, copy-paste from threads mentioned above TODO: list names and reasons for/against

other favorites

Such as endsWith, beginsWith, etc.

TODO: copy-paste from threads mentioned above, or from your own code