Skip to content

Instantly share code, notes, and snippets.

@gfldex
Created February 24, 2021 00:38
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 gfldex/79ac4c5505086b37bf42affac89084db to your computer and use it in GitHub Desktop.
Save gfldex/79ac4c5505086b37bf42affac89084db to your computer and use it in GitHub Desktop.
class Hold { has $.key; }
class Press { has $.key; }
class Err { has $.msg; }
sub else(&code) { &code }
sub match($needle, *@tests) {
for @tests.head(*-1) -> &f {
if &f.cando(\($needle)) {
f($needle);
last;
}
}
@tests.tail.();
}
my @possibilities = Hold.new(:key<a>), Press.new(:key<b>), Err.new(:msg<WELP!>), 'unsupported';
# my @possibilities = 'unsupported';
for ^10 {
match @possibilities.pick,
-> Hold (:$key) { put „holding $key“; },
-> Press (:$key) { put „pressing $key“; },
-> Err (:$msg) { warn „ERR: $msg“ },
else { warn ‚unsopported‘ };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment