Skip to content

Instantly share code, notes, and snippets.

View moritz's full-sized avatar

Moritz Lenz moritz

View GitHub Profile
@moritz
moritz / closure.pl6
Created March 1, 2014 16:28
A simple closures example, with file walking
#!./perl6-m
use v6;
sub MAIN($path = '.') {
my $counter = 0;
my $callback = sub ($file) {
$counter++;
say $counter, "\t", $file.Str;
};
@moritz
moritz / find.pl6
Last active August 29, 2015 13:56
recursive file traversal, in parallel!
#!./perl6-j
use v6;
sub MAIN($path = '.') {
my $counter = 0;
my $callback = sub ($file) {
$counter++;
# say $counter, "\t", $file.Str;
};
@moritz
moritz / genclass.pl
Created May 8, 2014 18:55
Generate a class on the fly (or at least trying to...)
use v6;
my ($classname, $message) = ('C', 'hi');
my $class := 1.HOW.new_type(:name($classname));
$class.^add_method('message', anon method message() { $message });
$class.^compose();
say $class.new.message();
@moritz
moritz / override.pl
Created May 15, 2014 18:53
Attempt to mimic Java's @OverRide
use v6;
multi trait_mod:<is>(Routine:D $m, Mu $interface, :$declared-in!) {
my $name = $m.name;
unless $interface.^can($name) {
die "Method $name clames to be declared in {$interface.^name}, but there's no such method in {$interface.^name}!\n";
}
}
role R {
@moritz
moritz / meta.pl
Last active August 29, 2015 14:01
Attempt to mimic Java's @OverRide
use v6;
multi trait_mod:<is>(Routine:D $m, Mu:$declared-in!) {
my $name = $m.name;
unless $declared-in.^can($name) {
die "Method $name claims to be declared in {$declared-in.^name}, but there's no such method in {$declared-in.^name}!\n";
}
}
role R {
@moritz
moritz / call-positional-by-name.pl
Created May 18, 2014 13:57
Fill positional arguments by name (Perl 6)
use v6;
sub t ($x, $y, :$label = 'sum') {
say "$label: ", $x + $y;
}
sub namecall(&c, *@pos, *%named is rw) {
my %name-to-idx;
for &c.signature.params.kv -> $idx, $p {
next if $p.named;
@moritz
moritz / weird.pl
Created August 4, 2014 07:41
Bisect string weirdness
use v6;
my $str = slurp 'gistfile1.txt';
sub test(Str $str) {
return not try { $str.perl; True };
}
my $left = 0;
my $right = $str.chars;
@moritz
moritz / trans-fast.patch
Created October 11, 2014 10:58
Untested patch to speed up certain transliterations by ~25%
diff --git a/src/core/Str.pm b/src/core/Str.pm
index c0c886a..3590fbe 100644
--- a/src/core/Str.pm
+++ b/src/core/Str.pm
@@ -1081,10 +1081,9 @@ my class Str does Stringy { # declared in BOOTSTRAP
}
multi method triage_substitution($_ where { .key ~~ Cool }) {
- return unless defined index($!source, .key, $!index);
- self.compare_substitution($_,
@moritz
moritz / grammar-experiment.p6
Created October 12, 2014 20:27
Trying to construct a proto token
use v6;
my $re = rx{ a+ };
my $str = 'ab';
my $grammar := (grammar {}).HOW.new_type();
my $HOW := $grammar.HOW;
$HOW.add_method($grammar, 'trans', my proto token trans {*});
$HOW.add_method($grammar, "trans:sym<1>", $re);
$HOW.add_method($grammar, "trans:sym<2>", regex { $str });