Conversation
michaelpj
left a comment
There was a problem hiding this comment.
I would definitely like to have these!
optics-core/src/Optics/Plated.hs
Outdated
|
|
||
| -- | Rewrite recursively over part of a larger structure. | ||
| rewriteOn :: (Is k A_Setter, Plated a) => Optic k is s t a a -> (a -> Maybe a) -> s -> t | ||
| rewriteOn b = over b . rewrite |
There was a problem hiding this comment.
I have wondered in the past why these functions exist (in lens). Composing rewrite with over b seems like something we could expect our users to do. After all, a user of a lens library is probably pretty used to stringing together combinations of transformations.
Maybe we can do without the *On variants.
There was a problem hiding this comment.
I copied the API of lens verbatim since I never used Plated, but you're right, it makes sense to provide MVP and extend if necessary.
optics-core/src/Optics/Plated.hs
Outdated
| ------------------------------------------------------------------------------- | ||
|
|
||
| -- | Perform a fold-like computation on each value, technically a paramorphism. | ||
| paraOf :: Is k A_Fold => Optic' k is a a -> (a -> [r] -> r) -> a -> r |
There was a problem hiding this comment.
I don't know to what degree we want to mimic the exact structure of lens, but arguably it would make sense to put the *Of variants in the modules for the optics they use, e.g. paraOf in Fold, traverseOf in Setter etc, leaving this module to contain the versions that actually use Plated.
(Which I just noticed is exactly how it is for partsOf already.)
There was a problem hiding this comment.
Indeed. I moved paraOf to Optics.Fold and removed para and parts for now.
There was a problem hiding this comment.
Oh, I didn't read this suggestion well enough it seems. Yeah, having Optics.Plated only have the functions with plate applied also sounds reasonable.
optics-core/src/Optics/Plated.hs
Outdated
| universeOf :: Is k A_Fold => Optic' k is a a -> a -> [a] | ||
| universeOf l = go | ||
| where | ||
| go a = a : foldMapOf l go a |
There was a problem hiding this comment.
I know lens doesn't do this either, but is there a reason not to define universe* fucntions in terms of cosmos* and toListOf?
There was a problem hiding this comment.
I have no idea, sadly I never used these functions so have no intuition.
|
Any updates on this PR? I'd be happy to help get it over the line. |
|
I wonder if class Plated a where
plate :: Applicative f => (a -> f b) -> s -> f tI don't know if that's impossible with optics' |
|
I've updated the PR and stripped the API down. Could you tell me if that's ok? I've never used I'm even wondering about removing |
The last commit performs that experiment. It looks the most reasonable to me 🤔 Thoughts? |
|
@mikeplus64 this is a good idea. The only problem is the default instance would be missing, which might or might not be a big deal. |
|
In my opinion EDIT: I'm 👍 with adding non |
|
@phadej I'm with you on this one. I don't see the point of having I think this PR can be merged as-is. |
michaelpj
left a comment
There was a problem hiding this comment.
This looks great to me, very neat!
|
|
||
| -- | Transform every element by recursively applying a given 'Setter' in a | ||
| -- bottom-up manner. | ||
| transformOf :: Is k A_Setter => Optic k is a b a b -> (b -> b) -> a -> b |
There was a problem hiding this comment.
Is it deliberate that the *OnOf variants are left out? They are useful when you e.g. want to transform all Pats inside Exp:
transformOnOf patExps expPlated modifyPat :: Exp -> Exp where
patExps :: Setter' Pat Exp
expPlated :: Setter' Pat Pat
modifyPlated :: Pat -> PatOf course I can do over patExps (transformOf expPlated modifyPlated)...
There was a problem hiding this comment.
Of course I can do over patExps (transformOf expPlated modifyPlated)...
Yeah, that's what was also mentioned in #379 (comment), I think these are a bit excessive.
We can always add them later if there is enough demand.
optics-core/src/Optics/Fold.hs
Outdated
| universeOf :: Is k A_Fold => Optic' k is a a -> a -> [a] | ||
| universeOf l = go | ||
| where | ||
| go a = a : foldMapOf l go a |
There was a problem hiding this comment.
this is slow for trees. ++ can easily make this quadratic. I don't know why DList = Endo [a] is not used here (and in lens) instead.
There was a problem hiding this comment.
Can you give an example?
I don't know why DList = Endo [a] is not used here (and in lens) instead.
Me neither, I assume since lens has it like that it's fine 🤔 It can be optimized later if necessary.
There was a problem hiding this comment.
I updated PR to reflect changes made to lens.
It needs a bit of polish wrt. documentation. Do we want to have it in 0.4?