-
Notifications
You must be signed in to change notification settings - Fork 273
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
Add inon, a version of non to be used for caching #916
base: master
Are you sure you want to change the base?
Conversation
src/Control/Lens/Iso.hs
Outdated
-- Using 'inon' instead of 'non' means that key will not be deleted | ||
-- from the map: | ||
-- | ||
-- >>> fromList [("user1", "!!!")] & (at "user1" . inon def) .~ "" |
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.
Don't
fromList [("user1", "!!!")] & at "user1" ?~ ""
has same effect?
And for caching, why the removal of defautl value would be bad. I don't understand that either.
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'll answer the second question. The scenario is that your cache is a Map, and the set of keys is an important piece of information, you don't want to forget that a key was once an element of the map. Picture the user list of a web app, you don't want the user to disappear if they reset their user profile to the default.
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.
As for the first question, yes that has the same effect, but in my use case I am constructing the lens with non and then composing further lenses to look inside the value. This lens gets passed to a function, so we no longer have access to the lens that looks at the map itself. I guess I dropped that last bit out of my example.
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.
like this:
>>> (fromList [("user1", "axc")]) & (at "user1" . non "abc" . ix 1) .~ 'b'
fromList []
-- >>> fromList [("user1", "axc")] & (at "user1" . inon "abc" . ix 1) .~ 'b' | ||
-- fromlist [("user1", "abc")] | ||
inon :: a -> Iso' (Maybe a) a | ||
inon a = anon a (const False) |
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 have a gut feeling that you be ok with inon :: a -> Setter (Maybe a) a
, which isn't as unlawful. (It still changes Nothing
to Just a
when you %~ id
).
I'd ask you to make it a setter and move to Unsound
module.
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.
Then I would have to pass both a getter and a setter to the function, which seems the opposite direction from where I am trying to go. I will give it some thought.
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'll think more about this too.
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.
Ultimately I just started writing anon v (const False)
to avoid this problem.
I had a long bewildering experience using regular non for maintaining a cache, so I thought this modification might be a useful addition to the library.