[Haskell-cafe] Avoiding code-duplication for what is roughly fmap

Sebastiaan Joosten sjcjoosten+haskelcafe at gmail.com
Thu Oct 3 20:55:24 UTC 2019

Hi all,

I'm writing a lot of code that looks like this:
instance WhiteSpaced ClassItem where
  removeWS (Method a b c) = Method (removeWS a) (removeWS b) (removeWS c)
  removeWS (Declaration b) = Declaration (removeWS b)

Typically, all the way at the end there's an instance that deviates
(sometimes the deviating instances are somewhere in the middle). I need to
do this for a lot of functions, and a lot of data types, and all I'm doing
here is rewriting the data-type declaration in a different syntax (except
that you do not know the types of a, b and c from the above). For the sake
of maintainability, I want to avoid this code-duplication and focus only on
the deviating instances.

How to do better? I don't see how to use generics (in the hope of only
writing 'instance WhiteSpaced ClassItem where' instead of the three lines
above) for this: the types for a, b and c are all different here. Would
this be easier with Template Haskell? (in the hope of only writing
$(''something ClassItem) instead of the three lines above)

My main concern is maintainability, an ideal solution is either a clear
one-liner or a library import (in the same way that aeson allows me to use
generics or Template Haskell without needing to know much about them).
Other solutions are welcome too.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20191003/d8fd0199/attachment.html>

More information about the Haskell-Cafe mailing list