This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Implementing a fixed bit, nominally unsigned Integer class | |
# Major bummer; can not instantiate usefully in a $ sigiled scalar | |
class Fixed-Int { | |
has $!var handles <FETCH Str Numeric> = 0; | |
has $!bits; | |
has $!mask; | |
submethod BUILD (Int :bits(:$bit)) { $!bits = $bit; $!mask = 2**$!bits - 1 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Characters are expensive, and the accountants tell me we can’t hand them out | |
# willy-nilly anymore. Given a string x and a character y, how many times does y | |
# occur in x? | |
put "\nMultiplicity"; | |
sub Multiplicity (Str $a, Str $b) { $a.comb.Bag{$b} } | |
for < fhqwhgads h mississippi s life . > | |
-> $a, $b { put "$a, $b: ", Multiplicity $a, $b } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my $actions = Moedict::Actions.new(); | |
my @objects; | |
my @data = .lines # or whatever | |
(^@data).hyper.map: { | |
@objects[$_] = Moedict.parse(@data[$_], :actions($actions)) or die "$!"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use soft; | |
my @log; | |
sub loggit (*@args) { | |
my $return = try { callwith(|@args) }; | |
#say &?ROUTINE.name; # Off by one call frame | |
#say (&::CALLER::&?ROUTINE).name; # just plain wrong | |
#.say for Backtrace.new.list; # the wrapped sub name doesn't appear in the backtrace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my $id = 1; | |
my %customer = ( | |
$id++ => { :name('aa') :age(9) }, | |
$id++ => { :name('bb') :age(22) }, | |
$id++ => { :name('cc') :age(11) }, | |
); | |
say 'Sorted by age, descending:'; | |
say "ID# {.key}) Name: {.value<name>}, Age: {.value<age>}" | |
for %customer.sort( -*.value<age> ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my %core = ( | |
:A000001("groups") | |
:A000002("kolakoski") | |
:A000004("zeroes") | |
:A000005("divisors") | |
:A000007("zeros-powers") | |
:A000009("distinct-partitions") | |
:A000010("totient") | |
:A000012("ones") | |
:A000014("series-reduced-trees") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Generated by running in the perl6-Math-Sequences directory: | |
# grep '&NOSEQ ...' ./lib/Math/Sequences/Integer.pm6 | |
# If we don't yet have a formula for a given sequence, we use &NOSEQ in a | |
our @A000001 is export = 0, 1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, &NOSEQ ... *; | |
our @A000014 is export = 0, 1, 1, 0, 1, 1, 2, 2, 4, 5, 10, 14, &NOSEQ ... *; | |
our @A000019 is export = 1, 1, 2, 2, 5, 4, 7, 7, 11, 9, 8, 6, &NOSEQ ... *; | |
our @A000029 is export = 1, 2, 3, 4, 6, 8, 13, 18, 30, 46, 78, &NOSEQ ... *; | |
our @A000031 is export = 1, 2, 3, 4, 6, 8, 14, 20, 36, 60, 108, &NOSEQ ... *; | |
our @A000041 is export = 1, 1, 2, 3, 5, 7, 11, 15, 22, 30, 42, &NOSEQ ... *; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sub wat ($n) { | |
($n .. 1).map: -> $m { say "uno: $m" } | |
($n .. 1).map: -> $m { say "dos: $m" } | |
} | |
for ^2 -> $m { say "|$m|"; wat($m) } # explicit for loop variable | |
say ''; | |
for ^2 { say "|$_|"; wat($_) } # implicit for loop variable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Challenge #1 | |
#`[ Slow, naive, brute force perfect number calculation | |
sub is-perfect (Int $n) { | |
($n == sum grep $n %% *, 1 .. $n div 2) ?? | |
$n !! | |
Empty | |
} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Rat::Precise; # thundergnat++ | |
constant D = 54; # does double-duty: number-of-decimals (π), terms in Taylor series (𝑒) | |
constant d = 15; # digits past decimal for output | |
# exponentiation where base and exponent are both FatRat | |
multi infix:<**> (FatRat $base, FatRat $exp where * >= 1 --> FatRat) { | |
2 R** $base**($exp/2); | |
} |