Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
implement some more use cases of infix:<...>
Much of that consists of ugly hacks because
 * we'd need multi slices for the signature, and don't have 'em
 * I've hit at least three Rakudo bugs which I've worked around
 * infix:<...> has rather sophisticated cases, which I tried to cover
   at least partially without too much code duplication
  • Loading branch information
moritz committed Sep 24, 2009
1 parent 8e34da7 commit f2acdbb
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions src/setting/Operators.pm
@@ -1,14 +1,35 @@
# operators defined in the setting

multi sub infix:<...> (@lhs, Code $generator) {
multi sub infix:<...> (@lhs, @rhs) {
if @rhs == 2 && @rhs[0] ~~ Code {
&infix:<...>(@lhs, @rhs[0], :limit(@rhs[1]));
} else {
die "don't know how to handle a right-hand side of"
~ @rhs.perl
~ "in series operator";
}
}

multi sub infix:<...>($lhs, Code $generator) {
&infix:<...>([$lhs], $generator);
}

multi sub infix:<...> (@lhs, Code $generator, :$limit) {
my $c = $generator.count;
if $c > @lhs {
fail 'the closure wants more parameters than given on the LHS';
}
my @result = @lhs;
my @r;
my $argument-indexes;
if any( $generator.signature.params>>.<slurpy> ) {
warn $generator.perl;
warn $generator.signature.perl;
# WhateverCode objects don't have a signature yet (RT #69362),
# and we can't simply use a try { ... } block because its result
# throws a "Null PMC access in get_bool()" when used in boolean context.
# we have to use an ugly special case here.
# and we can't even used !~~ for that (RT #69364)
if !$generator.^isa(WhateverCode) and any( $generator.signature.params>>.<slurpy> ) {
$argument-indexes = 0..*-1;
} else {
$argument-indexes = *-$c .. *-1;
Expand All @@ -18,10 +39,24 @@ multi sub infix:<...> (@lhs, Code $generator) {
# this is a bit ugly.. since @a[1..1] returns a single item and not
# an array, |@result[$one-item-range] throws the error
# "argument doesn't array"
my $comp;
if defined($limit) {
$comp = @lhs[*-1] cmp $limit;
}

while @r = $generator(|@(@result[$argument-indexes])) {
if (defined($limit)) {
if (@r[*-1] cmp $limit) == 0 {
@result.push: @r;
last;
} elsif (@r[*-1] cmp $limit) != $comp {
last;
}
}

@result.push: @r;
}
return @result;
@result;
}

multi sub infix:<eqv> (Num $a, Num $b) { $a === $b }
Expand Down

0 comments on commit f2acdbb

Please sign in to comment.