Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uses of Wrapped typeclass in Control.Lens

I'm having a hard time reading the types in the Control.Lens.Wrapped module and I'm not finding much else written about it or the newtype library it is apparently based on. In particular I am wondering what sorts of things I can do while remaining in the newtype wrapper. For instance let's say I have two lists that are in a Data.Monoid.Sum wrapper. Is there some way I can easily ++ the lists and get back a new value still in a Sum wrapper? I would want a function that, monomorphically, looks something like this:

canIdoThis :: ([a] -> [a] -> [a]) -> Sum [a] -> Sum [a] -> Sum [a]

so that I could do something like

canIdoThis (++) (Sum [1,2]) (Sum [3,4])

and get Sum [1,2,3,4]. (Of course, canIdoThis would also work on Product [a], etc.)

Any other tips or just a general write up of all the goodness one could do with Control.Lens.Wrapped is appreciated very much.

like image 982
Omari Norman Avatar asked Jan 21 '26 06:01

Omari Norman


1 Answers

Doesn't use Wrapped, but:

import Data.Coercible

canIdoThis :: ([a] -> [a] -> [a]) -> Sum [a] -> Sum [a] -> Sum [a]
canIdoThis = coerce

Are you sure these should be wrapped in Sum? No instance Num [a] by default.

like image 189
Gurkenglas Avatar answered Jan 23 '26 19:01

Gurkenglas