Skip to content

Commit bbfb05d

Browse files
authored
Merge pull request #94 from mreichelt/91-make-zip-work-for-different-types
2 parents 1607043 + b0c3ded commit bbfb05d

File tree

2 files changed

+58
-7
lines changed

2 files changed

+58
-7
lines changed

lib/src/iterable.dart

+17-3
Original file line numberDiff line numberDiff line change
@@ -948,9 +948,23 @@ extension IterableX<E> on Iterable<E> {
948948
///
949949
/// Using the provided [transform] function applied to each pair of elements.
950950
/// The returned list has length of the shortest collection.
951-
Iterable<R> zip<R>(Iterable<E> other, R transform(E a, E b)) sync* {
952-
var it1 = iterator;
953-
var it2 = other.iterator;
951+
///
952+
/// Example (with added type definitions for [transform] parameters):
953+
///
954+
/// ```dart
955+
///final amounts = [2, 3, 4];
956+
///final animals = ['dogs', 'birds', 'cats'];
957+
///final all = amounts.zip(
958+
/// animals,
959+
/// (int amount, String animal) => '$amount $animal'
960+
///); // returns: ['2 dogs', '3 birds', '4 cats']
961+
/// ```
962+
Iterable<V> zip<R, V>(
963+
Iterable<R> other,
964+
V Function(E a, R b) transform,
965+
) sync* {
966+
final it1 = iterator;
967+
final it2 = other.iterator;
954968
while (it1.moveNext() && it2.moveNext()) {
955969
yield transform(it1.current, it2.current);
956970
}

test/iterable_test.dart

+41-4
Original file line numberDiff line numberDiff line change
@@ -793,10 +793,47 @@ void main() {
793793
expect([3, 4, 5].union([4, 10, 20]), [3, 4, 5, 10, 20]);
794794
});
795795

796-
test('.zip()', () {
797-
expect([].zip([], (e1, e2) => null), []);
798-
expect([1, 2, 3].zip([2, 4, 6], (e1, e2) => e1 / e2), [0.5, 0.5, 0.5]);
799-
expect([2, 4, 6].zip([1, 2, 3], (e1, e2) => e1 / e2), [2.0, 2.0, 2.0]);
796+
group('.zip()', () {
797+
test('with same types', () {
798+
expect([].zip([], (e1, e2) => null), []);
799+
expect(
800+
[1, 2, 3].zip([2, 4, 6], (e1, int e2) => e1 / e2),
801+
[0.5, 0.5, 0.5],
802+
);
803+
expect(
804+
[2, 4, 6].zip([1, 2, 3], (e1, int e2) => e1 / e2),
805+
[2.0, 2.0, 2.0],
806+
);
807+
808+
// with type definitions
809+
expect(
810+
[1, 2, 3].zip([2, 4, 6], (int e1, int e2) => e1 / e2),
811+
[0.5, 0.5, 0.5],
812+
);
813+
});
814+
815+
test('with same types and different length', () {
816+
expect([1, 2, 3].zip([2, 4], (e1, int e2) => e1 / e2), [0.5, 0.5]);
817+
expect([2, 4].zip([2, 2, 2], (e1, int e2) => e1 / e2), [1.0, 2.0]);
818+
});
819+
820+
test('with different types', () {
821+
final amounts = [2, 3, 4];
822+
final animals = ['dogs', 'birds', 'cats'];
823+
expect(
824+
amounts.zip(animals, (amount, String animal) => '$amount $animal'),
825+
['2 dogs', '3 birds', '4 cats'],
826+
);
827+
});
828+
829+
test('with different types and different lengths', () {
830+
final amounts = [2, 3];
831+
final animals = ['dogs', 'birds', 'cats'];
832+
expect(
833+
amounts.zip(animals, (amount, String animal) => '$amount $animal'),
834+
['2 dogs', '3 birds'],
835+
);
836+
});
800837
});
801838

802839
test('.toIterable()', () {

0 commit comments

Comments
 (0)