pumascript / puma

Meta-programming framework for JavaScript based on LayerD concepts
MIT License
30 stars 28 forks source link

ECMA 6 - Test Function Generators #131

Open emravera opened 6 years ago

emravera commented 6 years ago

LOW PRIORITY -

Generate a test suite for:

// Generator and iterator
let fibonacci = {
    *[Symbol.iterator]() {
        let pre = 0, cur = 1
        for (;;) {
            [ pre, cur ] = [ cur, pre + cur ]
            yield cur
        }
    }
}

for (let n of fibonacci) {
    if (n > 1000)
        break
    console.log(n)
}

//Generator function direct use
function* range (start, end, step) {
    while (start < end) {
        yield start
        start += step
    }
}

for (let i of range(0, 10, 2)) {
    console.log(i) // 0, 2, 4, 6, 8
}

// Generator Matching
let fibonacci = function* (numbers) {
    let pre = 0, cur = 1
    while (numbers-- > 0) {
        [ pre, cur ] = [ cur, pre + cur ]
        yield cur
    }
}

for (let n of fibonacci(1000))
    console.log(n)

let numbers = [ ...fibonacci(1000) ]

let [ n1, n2, n3, ...others ] = fibonacci(1000)

// Generator control flows

// Generator methods
class Clz {
    * bar () {
        …
    }
}
let Obj = {
    * foo () {
        …
    }
}

More info: