[Haskell-cafe] help with some code

Erik Rantapaa erantapaa at gmail.com
Sat Jul 12 03:39:26 UTC 2014


On Friday, July 11, 2014 10:01:38 AM UTC-5, gbwey9 wrote:
>
> Hi Cafe,
>
> I've got some code where a user can provide a filter to subscribe to a 
> particular type of event.  The events come in as xml and are parsed using 
> the toEvent method.
> The code works fine but is repetitive since each event record potentially 
> has the information needed to parse the xml using the field names and 
> types. 
>
> So is there a way to get rid of the repetition and streamline the code? 
> Should I be using TH or lenses or something else entirely?
>
> Here is the paste. 
> http://lpaste.net/107338 
> <http://www.google.com/url?q=http%3A%2F%2Flpaste.net%2F107338&sa=D&sntz=1&usg=AFQjCNGI_2_sqb7wOrLNKJBuTeDzCpLLBQ>
>
> Thanks for any pointers!
> Grant
>

Regarding how the defining the toEvent instances can be simplified, have a 
look at the FromJSON type class works in the aeson package. One technique 
they use is to define FromJSON instances for even the primitive types (e.g. 
Int, String, DateTime, etc.) so that the FromJSON instances of records can 
be entirely type-directed removing the need to reference specific 
deserialization functions (e.g.  getint, getbool, etc.)

One example of a package which uses aeson (of which there are many) is the 
github package:

https://github.com/jwiegley/github/blob/master/Github/Data.hs

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140711/97a742e4/attachment.html>


More information about the Haskell-Cafe mailing list