DanBurton / lens-family-th

Template Haskell to generate lenses for lens-family and lens-family-core
BSD 3-Clause "New" or "Revised" License
7 stars 4 forks source link

Generic make traversals #8

Closed Gabriella439 closed 10 years ago

Gabriella439 commented 10 years ago

Would it be possible to add something like makePrisms from lens, except that it just generates traversals instead? The idea is that you have a data type like:

data T a b c = A a | B b | C c

... it would generate:

_A :: Applicative f => (a -> f a) -> T -> f T
_A k (A a) = fmap A (k a)
_A _  t    = pure t

_B :: Applicative f => (b -> f b) -> T -> f T
_B k (B b) = fmap B (k b)
_B _  t    = pure t

_C :: Applicative f => (c -> f c) -> T -> f T
_C k (C c) = fmap C (k c)
_C _  t    = pure t
DanBurton commented 10 years ago

Sure, that doesn't seem too hard. I think I can generate it without the type signatures following mostly the same pattern that I've already got for makeLenses. Pull requests welcome, or I'll probably get around to it myself this weekend.

DanBurton commented 10 years ago

@Gabriel439, I hacked up makeTraversals (how's that name?) today. Try installing it from the git repo and let me know if this suits your needs. If you think it works okay then I'll go ahead and release it to hackage.

Note: Things that haven't been implemented yet:

Gabriella439 commented 10 years ago

I tried it and it works fantastic! In my case I only need single-field constructors and the inferred types, so this works great for me. Thanks a lot! :)

DanBurton commented 10 years ago

Okay, I just pushed lens-family-th v0.3 to hackage with this functionality. In other news, I also added lens-family-th to Stackage.