Skip to content

Instantly share code, notes, and snippets.

@thundergnat
thundergnat / FixedInt.p6
Created August 19, 2020 20:49
This fills me with nearly equal parts pleasure and horror
# 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 }
@thundergnat
thundergnat / string-chaos.p6
Created March 27, 2020 19:08
A World Without Strings Is Chaos
# 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 }
my $actions = Moedict::Actions.new();
my @objects;
my @data = .lines # or whatever
(^@data).hyper.map: {
@objects[$_] = Moedict.parse(@data[$_], :actions($actions)) or die "$!";
}
@thundergnat
thundergnat / logged.p6
Created January 26, 2020 15:52
wrapped routine caller name?
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
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> );
@thundergnat
thundergnat / core-id.p6
Created July 6, 2019 14:20
core sequences name-id hash
my %core = (
:A000001("groups")
:A000002("kolakoski")
:A000004("zeroes")
:A000005("divisors")
:A000007("zeros-powers")
:A000009("distinct-partitions")
:A000010("totient")
:A000012("ones")
:A000014("series-reduced-trees")
@thundergnat
thundergnat / Unimplemented sequences.txt
Created July 6, 2019 13:03
Sqashathon sequences needing updates
# 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 ... *;
@thundergnat
thundergnat / wat.p6
Last active June 1, 2019 15:22
Bizarre interaction between explict for loop variables and iterators
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
# Challenge #1
#`[ Slow, naive, brute force perfect number calculation
sub is-perfect (Int $n) {
($n == sum grep $n %% *, 1 .. $n div 2) ??
$n !!
Empty
}
]
@thundergnat
thundergnat / Ramanujan.p6
Last active April 20, 2019 14:12
Display configuration updates
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);
}