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

Yuji Yamamoto whosekiteneverfly at gmail.com
Thu Oct 3 22:41:29 UTC 2019

Use DeriveGenerics, and higgledy <https://github.com/i-am-tom/higgledy> (or
some packages supporting higher kinded data) would help you.

2019年10月4日(金) 5:56 Sebastiaan Joosten <sjcjoosten+haskelcafe at gmail.com>:

> 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.
> Best,
> Sebastiaan
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.

twitter: https://twitter.com/igrep
GitHub: https://github.com/igrep
GitLab: https://gitlab.com/igrep
Facebook: http://www.facebook.com/igrep
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20191004/5e6fb312/attachment.html>

More information about the Haskell-Cafe mailing list