Can I pass parameter to ES6 generator function

Here's the ES6 generator:

function *Gen() {
    var input1 = yield 'output1'
}

var gen = Gen()

gen.next('input1').value // return 'output1'

gen is called the 1st time, returns output1, but the variable is input1not equal to 'input1'that which passed, the value inputis actually "input2", the second time next('input2')is called

My question is how to access input1which for the first time is called the following, for example:

function *Gen() {
    var input 1 = param1
    var input2 = yield 'output1'
}
+7
source share
5 answers

, yield, ( ). yield, gen.next() - . yield , gen.next('input1'), yield 'output1' , next - 'input1'. yield return, . :

function *Gen() {
    var input1 = yield 'output1'
    return input1
}

var gen = Gen()

gen.next();
gen.next('input1').value // return 'input1'
+18

, 1 , .

+6

, , :

function myGenerator(startFrom) {
    return (function *() {
        var i = startFrom;
        while (true) yield i++;
    })();
}

var gen = myGenerator(5);
console.log(gen.next().value) // 5
console.log(gen.next().value) // 6
console.log(gen.next().value) // 7

:

function getGenerator(baseStartFrom, expStartFrom) {
    return (function *() {
        var a = baseStartFrom;
        while (true) {
          yield (function *() {
              var i = expStartFrom;
              while (true) yield Math.pow(a, i++);
          })();
          a++;
        }
    })();
}

var gen = getGenerator(2, 3);
var gen2 = gen.next().value; // generator yields powers of 2
  console.log(gen2.next().value); // 8
  console.log(gen2.next().value); // 16
  console.log(gen2.next().value); // 32
var gen3 = gen.next().value; // generator yields powers of 3
  console.log(gen3.next().value); // 27
  console.log(gen3.next().value); // 81
  console.log(gen3.next().value); // 243

, , , , , .

0

-, :

function *createGenerator(input) {
    yield input
}

var generator = createGenerator('input')

console.log(
    generator
    .next()
    .value
)
// input

, , , - .next. .next?

function *createGenerator() {
    const input = yield
    yield input
}

var generator = createGenerator()

console.log(
    generator
    .next('input1')
    .value
)
// undefined

console.log(
    generator
    .next('input2')
    .value
)
// input2

, , . , yield . - , , , .

:

function *createGenerator() {
    const input1 = yield
    const input2 = yield input1
    yield input2
}

var generator = createGenerator()

console.log(
    generator
    .next('input0')
    .value
)
// undefined

console.log(
    generator
    .next('input1')
    .value
)
// input1

console.log(
    generator
    .next('input2')
    .value
)
// input2

console.log(
    generator
    .next('input3')
    .value
)
// undefined

.next , yield. yield input, , " " JavaScript AST.

0

, .

, , . next .

offer_comparer.send() offer1 offer2 offers .

, .

import time


def compare_offers():
    count_max = 10
    while True:
        count1 = 0
        count2 = 0
        while count1 < count_max and count2 < count_max:
            offers = yield
            offer1, offer2 = offers[0], offers[1]
            if offer1 > offer2:
                count1 += 1
                print("Store1 has the better deal")
                if count1 == count_max:
                    print("go ahead and buy from store1")
            elif offer2 > offer1:
                count2 += 1
                print("Store2 has the better deal")
                if count2 == count_max:
                    print("go buy this from store2")


offer_comparer = compare_offers()
next(offer_comparer)


def testing():
    stores = {'lastUpdateId': 202588846,
              'store1': [['24.43000000', '0.00606000'],
                         ['24.42000000', '14.76000000'],
                         ['24.39000000', '2.65760000'],
                         ['24.38000000', '29.59867000'],
                         ['24.35000000', '7.71171000']],
              'store2': [['24.47000000', '0.22601000'],
                         ['24.52000000', '0.72000000'],
                         ['24.53000000', '3.28839000'],
                         ['24.54000000', '5.63226000'],
                         ['24.55000000', '20.64052000']]}

    firststore = [float(i[1]) for i in stores['store1']]
    secondstore = [float(e[1]) for e in stores['store2']]

    offer1 = sum(firststore[:3])
    offer2 = sum(secondstore[:3])

    offer_comparer.send((offer1, offer2))


i = 0
while True:
    i += 1
    testing()
    time.sleep(2)
0

All Articles