Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tag: 2011.10
Fetching contributors…

Cannot retrieve contributors at this time

259 lines (215 sloc) 7.87 kb
my class MapIter { ... }
my class Whatever { ... }
my class Range { ... }
my class Any {
########
# List-like methods for Any.
########
method eager() { nqp::p6list(nqp::list(self), List, Bool::True).eager }
method elems() { self.list.elems }
method end() { self.list.end }
method classify(&t) { self.list.classify(&t) }
method infinite() { Mu }
method flat() { nqp::p6list(nqp::list(self), List, Bool::True) }
method hash() { my %h = self }
method list() { nqp::p6list(nqp::list(self), List, Mu) }
method pick($n = 1) { self.list.pick($n) }
method roll($n = 1) { self.list.roll($n) }
method reverse() { self.list.reverse }
method sort($by = &infix:<cmp>) { self.list.sort($by) }
method values() { self.list }
method keys() { self.list.keys }
method kv() { self.list.kv }
method pairs() { self.list.pairs }
method Array() { Array.new(self.flat) }
method grep(Mu $test) is rw {
self.map({ $_ if $_ ~~ $test });
}
method first(Mu $test) is rw {
for self.list {
return $_ if $test.ACCEPTS($_);
}
fail 'No values matched';
}
method join($separator = '') {
my $list = (self,).flat.eager;
my Mu $rsa := pir::new__Ps('ResizableStringArray');
nqp::push_s($rsa, nqp::unbox_s($list.shift.Stringy))
while $list.gimme(0);
nqp::push_s($rsa, '...') if $list.infinite;
nqp::p6box_s(nqp::join(nqp::unbox_s($separator.Stringy), $rsa))
}
method map($block) is rw {
MapIter.new(:list((self,).flat), :block($block)).list
}
method min($by = &infix:<cmp>) {
my $cmp = $by.arity == 2 ?? $by !! { $by($^a) cmp $by($^b) }
my $min = +$Inf;
for self {
$min = $_ if .defined && $cmp($_, $min) < 0;
}
$min;
}
method max($by = &infix:<cmp>) {
my $cmp = $by.arity == 2 ?? $by !! { $by($^a) cmp $by($^b) }
my $max = -$Inf;
for self {
$max = $_ if .defined && $cmp($_, $max) > 0;
}
$max;
}
method minmax($by = &infix:<cmp>) {
my $cmp = $by.arity == 2 ?? $by !! { $by($^a) cmp $by($^b) };
my $min = +$Inf;
my $max = -$Inf;
my $excludes_min = Bool::False;
my $excludes_max = Bool::False;
for @.list {
.defined or next;
if .isa(Range) {
if $cmp($_.min, $min) < 0 {
$min = $_;
$excludes_min = $_.excludes_min;
}
if $cmp($_.max, $max) > 0 {
$max = $_;
$excludes_max = $_.excludes_max;
}
} else {
if $cmp($_, $min) < 0 {
$min = $_;
$excludes_min = Bool::False;
}
if $cmp($_, $max) > 0 {
$max = $_;
$excludes_max = Bool::False;
}
}
}
Range.new($min,
$max,
:excludes_min($excludes_min),
:excludes_max($excludes_max));
}
proto method postcircumfix:<[ ]>(|$) { * }
multi method postcircumfix:<[ ]>() { self.list }
multi method postcircumfix:<[ ]>($pos) is rw {
fail "Cannot use negative index $pos on {self.WHAT.perl}" if $pos < 0;
self.at_pos($pos)
}
multi method postcircumfix:<[ ]>(Positional $pos) is rw {
my $list = $pos.flat;
$list.gimme(*);
$list.map($list.infinite
?? { last if $_ >= self.gimme($_ + 1); self[$_] }
!! { self[$_] }).eager.Parcel;
}
multi method postcircumfix:<[ ]>(Callable $block) is rw {
self[$block(|(self.elems xx $block.count))]
}
multi method postcircumfix:<[ ]>(Whatever) is rw {
self[^self.elems]
}
method at_pos($pos) is rw {
if self.defined {
fail ".[$pos] out of range for type {self.perl}" if $pos != 0;
return self;
}
}
method all() { all(self.list) }
method any() { any(self.list) }
method one() { one(self.list) }
method none() { none(self.list) }
########
# Hash-like methods for Any.
########
proto method postcircumfix:<{ }>(|$) { * }
multi method postcircumfix:<{ }>() { self.values }
multi method postcircumfix:<{ }>($key) is rw {
self.at_key($key)
}
multi method postcircumfix:<{ }>(Positional $key) is rw {
$key.map({ self{$_} }).eager.Parcel
}
multi method postcircumfix:<{ }>(Whatever) is rw {
self{self.keys}
}
method reduce(&with) { self.list.reduce(&with) }
}
proto infix:<===>($?, $?) { * }
multi infix:<===>($a?) { Bool::True }
multi infix:<===>($a, $b) { $a.defined eq $b.defined && $a.WHICH === $b.WHICH }
proto infix:<cmp>($, $) { * }
multi infix:<cmp>(\$a, \$b) {
return -1 if $a == -$Inf || $b == $Inf;
return 1 if $a == $Inf || $b == -$Inf;
$a.Stringy cmp $b.Stringy
}
proto infix:<before>(|$) { * }
multi infix:<before>($x?) { Bool::True }
multi infix:<before>(\$a, \$b) { ($a cmp $b) < 0 }
proto infix:<after>(|$) { * }
multi infix:<after>($x?) { Bool::True }
multi infix:<after>(\$a, \$b) { ($a cmp $b) > 0 }
# XXX: should really be '$a is rw' (no \) in the next four operators
proto prefix:<++>(|$) { * }
multi prefix:<++>(Mu:D \$a is rw) { $a = $a.succ }
multi prefix:<++>(Mu:U \$a is rw) { $a = 1 }
proto prefix:<-->(|$) { * }
multi prefix:<-->(Mu:D \$a is rw) { $a = $a.pred }
multi prefix:<-->(Mu:U \$a is rw) { $a = -1 }
proto postfix:<++>(|$) { * }
multi postfix:<++>(Mu:D \$a is rw) { my $b = $a; $a = $a.succ; $b }
multi postfix:<++>(Mu:U \$a is rw) { $a = 1; 0 }
proto postfix:<-->(|$) { * }
multi postfix:<-->(Mu:D \$a is rw) { my $b = $a; $a = $a.pred; $b }
multi postfix:<-->(Mu:U \$a is rw) { $a = -1; 0 }
proto infix:<min>(|$) { * }
multi infix:<min>(*@args) { @args.min }
# XXX the multi version suffers from a multi dispatch bug
# where the mandatory named is ignored in the presence of a slurpy
#proto sub min(|$) { * }
#multi sub min(*@args) { @args.min() }
#multi sub min(*@args, :&by!) { @args.min(&by) }
sub min(*@args, :&by = &infix:<cmp>) { @args.min(&by) }
proto infix:<max>(|$) { * }
multi infix:<max>(*@args) { @args.max }
#proto sub max(|$) { * }
#multi sub max(*@args) { @args.max() }
#multi sub max(*@args, :&by!) { @args.max(&by) }
sub max(*@args, :&by = &infix:<cmp>) { @args.max(&by) }
proto infix:<minmax>(|$) { * }
multi infix:<minmax>(*@args) { @args.minmax }
#proto sub minmax(|$) { * }
#multi sub minmax(*@args) { @args.minmax() }
#multi sub minmax(*@args, :&by!) { @args.minmax(&by) }
sub minmax(*@args, :&by = &infix:<cmp>) { @args.minmax(&by) }
proto map(|$) {*}
multi map(&code, *@values) { @values.map(&code) }
proto grep(|$) {*}
multi grep(Mu $test, *@values) { @values.grep($test) }
proto first(|$) {*}
multi first(Mu $test, *@values) { @values.first($test) }
proto join(|$) { * }
multi join($sep = '', *@values) { @values.join($sep) }
proto pick(|$) { * }
multi pick($n, *@values) { @values.pick($n) }
proto roll(|$) { * }
multi roll($n, *@values) { @values.roll($n) }
proto keys(|$) { * }
multi keys($x) { $x.keys }
proto values(|$) { * }
multi values($x) { $x.values }
proto pairs(|$) { * }
multi pairs($x) { $x.pairs }
proto kv(|$) { * }
multi kv($x) { $x.kv }
proto elems(|$) { * }
multi elems($a) { $a.elems }
proto end(|$) { * }
multi end($a) { $a.end }
proto classify(|$) { * }
multi classify(&test, *@items) { @items.classify(&test) }
proto sub sort(|$) {*}
multi sub sort(&by, *@values) { @values.sort(&by) }
multi sub sort(*@values) { @values.sort }
Jump to Line
Something went wrong with that request. Please try again.