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

Fix #91 make .zip() work for different types #94

Merged
merged 4 commits into from
Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions lib/src/iterable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -948,9 +948,23 @@ extension IterableX<E> on Iterable<E> {
///
/// Using the provided [transform] function applied to each pair of elements.
/// The returned list has length of the shortest collection.
Iterable<R> zip<R>(Iterable<E> other, R transform(E a, E b)) sync* {
var it1 = iterator;
var it2 = other.iterator;
///
/// Example (with added type definitions for [transform] parameters):
///
/// ```dart
///final amounts = [2, 3, 4];
///final animals = ['dogs', 'birds', 'cats'];
///final all = amounts.zip(
/// animals,
/// (int amount, String animal) => '$amount $animal'
///); // returns: ['2 dogs', '3 birds', '4 cats']
/// ```
Iterable<V> zip<R, V>(
Iterable<R> other,
V Function(E a, R b) transform,
) sync* {
final it1 = iterator;
final it2 = other.iterator;
while (it1.moveNext() && it2.moveNext()) {
yield transform(it1.current, it2.current);
}
Expand Down
45 changes: 41 additions & 4 deletions test/iterable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -793,10 +793,47 @@ void main() {
expect([3, 4, 5].union([4, 10, 20]), [3, 4, 5, 10, 20]);
});

test('.zip()', () {
expect([].zip([], (e1, e2) => null), []);
expect([1, 2, 3].zip([2, 4, 6], (e1, e2) => e1 / e2), [0.5, 0.5, 0.5]);
expect([2, 4, 6].zip([1, 2, 3], (e1, e2) => e1 / e2), [2.0, 2.0, 2.0]);
group('.zip()', () {
test('with same types', () {
expect([].zip([], (e1, e2) => null), []);
expect(
[1, 2, 3].zip([2, 4, 6], (e1, int e2) => e1 / e2),
[0.5, 0.5, 0.5],
);
expect(
[2, 4, 6].zip([1, 2, 3], (e1, int e2) => e1 / e2),
[2.0, 2.0, 2.0],
);

// with type definitions
expect(
[1, 2, 3].zip([2, 4, 6], (int e1, int e2) => e1 / e2),
[0.5, 0.5, 0.5],
);
});

test('with same types and different length', () {
expect([1, 2, 3].zip([2, 4], (e1, int e2) => e1 / e2), [0.5, 0.5]);
expect([2, 4].zip([2, 2, 2], (e1, int e2) => e1 / e2), [1.0, 2.0]);
});

test('with different types', () {
final amounts = [2, 3, 4];
final animals = ['dogs', 'birds', 'cats'];
expect(
amounts.zip(animals, (amount, String animal) => '$amount $animal'),
['2 dogs', '3 birds', '4 cats'],
);
});

test('with different types and different lengths', () {
final amounts = [2, 3];
final animals = ['dogs', 'birds', 'cats'];
expect(
amounts.zip(animals, (amount, String animal) => '$amount $animal'),
['2 dogs', '3 birds'],
);
});
});

test('.toIterable()', () {
Expand Down