Skip to content

Instantly share code, notes, and snippets.

@zoffixznet

zoffixznet/p6.p6 Secret

Created February 18, 2017 16:41
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 zoffixznet/82ef0cfb76ef1cbb6717555fab557427 to your computer and use it in GitHub Desktop.
Save zoffixznet/82ef0cfb76ef1cbb6717555fab557427 to your computer and use it in GitHub Desktop.
sub infix:<orelse>(+a) {
my $ai := a.iterator;
my Mu $current := $ai.pull-one;
return Nil if $current =:= IterationEnd;
nqp::until(
(($_ := $ai.pull-one) =:= IterationEnd),
nqp::stmts(
nqp::if(
$current.defined,
nqp::stmts(
nqp::if( $current.WHAT === Code, $current := $current() ),
return $current,
),
),
($current := $_ ~~ Callable
?? (.count ?? $_($current) !! $_())
!! $_
),
),
:nohandler, # do not handle control stuff in thunks
);
$current.WHAT === Code ?? $current() !! $current
}
# cpan@perlbuild2~/CPANPRC/rakudo (nom)$ ./perl6 -e 'note (Int andthen "foo" orelse Int);'
# -> ;; $_ is raw { #`(Block|70927672) ... }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment