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

Enumerate all distinct values #7

Closed
Synthetica9 opened this issue Jun 20, 2019 · 1 comment · Fixed by #13
Closed

Enumerate all distinct values #7

Synthetica9 opened this issue Jun 20, 2019 · 1 comment · Fixed by #13
Milestone

Comments

@Synthetica9
Copy link

As the title states. I have a function as follows:

mergeEquivalent :: (Ord a) => (a -> a -> Bool) -> (a -> a -> a) -> ([a] -> [a])
mergeEquivalent (===) (+) xs = let
  -- https://github.com/quchen/articles/blob/master/2018-11-22_zipWith_const.md
  indices = zipWith const [0..] xs -- Because just [0..len] generates [0] when xs == []!
  combis = pairs indices

  update :: IntSet.IntSet -> (Int, Int) -> EQ.EquivM _ _ Int IntSet.IntSet
  update = \s (i1, i2) -> do
    eq <- EQ.equivalent i1 i2
    if eq
      then pure s
      else do
        x1 <- EQ.classDesc i1
        x2 <- EQ.classDesc i2
        if (x1 === x2)
          then do
            EQ.equate i1 i2
            pure $ IntSet.delete i1 s
          else
            pure s

  in EQ.runEquivM (xs !!) (+) $ do
    s <- foldM update (IntSet.fromList indices) combis
    classes <- for (IntSet.toList s) EQ.classDesc
    pure classes

But I have to manually keep track of the classes that exists. Is there a possibility that a function would be added that does this automatically? If I understand the implementation correctly, this should be possible in O(n)

An alternative would be to implement something like redundant :: v -> m Bool so that I can manually filter.

TL;DR:

I want an allElements :: m [v]

@Synthetica9 Synthetica9 changed the title Enumerate all distinct classes Enumerate all distinct values Jun 20, 2019
@andreasabel andreasabel linked a pull request Jul 26, 2022 that will close this issue
@andreasabel
Copy link
Collaborator

Fixed by

Scheduled for 0.4.1.

@andreasabel andreasabel added this to the 0.4.1 milestone Jul 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants