Skip to content

Instantly share code, notes, and snippets.

# bintree - binary tree demo program
# adapted from "Perl Cookbook", Recipe 11.15
# converted to modern Perl 6 by SF & Carl Masak
use v6;
role Found { has Int $.found is rw }
my %root;
for (1..1000).pick(20) {
insert(%root, $_);
method parse(Str $message) {
my ($command, @params) = $message.split(/\s+/);
given $command {
when 'help' { self.cmd-help(@params); }
when 'karma' { self.cmd-karma(@params); }
when 'purge' { self.cmd-purge(@params); }
when 'link' { self.cmd-link(@params); }
when 'unlink' { self.cmd-unlink(@params); }
when * { "Sorry, I don't understand that command"; }
}
method parse(Str $message is rw) {
$message .= split(' ');
my $command = $message.shift;
my $params = $message;
if $command ~~ 'help' {
self.cmd-help($params);
} elsif $command ~~ 'karma' {
self.cmd-karma($params);
} elsif $command ~~ 'purge' {
self.cmd-purge($params);
multi method KnotVector.N0_index(Int $p, $u, KnotBasisDirection $direction = Left)
{
given $direction
{
when Left { UpperBound(@.knots, $u) - $p - 1; }
when Right { LowerBound(@.knots, $u) - $p - 1; }
}
}
multi method Nubs.Direction($t)
multi method KnotVector.N0_index(Int $p, $u, KnotBasisDirection $direction = Left)
{
given $direction
{
when Left { UpperBound(@.knots, $u) - $p - 1; }
when Right { LowerBound(@.knots, $u) - $p - 1; }
when Reasonable { die "Must specify Left or Right for KnotBasisDirection"; }
}
}
use v6;
my %dictionary;
slurp("big.txt").comb(/<alpha>+/).map({%dictionary{$_.lc}++});
sub edits($word) {
my @s = (^$word.chars).map({$word.substr(0, $_), $word.substr($_)});
my @deletes = @s.map(-> $a, $b { $a ~ $b.substr(1); });
my @transposes = @s.map(-> $a, $b { $a ~ $b.substr(0, 2).flip ~ $b.substr(2) if $b.chars > 1 });
my @replaces = @s.map(-> $a, $b {$a ~ ':' ~ $b.substr(1)});
my @polys = Polynomial.new(0) xx @control_points;
my $n0 = $kv.N0_index($u);
@polys[($n0 - ($degree + 1)) .. ($n0 - 1)] = $kv.N_local($n0, $degree, Polynomial.new(0, 1));
my $poly = [+] (@polys >>*<< @control_points);
class ShouldWeUseExactCalculations is SurfaceVisitor
{
has $.use_exact_calculations = Bool::False;
multi method VisitPlane(SurfacePlane $plane)
{
self.use_exact_calculations = Bool::True;
}
}
# .... code where it is used ....
my $use_exact_calculations = ($surface ~~ SurfacePlane);
multi sub infix:<+>(Polynomial $a, Polynomial $b)
{
my $length = ($a.coefficients.elems, $b.coefficients.elems).max;
my @ac = $a.coefficients;
@ac.push: (0 xx ($length - @ac.elems));
my @bc = $b.coefficients;
@bc.push: (0 xx ($length - @bc.elems));
return Polynomial.new(@ac »+« @bc);
}