generators

generators


Node.js

function *generator() {
  yield 'hello'
  yield 'world'
}

let gen = generator()

while (true) {
  let { value, done } = gen.next()
  console.log(value, done)

  if (done) {
    break
  }
}

// alternatively
for (let value of generator()) {
  console.log(value)
}

Output

hello false
world false
undefined true
hello
world

Go

package main

import "fmt"

func Generator() chan string {
    c := make(chan string)

    go func() {
        c <- "hello"
        c <- "world"

        close(c)
    }()

    return c
}

func GeneratorFunc() func() (string, bool) {
    s := []string{"hello", "world"}
    i := -1

    return func() (string, bool) {
        i++
        if i >= len(s) {
            return "", false
        }

        return s[i], true
    }
}

func main() {
    gen := Generator()
    for {
        value, more := <-gen
        fmt.Println(value, more)

        if !more {
            break
        }
    }

    // alternatively
    for value := range Generator() {
        fmt.Println(value)
    }

    // alternatively
    genfn := GeneratorFunc()
    for {
        value, more := genfn()
        fmt.Println(value, more)

        if !more {
            break
        }
    }
}

Output

hello true
world true
 false
hello
world
hello true
world true
 false