forked from beeware/batavia
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request beeware#543 from Nurdok/feature/enumerate-iterator
Implement 'enumerate' as an Iterator
- Loading branch information
Showing
4 changed files
with
56 additions
and
17 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
var PyObject = require('../core').Object | ||
var create_pyclass = require('../core').create_pyclass | ||
|
||
/************************************************** | ||
* Enumerate | ||
**************************************************/ | ||
|
||
function Enumerate(iterable) { | ||
PyObject.call(this) | ||
this.iterator = iterable.__iter__([]) | ||
this.count = 0 | ||
} | ||
|
||
create_pyclass(Enumerate, 'enumerate') | ||
|
||
Enumerate.prototype.__next__ = function() { | ||
var item = this.iterator.__next__([]) | ||
var types = require('../types') | ||
var index = new types.Int(this.count) | ||
this.count += 1 | ||
|
||
return new types.Tuple([index, item]) | ||
} | ||
|
||
Enumerate.prototype.__iter__ = function() { | ||
return this | ||
} | ||
|
||
Enumerate.prototype.__str__ = function() { | ||
return '<enumerate object at 0x99999999>' | ||
} | ||
|
||
/************************************************** | ||
* Module exports | ||
**************************************************/ | ||
|
||
module.exports = Enumerate |
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