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#539 from Nurdok/feature/iter-sentinel
Implement the sentinel variant of the 'iter' builtin
- Loading branch information
Showing
4 changed files
with
85 additions
and
1 deletion.
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,43 @@ | ||
var PyObject = require('../core').Object | ||
var exceptions = require('../core').exceptions | ||
var create_pyclass = require('../core').create_pyclass | ||
|
||
/************************************************** | ||
* Callable Iterator | ||
**************************************************/ | ||
|
||
function CallableIterator(callable, sentinel) { | ||
PyObject.call(this) | ||
this.callable = callable | ||
this.sentinel = sentinel | ||
this.exhausted = false | ||
} | ||
|
||
create_pyclass(CallableIterator, 'callable_iterator') | ||
|
||
CallableIterator.prototype.__next__ = function() { | ||
if (this.exhausted) { | ||
throw new exceptions.StopIteration.$pyclass() | ||
} | ||
|
||
var item = this.callable.__call__([]) | ||
if (item.__eq__(this.sentinel)) { | ||
this.exhausted = true | ||
throw new exceptions.StopIteration.$pyclass() | ||
} | ||
return item | ||
} | ||
|
||
CallableIterator.prototype.__iter__ = function() { | ||
return this | ||
} | ||
|
||
CallableIterator.prototype.__str__ = function() { | ||
return '<callable_iterator object at 0x99999999>' | ||
} | ||
|
||
/************************************************** | ||
* Module exports | ||
**************************************************/ | ||
|
||
module.exports = CallableIterator |
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