Skip to content

Instantly share code, notes, and snippets.

@xordspar0
Last active June 6, 2019 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xordspar0/199d4a60ecfe5fd10ee45a5b322d6c4e to your computer and use it in GitHub Desktop.
Save xordspar0/199d4a60ecfe5fd10ee45a5b322d6c4e to your computer and use it in GitHub Desktop.
A concurrent FizzBuzz implemented in Wren (ported from Go: https://play.golang.org/p/ssVH6Rg3-Z)
/*
* fizzbuzz.wren by Jordan Christiansen
*
* This is a concurrent implementation of FizzBuzz that shows off how to send
* and receive data from Fibers. It was ported from a Go implementation by Russ Cox:
* https://play.golang.org/p/ssVH6Rg3-Z
*/
class Generator {
static create() {
return Fiber.new {
while (true) {
Fiber.yield("")
}
}
}
}
class Filter {
static create(inputStream, n, label) {
return Fiber.new {
while (true) {
for (i in 1...n) {
Fiber.yield(inputStream.call())
}
Fiber.yield(inputStream.call() + label)
}
}
}
}
var generate = Generator.create()
var filter = Filter.create(generate, 3, "Fizz")
filter = Filter.create(filter, 5, "Buzz")
for (i in 1..100) {
var word = filter.call()
if (word != "") {
System.print(word)
} else {
System.print(i)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment