[GHC] #11815: Data.List: Add a function to get consecutive elements (mapConsecutives)
GHC
ghc-devs at haskell.org
Fri Apr 8 09:09:57 UTC 2016
#11815: Data.List: Add a function to get consecutive elements (mapConsecutives)
-------------------------------------+-------------------------------------
Reporter: holmisen | Owner:
Type: feature | Status: new
request |
Priority: low | Milestone:
Component: | Version:
libraries/base |
Keywords: | Operating System: Unknown/Multiple
Architecture: | Type of failure: None/Unknown
Unknown/Multiple |
Test Case: | Blocked By:
Blocking: | Related Tickets:
Differential Rev(s): | Wiki Page:
-------------------------------------+-------------------------------------
A recurring pattern is to get a list of all consecutive elements of a
list, for further processing.
I propose adding the following function to `Data.List` for this:
{{{
mapConsecutives :: (a -> a -> b) -> [a] -> [b]
mapConsecutives _ [] = []
mapConsecutives f xs = zipWith f xs (tail xs)
}}}
Since it requires pattern matching, to separate the empty case, it is not
practical to inline at each use site.
Sidenote: A similar function `mapAdjacent` is available in the `utility-
ht` library (with a partial implementation(!)) [1].
I realise that `Data.List` is often imported unqualified and hence
additions may cause trouble. I would have raised this on the libraries
mailing list first, but the guidelines for proposals pointed me here.
[1] http://hackage.haskell.org/package/utility-ht-0.0.11/docs/Data-List-
HT.html#v:mapAdjacent
--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/11815>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
More information about the ghc-tickets
mailing list