Skip to content

Instantly share code, notes, and snippets.

@marcoonroad
Created December 10, 2014 23:19
Show Gist options
  • Save marcoonroad/a9b36741481a40c538f7 to your computer and use it in GitHub Desktop.
Save marcoonroad/a9b36741481a40c538f7 to your computer and use it in GitHub Desktop.
Call/cc-like stuff in Perl 6...
#!/usr/bin/perl6
use v6;
# call with current continuation
sub infix:<⇐> (&cont, &expr) {
my @rest = expr &cont;
return @rest[ 0 ] if @rest[ 1 ];
return cont @rest[ 0 ];
}
sub infix:<⇒> ($x, $y) { $x == $y } # x evaluates to y?
sub prefix:<⇑> ($x) { $x, True } # abort continuation
sub prefix:<⇓> ($x) { $x, False } # pass expression
sub prefix:<λ> (&block) { &block } # yep, a no-op / id function
sub infix:<×> ($x, $y) { $x * $y } # alias to '*'
say (&(5 + *) ⇐ λ -> &k { ⇓ (4 × 3) }) ⇒ 17; # True
say (&(2 × *) ⇐ λ -> &k { ⇑ k (3) }) ⇒ 6; # True
# end of script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment