[Haskell-cafe] Specializing argument to forM_, mapM_ & foldM_ in base-4.8
Amit Aryeh Levy
amit at amitlevy.com
Thu Oct 15 17:16:48 UTC 2015
I've been running into a relatively small but frequent annoyance with
base >= 4.8 (GHC 7.10). `Control.Monad.foldM_`, `Control.Monad.mapM_`
and `Control.Monad.forM_` are generalized traverse over any `Foldable a`
rather than just arrays (`[a]`).
This is great, except I'm finding that, for a lot of my code that works
well in previous versions, I need to specialize the argument to `[a]`
now. If other people are encoutering a similar patter, I wonder what are
your best practices for doing this: ScopedTypeVariables? Deconstruct the
reconstruct the array? ...
The most common example is when I deserialize a JSON array with aeson
and want to traverse over that array (say, to store the objects to a DB):
```
let objArray = eitherDecode myjson
case objArray of
Left err -> ...
Right (objs :: [MyObjType]) ->
forM_ objs $ \obj -> saveToDb obj
```
The above fix requires `ScopedTypeVariables` (which is probably OK).
Another option is to deconstruct and reconstruct the list:
```
Right (o:objs) ->
forM_ (o:objs) $ \obj -> saveToDb obj
```
Does this get optimized away?
Penny for your thoughts?
Cheers!
Amit
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 473 bytes
Desc: OpenPGP digital signature
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20151015/a4b96bd0/attachment.sig>
More information about the Haskell-Cafe
mailing list