Created
February 25, 2021 15:46
-
-
Save guid-empty/2bbb3bd92891dace3499c8fad21e2cfd to your computer and use it in GitHub Desktop.
Dart.Language.Extentions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
for (final current in StringIterable('Hello World!')) { | |
print(current); | |
} | |
// for (final current in 'Hello World!'.toIterable()) { | |
// print(current); | |
// } | |
} | |
extension MyStringExtensions on String { | |
StringIterable toIterable() { | |
return StringIterable(this); | |
} | |
} | |
class StringIterable extends Iterable<String> { | |
StringIterable(this.source); | |
final String source; | |
@override | |
Iterator<String> get iterator => StringIterator(source); | |
} | |
class StringIterator implements Iterator<String> { | |
StringIterator(this.source); | |
final String source; | |
String _current; | |
int _startIndex = 0; | |
@override | |
String get current => _current; | |
@override | |
bool moveNext() { | |
if (_startIndex == source.length) { | |
_current = null; | |
return false; | |
} | |
_current = source.substring(_startIndex, _startIndex + 1); | |
_startIndex++; | |
return true; | |
} | |
} | |
// abstract class Iterable<T> { | |
// @override | |
// Iterator<T> iterator; | |
// } | |
// abstract class Iterator<T> { | |
// T get current; | |
// bool moveNext(); | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment