Skip to content

Commit

Permalink
Merge pull request beeware#543 from Nurdok/feature/enumerate-iterator
Browse files Browse the repository at this point in the history
Implement 'enumerate' as an Iterator
  • Loading branch information
freakboy3742 committed May 25, 2017
2 parents acb077e + 094ac3b commit 8117c26
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 17 deletions.
9 changes: 2 additions & 7 deletions batavia/builtins/enumerate.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var exceptions = require('../core').exceptions
var types = require('../types')

function enumerate(args, kwargs) {
if (arguments.length !== 2) {
Expand All @@ -7,13 +8,7 @@ function enumerate(args, kwargs) {
if (kwargs && Object.keys(kwargs).length > 0) {
throw new exceptions.TypeError.$pyclass("enumerate() doesn't accept keyword arguments")
}
var result = []
var values = args[0]
for (var i = 0; i < values.length; i++) {
result.push([i, values[i]])
}
// FIXME this should return a generator, not list
return result
return new types.Enumerate(args[0])
}
enumerate.__doc__ = 'enumerate(iterable[, start]) -> iterator for index, value of iterable\n\nReturn an enumerate object. iterable must be another object that supports\niteration. The enumerate object yields pairs containing a count (from\nstart, which defaults to zero) and a value yielded by the iterable argument.\nenumerate is useful for obtaining an indexed list:\n (0, seq[0]), (1, seq[1]), (2, seq[2]), ...'

Expand Down
2 changes: 2 additions & 0 deletions batavia/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ types['Slice'] = require('./types/Slice')

types['CallableIterator'] = require('./types/CallableIterator')

types['Enumerate'] = require('./types/Enumerate')

/*************************************************************************
* Type comparison defintions that match Python-like behavior.
*************************************************************************/
Expand Down
37 changes: 37 additions & 0 deletions batavia/types/Enumerate.js
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
25 changes: 15 additions & 10 deletions tests/builtins/test_enumerate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,21 @@


class EnumerateTests(TranspileTestCase):
pass
def test_enumerate_iter(self):
self.assertCodeExecution("""
iterator = enumerate('abc')
print(next(iterator))
print(next(iterator))
print(next(iterator))
try:
print(next(iterator))
except StopIteration:
pass
try:
print(next(iterator))
except StopIteration:
pass
""")


class BuiltinEnumerateFunctionTests(BuiltinFunctionTestCase, TranspileTestCase):
Expand All @@ -11,20 +25,11 @@ class BuiltinEnumerateFunctionTests(BuiltinFunctionTestCase, TranspileTestCase):
not_implemented = [
'test_noargs',
'test_bool',
'test_bytearray',
'test_bytes',
'test_class',
'test_complex',
'test_dict',
'test_float',
'test_frozenset',
'test_int',
'test_list',
'test_None',
'test_NotImplemented',
'test_range',
'test_set',
'test_slice',
'test_str',
'test_tuple',
]

0 comments on commit 8117c26

Please sign in to comment.