Skip to content

Commit

Permalink
Implement .map in Pure Perl 6
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit cc76a676174b883e5b9025d36fdd34e9b7e13fe0
Merge: f1d4f77... ed4cd14...
Author: Cory Spencer <cspencer@sprocket-2.local>
Date:   Sat Mar 7 19:03:50 2009 -0800

    Merge branch 'master' of git://github.com/rakudo/rakudo into any-pm

    Conflicts:
    	src/setting/Any-list.pm

commit f1d4f77de8c5a46ecfed86bf9060d2eb5dc9072a
Author: Cory Spencer <cspencer@sprocket-2.local>
Date:   Sat Mar 7 18:09:59 2009 -0800

    Remove the PIR map method and replace with a Perl 6 version.

commit a8824fac6d139c91352ee9f775a5e37eca4d446c
Author: Cory Spencer <cspencer@sprocket-2.local>
Date:   Sat Mar 7 12:55:10 2009 -0800

    Removed PIR of grep and replaced with P6 version.

Signed-off-by: Moritz Lenz <moritz@faui2k3.org>
  • Loading branch information
Cory Spencer authored and moritz committed Mar 8, 2009
1 parent 4947aea commit 137dcfa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 55 deletions.
55 changes: 0 additions & 55 deletions src/builtins/any-list.pir
Expand Up @@ -168,61 +168,6 @@ Return a List with the keys of the invocant.
.return (result)
.end


=item map()

=cut

.namespace []
.sub 'map' :multi('Sub')
.param pmc expression
.param pmc values :slurpy
.tailcall values.'map'(expression)
.end

.namespace ['Any']
.sub 'map' :method :multi(_, 'Sub')
.param pmc expression
.local pmc res, elem, block, mapres, iter, args
.local int i, arity

arity = expression.'arity'()
if arity > 0 goto body
arity = 1
body:
res = new 'List'
iter = self.'iterator'()
map_loop:
unless iter goto done

# Creates arguments for closure
args = new 'ResizablePMCArray'

i = 0
args_loop:
if i == arity goto invoke
unless iter goto elem_undef
elem = shift iter
goto push_elem
elem_undef:
elem = new 'Failure'
push_elem:
push args, elem
inc i
goto args_loop

invoke:
(mapres :slurpy) = expression(args :flat)
unless mapres goto map_loop
mapres.'!flatten'()
$I0 = elements res
splice res, mapres, $I0, 0
goto map_loop

done:
.return(res)
.end

=item min

=cut
Expand Down
21 changes: 21 additions & 0 deletions src/setting/Any-list.pm
Expand Up @@ -4,10 +4,31 @@ class Any is also {
take $_ if $test($_) for $values.list;
}
}

our List of Capture multi method map(List @values: Code *&expr) {
gather {
my $i = 0;
while ($i <= @values.end) {
my @args;
@args.push(($i <= @values.end) ?? @values[$i++] !! undef)
for (1..&expr.arity || 1);

take &expr(|@args);
}
}
}

our List of Capture multi method map($value: Code *&expr) {
($value,).map: &expr
}
}

our List multi grep(Code $test, *@values) {
@values.grep($test)
}

our List of Capture multi map(Code $expr, *@values) {
@values.map($expr)
}

# vim: ft=perl6

0 comments on commit 137dcfa

Please sign in to comment.