[Haskell-cafe] GADTs and Exponentiated Functors
Daniel Filonik
d.filonik at hdr.qut.edu.au
Sun Mar 6 12:11:19 UTC 2016
Hi,
I have been recently playing around with GADTs, using them to keep track of how many times a functor has been applied. This provides a solution to what seems to be a long standing problem, summarized here:
https://byorgey.wordpress.com/2007/08/16/mapping-over-a-nested-functor/
If the nesting depth is part of the type, it is possible to apply fmap automatically as needed. This allows you to write fairly elegant expressions like:
data Person = Person { name :: String, age :: Integer, gender :: String, status :: String } deriving Show
let persons = fromList' [Person {name="Alice", age=20, gender="F", status="Good"}, Person {name="Bob", age=18, gender="M", status="Good"}, Person {name="Chuck", age=16, gender="M", status="Bad"}] :: NList N1 Person
persons `select` age
-- [20,18,16]
persons `groupby` gender `select` age
-- [[20],[18,16]]
persons `groupby` gender `groupby` status `select` age
-- [[[20]],[[18],[16]]]
Note that "`select` age" works regardless of nesting depth. You can also append or remove nesting levels:
persons `groupby` gender `select` age `produce` (\x -> [0..x])
-- [[[0..20]],[[0..18],[0..16]]]
persons `groupby` gender `reduce` (sumof age)
-- [20, 34]
Would this kind of thing be of interest? The code is here:
https://github.com/filonik/nlists
Please feel free to suggest improvements.
Cheers,
Daniel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20160306/d48d439f/attachment.html>
More information about the Haskell-Cafe
mailing list