Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stdlib synonym to my zipWithMap function?

Tags:

haskell

I often find myself looking to use a function like:

zipWithMap :: (a -> b) -> [a] -> [(a, b)]
zipWithMap f xs = zip xs $ map f xs

Such that:

zipWithMap (+3) [1..4] = [(1, 4), (2, 5), (3, 6), (4, 7)]

I find myself looking for this so often, in fact, that I fear I am either reinventing the wheel or using an idiom so poorly-suited to Haskell that Real Programmers have moved away from it, thus its exclusion from stdlib.

Hoogle gives me nothing with that type signature, so I'm forced to ask: is there a more idiomatic approach? Perhaps with Arrows?

zipWithMap f = map $ id &&& f

though I'm not sure that's any better than the (subjectively) simpler style above.

like image 863
Adam Smith Avatar asked Apr 01 '26 22:04

Adam Smith


1 Answers

Also:

Data.Tuple.Extra>  map (second (+3) . dupe) [1..4]
[(1,4),(2,5),(3,6),(4,7)]
like image 162
Stéphane Laurent Avatar answered Apr 04 '26 10:04

Stéphane Laurent



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!