package python; import python.lib.Types; class HaxeIterable { var x :NativeIterable; public inline function new (x:NativeIterable) { this.x = x; } public inline function iterator ():HaxeIterator return new HaxeIterator(x.__iter__()); } class HaxeIterator { var it :NativeIterator; var x:Null = null; var has = false; var checked = false; public function new (it:NativeIterator) { this.it = it; } public inline function next ():T { checked = false; return x; } public function hasNext ():Bool { if (checked) { return has; } else { try { x = it.__next__(); has = true; } catch (s:StopIteration) { has = false; x = null; } checked = true; return has; } } } class Lib { /* public static inline function assert(value:Dynamic) { untyped __assert__(value); } public static function random(max:Int) { var r = new dart.math.Random(); //TODO(av) benchmark / test caching Random instance as static return r.nextInt(max); } */ public static function print(v:Dynamic) { python.lib.Sys.stdout.write(Std.string(v)); python.lib.Sys.stdout.flush(); } public static function println(v:Array) { for (e in v) { untyped __python__("print")(Std.string(e)); } } public static function toPythonIterable (it:Iterable):python.lib.Types.NativeIterable { return { __iter__ : function () { var it1 = it.iterator(); var self:PyIterator = null; self = new PyIterator({ __next__ : function ():T { if (it1.hasNext()) { return it1.next(); } else { throw new python.lib.Types.StopIteration(); } }, __iter__ : function () return self }); return self; } } } public static inline function toHaxeIterable (it:NativeIterable):HaxeIterable { return new HaxeIterable(it); } public static inline function toHaxeIterator (it:NativeIterator):HaxeIterator { return new HaxeIterator(it); } }