Proposal: Add mapWithIndex to Data.List

David Feuer david.feuer at gmail.com
Sun Feb 1 13:23:11 UTC 2015


What is a Stream in this context?
On Feb 1, 2015 3:39 AM, "Henning Thielemann" <lemming at henning-thielemann.de>
wrote:

>
> On Sat, 31 Jan 2015, David Feuer wrote:
>
>  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
>>
>
>
> It can also be written using Traversable. I have once written the
> following more general function for this purpose:
>
> zipWithTraversable ::
>    (Traversable f) => (a -> b -> c) -> Stream a -> f b -> f c
> zipWithTraversable f as0 =
>    snd . mapAccumL (\(Stream.Cons a as) b -> (as, f a b)) as0
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/libraries/attachments/20150201/66c66c2c/attachment.html>


More information about the Libraries mailing list