Skip to content

Add inon, a version of non to be used for caching#916

Open
ddssff wants to merge 2 commits intoekmett:masterfrom
ddssff:master
Open

Add inon, a version of non to be used for caching#916
ddssff wants to merge 2 commits intoekmett:masterfrom
ddssff:master

Conversation

@ddssff
Copy link
Contributor

@ddssff ddssff commented Mar 24, 2020

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.

-- Using 'inon' instead of 'non' means that key will not be deleted
-- from the map:
--
-- >>> fromList [("user1", "!!!")] & (at "user1" . inon def) .~ ""
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

@ddssff ddssff Mar 24, 2020

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.

Copy link
Contributor Author

@ddssff ddssff Mar 24, 2020

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)
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

Copy link
Collaborator

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.

Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants