Skip to content

Instantly share code, notes, and snippets.

@nanjizal
Created May 28, 2022 17:19
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 nanjizal/c3beb378a1cc89c2754cfceecbfbef24 to your computer and use it in GitHub Desktop.
Save nanjizal/c3beb378a1cc89c2754cfceecbfbef24 to your computer and use it in GitHub Desktop.
Backwards Iterator
@:access(IntIterator.min, IntIterator.max )
class IntIterStart {
public var start: Int;
public var max: Int;
public function new( min_: Int, max_: Int ){
start = min_;
max = max_;
}
}
@:transitive
@:access( IntIterator.min, IntIterator.max )
abstract Backwards( IntIterStart ) from IntIterStart {
public inline
function new( min: Int, max: Int ){
this = new IntIterStart( min, max );
}
@:from
static inline
public function fromIterator( ii: IntIterator ): Backwards {
return new Backwards( ii.min, ii.max );
}
@:to
function toIterStart():Backwards {
return new Backwards( this.start, this.max );
}
public inline function hasNext() {
return this.max > this.start;
}
public inline function next() {
return ( this.max-- -1 );
}
}
class Test {
static function main() {
for( i in (( 0...10 ): Backwards ) ){
trace( i );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment