Last active
June 6, 2019 15:06
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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