Skip to content
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

Insert into a set, returning the prior existence #23

Open
rehno-lindeque opened this issue Oct 21, 2018 · 1 comment
Open

Insert into a set, returning the prior existence #23

rehno-lindeque opened this issue Oct 21, 2018 · 1 comment

Comments

@rehno-lindeque
Copy link

Hi, I was taking a stab at a function that inserts an item into a set and returns whether it already existed prior to the operation.

-- The result is False if the item already exists in the set
insertIfMissing :: item -> Set item -> STM Bool

Obviously this can be done as a lookup followed by insert. However, I was wondering if it ought to be possible via focus?

E.g.

do
  let insertIfMissing :: a -> Focus a m Bool
       insertIfMissing element = do
         F.cases (True, F.Set element) (const (False, F.Leave))
  alreadyPresent <- focus insertIfMissing item set

focus expects Focus () STM result, which is explained in the docs:

The strategy is over a unit since we already know, which element we're focusing on and it doesn't make sense to replace it

Would it be fair to say that this use-case contradicts the rationale?

@nikita-volkov
Copy link
Owner

Seems like a perfectly valid use-case to me :)

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

No branches or pull requests

2 participants