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

Add subtract to IterableExtensions #306

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 1.19.0-wip

- Adds `subtract` to `IterableExtension`.
- Adds `shuffled` to `IterableExtension`.
- Shuffle `IterableExtension.sample` results.

Expand Down
25 changes: 25 additions & 0 deletions lib/src/iterable_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,31 @@ extension IterableExtension<T> on Iterable<T> {
yield slice;
}
}

/// Returns an iterable of the elements in this iterable that are not present
/// in [other].
///
/// It's aware about occurrence count and excludes elements only the number of
/// times they occurred in [other] but it's indifferent about order of items
/// in [other].
///
/// It assumes the iterable items have consistent `==` and `hashCode`.
Iterable<T> subtract(Iterable<T> other) sync* {
var elementsCount = <T, int>{};
for (var element in other) {
elementsCount[element] = (elementsCount[element] ?? 0) + 1;
}
for (var element in this) {
var count = elementsCount[element];
if (count == null) {
yield element;
} else if (count == 1) {
elementsCount.remove(element);
} else {
elementsCount[element] = count - 1;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A switch with patterns can be shorter:

  switch (elementsCount[element]) {
    case null: yield element;
    case > 1 && var count: elementsCount[element] = count - 1;
    case == 1: elementsCount.remove(element);
  }

}
}
}

/// Extensions that apply to iterables with a nullable element type.
Expand Down
17 changes: 17 additions & 0 deletions test/extensions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1262,6 +1262,23 @@ void main() {
expect(l3.toList(), [4, 5]);
});
});
group('.subtract', () {
test('empty', () {
expect(iterable(<int>[]).subtract([]), []);
});
test('returns the same list when it shares nothing with the other', () {
expect(iterable([1, 2, 3, 4]).subtract([5]), [1, 2, 3, 4]);
});
test('removes two element', () {
expect(iterable([1, 2, 3, 4]).subtract([2, 3]), [1, 4]);
});
test('removes only one of the occurrence', () {
expect(iterable([1, 2, 2, 3]).subtract([2]), [1, 2, 3]);
});
test('removes only two of the occurrences', () {
expect(iterable([1, 2, 2, 3]).subtract([2, 0, 2]), [1, 3]);
});
});
});

group('Comparator', () {
Expand Down