[Haskell-cafe] create a record with all record accessors

Henning Thielemann lemming at henning-thielemann.de
Sun Feb 16 20:05:16 UTC 2014


I have records of type (f a) where all fields are of type "a". I want to 
create records that contain all field getters as elements. I can do this 
generically:

getters ::
    (Traversable f, Applicative f) =>
    f (f a -> a)
getters =
    fmap (\n x -> Fold.toList x !! n) $
    MS.evalState (Trav.sequenceA (pure (MS.state $ \n -> (n, succ n)))) 0

Applicative is only needed for "pure". That is, "Pointed f" would be enough.

However, (!!) is not total and not very efficient. Is there already a 
type class in a package with "getters" as method or something I can 
build an efficient "getters" from? I suspect the answer lies in one of 
the "lens" modules. :-)


More information about the Haskell-Cafe mailing list