Created
December 10, 2014 23:19
-
-
Save marcoonroad/a9b36741481a40c538f7 to your computer and use it in GitHub Desktop.
Call/cc-like stuff in Perl 6...
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
#!/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