Skip to content

Instantly share code, notes, and snippets.

@fredgrott
Created March 10, 2024 17:20
Show Gist options
  • Save fredgrott/c0608092bed0583f8db099817f71cdee to your computer and use it in GitHub Desktop.
Save fredgrott/c0608092bed0583f8db099817f71cdee to your computer and use it in GitHub Desktop.
dart oop behavioral, iterator
// Copyright 2024 Fredrick Allan Grott. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
import 'package:iterator/rainbow_iterator.dart';
void main() {
var rainbowColors = RainbowIterator();
while (rainbowColors.moveNext()) {
print(rainbowColors.current);
}
/*
Red
Orange
Yellow
Green
Blue
Indigo
Violet
*/
}
// Copyright 2024 Fredrick Allan Grott. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
class RainbowIterator implements Iterator {
final _colors = ["Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet"];
var _index = 0;
@override
String get current => _colors[_index++];
@override
bool moveNext() => _index < _colors.length;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment