Transcrypt icon indicating copy to clipboard operation
Transcrypt copied to clipboard

enumerate

Open mine-bach opened this issue 7 years ago • 2 comments

Hello,

I have slighly modified the "iterators_and_generators" autotest code. (I didn't understood all comments, but....)

Trying to use enumerate with the given Iterable does not seems to work :

    autoTester.check ('[4]')
    for i, n in enumerate(iterable):
        toto = str(i) +'-'+ str(n)
        autoTester.check (toto)`

mine-bach avatar Feb 24 '19 14:02 mine-bach

enumerate seems to work properly for strings and lists but not for generators.

JennaSys avatar Aug 05 '24 00:08 JennaSys

A fix is in place for v3.9.4 but needs more testing, and maybe a performance check.

Instead of using zip, enumerate has instead been changed to use a JS generator, which I had some issues getting exported from the built-ins module. On the plus side, it wasn't much more code than what was already there and enumerate is now lazy. Autotests are all currently passing.

function* __enumerate__ (iterable, start=0) {
    if (start.hasOwnProperty("__kwargtrans__")) {
        // start was likely passed in as kwarg
        start = start['start'];
    }
    let n = start
    for (const item of iterable) {
        yield [n, item]
        n += 1
    }
}
export var py_enumerate = __enumerate__;

JennaSys avatar Aug 05 '24 09:08 JennaSys