Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
Revert "Add count and countWhere extensions. (#196)" (#201)
Browse files Browse the repository at this point in the history
This reverts commit ae0725c.
  • Loading branch information
natebosch authored Jun 25, 2021
1 parent 04eb292 commit 75a7a55
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 106 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
## 1.16.0

* Use a stable sort algorithm in the `IterableExtension.sortedBy` method.
* Add `count` and `countWhere` extensions on `Iterable`.

## 1.15.0

Expand Down
3 changes: 1 addition & 2 deletions lib/src/empty_unmodifiable_set.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

import 'dart:collection';

import 'unmodifiable_wrappers.dart';
import 'wrappers.dart';
import 'package:collection/collection.dart';

/// An unmodifiable, empty set which can be constant.
class EmptyUnmodifiableSet<E> extends IterableBase<E>
Expand Down
40 changes: 0 additions & 40 deletions lib/src/iterable_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,6 @@ import 'algorithms.dart';
/// iterables with specific element types include those of
/// [IterableComparableExtension] and [IterableNullableExtension].
extension IterableExtension<T> on Iterable<T> {
/// Counts the elements that are equal to [value].
///
/// Returns the number of elements of this iterable
/// which are equal to [value] according to their `==`
/// operation.
///
/// Example:
/// ```dart
/// var nullCount = maybeValues.count(null);
/// ```
///
/// Equivalent to `.countWhere((e) => e == value)`.
int count(T value) {
var result = 0;
for (var element in this) {
if (element == value) result++;
}
return result;
}

/// Counts the elements that satisfy [test].
///
/// Returns the number of elements of this iterable
/// which [test] returns `true` for.
/// The test must not modify this iterable.
///
/// Example:
/// ```dart
/// var primeCount = integers.countWhere(isPrime);
/// ```
///
/// Equivalent to `.where(test).length`.
int countWhere(bool Function(T element) test) {
var result = 0;
for (var element in this) {
if (test(element)) result++;
}
return result;
}

/// Selects [count] elements at random from this iterable.
///
/// The returned list contains [count] different elements of the iterable.
Expand Down
63 changes: 0 additions & 63 deletions test/extensions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,58 +10,6 @@ import 'package:test/test.dart';
void main() {
group('Iterable', () {
group('of any', () {
group('.count', () {
test('empty', () {
// Empty iterable.
var iterable = [1, 2, 3, 2, 3, 2].where((_) => false);
expect(iterable.count(1), 0);
expect(<int>[].count(1), 0);
});
test('none', () {
var iterable = [1, 2, 3, 4, 2, 3, 2].where((_) => true);
expect(iterable.count(0), 0);
expect(<int>[4].count(0), 0);
});
test('single', () {
var iterable = [1, 2, 3, 4, 2, 3, 2].where((_) => true);
expect(iterable.count(4), 1);
expect(<int>[4].count(4), 1);
});
test('multiple', () {
var iterable = [1, 2, 3, 4, 2, 3, 2].where((_) => true);
expect(iterable.count(2), 3);
expect(iterable.count(3), 2);
expect(<int>[2, 3, 2].count(2), 2);
});
test('uses element equality', () {
var iterable = <Object>[EqTo(2), 2];
expect(iterable.count(2), 2);
});
});
group('.countWhere', () {
test('empty', () {
// Empty iterable.
var iterable = [1, 2, 3, 2, 3, 2].where((_) => false);
expect(iterable.countWhere((_) => true), 0);
expect(<int>[].countWhere((_) => true), 0);
});
test('none', () {
var iterable = [1, 2, 3, 4, 2, 3, 2].where((_) => true);
expect(iterable.countWhere((_) => false), 0);
expect(<int>[4].countWhere((_) => false), 0);
});
test('single', () {
var iterable = [1, 2, 3, 4, 2, 3, 2].where((_) => true);
expect(iterable.countWhere((x) => x == 4), 1);
expect(<int>[4].countWhere((x) => x == 4), 1);
});
test('multiple', () {
var iterable = [1, 2, 3, 4, 2, 3, 2].where((_) => true);
expect(iterable.countWhere((x) => x == 2), 3);
expect(iterable.countWhere((x) => x == 3), 2);
expect(<int>[2, 3, 2].countWhere((x) => x == 2), 2);
});
});
group('.whereNot', () {
test('empty', () {
expect(iterable([]).whereNot(unreachable), isEmpty);
Expand Down Expand Up @@ -1755,14 +1703,3 @@ bool isEven(int x) => x.isEven;

/// Tests an integer for being odd.
bool isOdd(int x) => x.isOdd;

/// Objects which claim to be equal to other values.
class EqTo {
final Object? value;
EqTo(this.value);
@override
int get hashCode => value.hashCode;
@override
bool operator ==(Object other) =>
value == (other is EqTo ? other.value : other);
}

0 comments on commit 75a7a55

Please sign in to comment.