0

How does the following work? Given a list of items [a] and a function mapping a to g b, how do I preserve the input in the resulting list?

[a] -> (a -> g b) -> g [(a,b)]

Concretely, I have a list of contract ids. I want to map every contract id to the tuple of (id, payload), where payload is some contract variable. And g b in the above example is fetch.

therat2000
  • 61
  • 1

1 Answers1

1

It's a bit easier if you flip the argument order, then you can do:

mapAWithArgs : Applicative m => (a -> m b) -> [a] -> m [(a, b)]
mapAWithArgs f = mapA (withArg f)
  where
    withArg f x = (x,) <$> f x

Then you can do withIds <- mapAWithArgs fetch <list of contract IDs> in your Update

oggy
  • 3,483
  • 1
  • 20
  • 24
  • While @oggy is right that it is easier to define with the alternate argument order, once you have defined `mapAWithArgs`, you can recover the original argument order as `flip mapAWithArgs`. – Recurse Nov 01 '19 at 00:06