puma icon indicating copy to clipboard operation
puma copied to clipboard

ECMA 6 - Test Function Generators

Open emravera opened this issue 7 years ago • 0 comments

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:

  • http://es6-features.org/#GeneratorFunctionIteratorProtocol
  • http://es6-features.org/#GeneratorFunctionDirectUse
  • http://es6-features.org/#GeneratorMatching
  • http://es6-features.org/#GeneratorControlFlow
  • http://es6-features.org/#GeneratorMethods

emravera avatar Apr 16 '18 23:04 emravera