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 type safe IterableZip #341

Open
narumincho opened this issue May 15, 2024 · 0 comments
Open

Add type safe IterableZip #341

narumincho opened this issue May 15, 2024 · 0 comments

Comments

@narumincho
Copy link

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

void main() {
  final listA = [0, 1, 2];
  final listB = ['aaa', 'bbb', 'ccc'];

  final zipped = IterableZip([listA, listB]);
  for (final [valueAsInt, valueAsString] in zipped) {
    print(valueAsString.substring(valueAsInt)); // type error. (The method 'substring' isn't defined for the type 'Object'.)
  }

  final zipped2 = IterableZip2(listA, listB);
  for (final (valueAsInt, valueAsString) in zipped2) {
    print(valueAsString.substring(valueAsInt)); // no type error. (output aaa, bb, c)
  }
}

Implementation Example

import 'dart:collection';

class IterableZip2<T, U> extends IterableBase<(T, U)> {
  final Iterable<T> _iterablesT;
  final Iterable<U> _iterablesU;

  IterableZip2(Iterable<T> iterablesT, Iterable<U> iterablesU)
      : _iterablesT = iterablesT,
        _iterablesU = iterablesU;

  @override
  Iterator<(T, U)> get iterator {
    return _IteratorZip2<T, U>(_iterablesT.iterator, _iterablesU.iterator);
  }
}

class _IteratorZip2<T, U> implements Iterator<(T, U)> {
  final Iterator<T> _iteratorT;
  final Iterator<U> _iteratorU;
  (T, U)? _current;

  _IteratorZip2(Iterator<T> iteratorsT, Iterator<U> iteratorU)
      : _iteratorT = iteratorsT,
        _iteratorU = iteratorU;

  @override
  bool moveNext() {
    if (!_iteratorT.moveNext()) {
      _current = null;
      return false;
    }
    if (!_iteratorU.moveNext()) {
      _current = null;
      return false;
    }
    _current = (_iteratorT.current, _iteratorU.current);
    return true;
  }

  @override
  (T, U) get current => _current ?? (throw StateError('No element'));
}

Examples of zip functions in other languages

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant