TranscryptOrg / Transcrypt

Python 3.9 to JavaScript compiler - Lean, fast, open!
https://www.transcrypt.org
Apache License 2.0
2.86k stars 215 forks source link

enumerate #618

Open mine-bach opened 5 years ago

mine-bach commented 5 years ago

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)`
JennaSys commented 3 months ago

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

JennaSys commented 3 months ago

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__;