Skip to content

Instantly share code, notes, and snippets.

@colomon
Created January 26, 2010 03:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colomon/286514 to your computer and use it in GitHub Desktop.
Save colomon/286514 to your computer and use it in GitHub Desktop.
# .get example
class MapIterator does Iterator {
has $.base-iterator;
has $.code;
multi method get() {
my $value = $.base-iterator.get;
$value ~~ Good ?? $.code($value) !! $value;
}
}
# .next / .value example, .value invalid after .next returns false
class MapIterator does Iterator {
has $.base-iterator;
has $.code;
multi method next() {
$.base-iterator.next;
}
multi method value() {
$.code($.base-iterator);
}
}
# .next / .value example, .value "valid" after .next returns false
class MapIterator does Iterator {
has $.base-iterator;
has $.code;
has $.current;
multi method next() {
my $okay = $.base-iterator.next;
if ($okay) {
$.current = $.code($.base_iterator.value);
}
else {
$.current = $.base_iterator.value;
}
}
multi method value() {
$.current;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment