Something went wrong. Try again.
An experimental alternative to Rust sans-io that enables the direct style
Something went wrong. Try again.
1.2 kB · 44 lines
Java
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445import java.util.ArrayDeque;import java.util.List;import java.util.Queue;import java.util.concurrent.atomic.AtomicInteger;
/// One participant's side of the world: scripted input, a lookup table, a/// counter, and tagged stdout. Same responsibilities as the Rust `Host`s in/// `compare/`, minus `Sleep`, which belongs to the scheduler.final class Host { final String tag; private final Queue<String> script; private final AtomicInteger count = new AtomicInteger();
Host(String tag, List<String> script) { this.tag = tag; this.script = new ArrayDeque<>(script); }
String readLine() { var line = script.poll(); if (line == null) { line = "quit"; } write("> " + line); return line; }
static String greetingFor(String name) { return switch (name) { case "Monde", "Amélie" -> "Bonjour"; case "Welt", "Hans" -> "Hallo"; case "Mundo", "Inés" -> "Hola"; default -> "Hello"; }; }
int count() { return count.incrementAndGet(); }
void write(String text) { System.out.printf("[%s @ %s] %s%n", tag, Thread.currentThread().getName(), text); }}