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

sequence-like collections should have a group operation that lumps together equal subsequences #601

Open
ggreif opened this issue Dec 3, 2023 · 0 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@ggreif
Copy link
Contributor

ggreif commented Dec 3, 2023

Like Haskell's group.

Her is one implementation for arrays, that I came up with:

type Comp = { #less; #equal; #greater };
func group<A>(arr : [A], ca : (A, A) -> Comp) : [[A]] =
      switch (arr.size()) {
        case 0 [];
        case 1 [arr];
        case _ {
          let (outer, inner) = (Buffer.Buffer<[A]> 10, Buffer.Buffer<A> 5);
          let iter = arr.vals();
          let ?front = iter.next() else panic "BAD!";
          inner.add front;
          var cur = front;
          for (e in iter) {
            if (ca(cur, e) == #equal)
              inner.add e
            else {
              outer.add(inner.toArray());
              inner.clear();
              inner.add e;
              cur := e
            }
          };
          outer.add(inner.toArray());
          outer.toArray()
        }
    };

It should be easy to do the same for List.

This is also tracked as https://dfinity.atlassian.net/browse/LANG-331.

@ggreif ggreif changed the title subsequence-like collections should have a group operation that lumps together equal subsequences sequence-like collections should have a group operation that lumps together equal subsequences Dec 3, 2023
@ggreif ggreif added enhancement New feature or request good first issue Good for newcomers labels Dec 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant