-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds "monadic" lenses and prisms #129
base: main
Are you sure you want to change the base?
Conversation
src/Data/Lens/Traversal.purs
Outdated
element :: | ||
forall p s t a. | ||
Wander p => | ||
Int -> | ||
Traversal s t a a -> | ||
Optic p s t a a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you mind preserving the existing formatting? That'll just help this PR stay easy to review and if we switch to arrow-last style we can apply it in one commit across the repository.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, is there a vs code setting that can bring that formatting in? Apologies for the clutter!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed the formatting back now 👍
I also marked it ready for review.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We haven’t formally adopted a formatter, so in these libraries it’s mostly about following the existing formatting until we do adopt one. Thanks!
I don't think I've seen optics used in this way before, do you have any example usages? |
The hackage package looks interesting, and the signatures seem similar. The example comes from my current sketches for the next iteration of https://github.com/mikesol/purescript-audio-behaviors. In it, the previous audio graph
To do this, I wound up writing a bunch of functions that produced stuff like I see that the hackage package is separate from the main lens one, so maybe these functions are too exotic to be in the |
Another example from our code base at Meeshkan: remove :: Int -> Script8Base -> Script8Base
remove sIndex script = widthAdjustedSIndex
where
items' = preview items script
withModifiedCount = set count (maybe 0 (add (-1) <<< A.length) items') script
withRemovedCommand = over items (\x -> fromMaybe x (A.deleteAt sIndex x)) withModifiedCount
widthAdjustedSIndex = over items (A.mapWithIndex (\i a -> a { sIndex = i })) withRemovedCommand Here, the |
Maybe a silly question, as I don't fully understand what you are doing, but
Maybe then you might find some solution in looking at creating optic using functions too? That is, could you create a lens using functions to achieve what you are wanting instead? In other words, come up with a profunctor to instantiate optics in a way to achieve what you are wanting. For example,
which is very much looking like the sort of thing you were thinking about when
This is also the classic container traversal definition for some applicative So, if |
This is a draft PR to create utilities for working with monads in lenses.
It currently covers
_1M
,_2M
,_LeftM
and_RightM
. I'd like to dotraversed
andpropM
as well, after which I think it'll be in good shape for review.propM
seems manageable buttraversedM
, with signaturetraversedM :: forall t a b m. Traversable t => Traversal (t a) (m (t b)) a (m b)
, seems quite daunting. Essentially you'd need to "apply traverse twice" but I'm not sure if that's possible and what that function would look like. If anyone would be interested in writingtraversedM
that'd be helpful!Checklist: