Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make for $@arr { } iterate only once
my @arr = 1, 2;
for @arr {} will iterate twice
for $@arr {} will iterate once
@arr.map: {} will iterate twice
$@arr.map: {} will iterate twice
  • Loading branch information
niner committed Aug 29, 2015
1 parent 2a58180 commit 7aee0ac
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
32 changes: 24 additions & 8 deletions src/Perl6/Actions.nqp
Expand Up @@ -1231,18 +1231,34 @@ Compilation unit '$file' contained the following violations:

method statement_control:sym<for>($/) {
my $xblock := $<xblock>.ast;
my $past := QAST::Op.new(
my $for-list-name := QAST::Node.unique('for-list');
my $iscont := QAST::Op.new(:op('iscont'), QAST::Var.new( :name($for-list-name), :scope('local') ));
$iscont.named('item');
my $call := QAST::Op.new(
:op<callmethod>, :name<map>, :node($/),
$xblock[0],
block_closure($xblock[1])
QAST::Var.new( :name($for-list-name), :scope('local') ),
block_closure($xblock[1]),
$iscont,
);
if $*LABEL {
$past.push(QAST::WVal.new( :value($*W.find_symbol([$*LABEL])), :named('label') ));
$call.push(QAST::WVal.new( :value($*W.find_symbol([$*LABEL])), :named('label') ));
}
$past := QAST::Want.new(
QAST::Op.new( :op<callmethod>, :name<eager>, $past ),
'v', QAST::Op.new( :op<callmethod>, :name<sink>, $past ));
my $sinkee := $past[0];
my $bind := QAST::Op.new(
:op('bind'),
QAST::Var.new( :name($for-list-name), :scope('local'), :decl('var') ),
$xblock[0],
);
my $past := QAST::Want.new(
QAST::Stmts.new(
$bind,
QAST::Op.new( :op<callmethod>, :name<eager>, $call )
),
'v', QAST::Stmts.new(
$bind,
QAST::Op.new( :op<callmethod>, :name<sink>, $call )
),
);
my $sinkee := $past[0][1];
$past.annotate('statement_level', -> { $sinkee.name('sink') });
make $past;
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/Any-iterable-methods.pm
Expand Up @@ -19,8 +19,8 @@ augment class Any {

proto method map(|) { * }

multi method map(\SELF: &block, :$label) {
sequential-map(as-iterable(SELF).iterator, &block, :$label);
multi method map(\SELF: &block, :$label, :$item) {
sequential-map(as-iterable($item ?? (SELF,) !! SELF).iterator, &block, :$label);
}

multi method map(HyperIterable:D: &block, :$label) {
Expand Down

0 comments on commit 7aee0ac

Please sign in to comment.