Proposal: Add mapWithIndex to Data.List
David Feuer
david.feuer at gmail.com
Sun Feb 1 01:21:02 UTC 2015
Vectors offer `imap` and sequences offer `mapWithIndex`. I think lists
deserve to have something like this too. The obvious implementation,
using `zipWith`, is unlikely to be the most efficient (because
foldr/build fusion doesn't handle zips so wonderfully).
To a first approximation:
mapWithIndex :: (Int -> a -> b) -> [a] -> [b]
mapWithIndex f xs = build $ \c n ->
let go x cont !i = f i x `c` cont (i+1)
in foldr go (`seq` n) 0
More information about the Libraries
mailing list