Skip to content

Instantly share code, notes, and snippets.

@thundergnat
thundergnat / natural-collation.p6
Last active September 20, 2017 23:26
Natural collation
sub natural ($a) { $a.subst(/(\d+)/, ->$/ {0~$0.chars~$0},:g)~$a }
sub ncollate (@list){ # this works... but yuck
@list[@list.kv.map( { [$^k, $^v.&natural] } ).sort( { $^a[1] unicmp $^b[1] } )[*;0]]
};
my @words = <32nd 3rd 144th 17th 2 95 apple ball Bald
Æon æon aether Évian evoke außen Autumn file3 file21 file17>;
#say @words.sort;
@thundergnat
thundergnat / collate-test.p6
Last active September 23, 2017 19:17
Collation numeric sorting test
# Testing against https://github.com/samcv/MoarVM/tree/natural
# using a Perl6 / NQP build against that branch.
# For these very limited character sets, I would expect these to return
# essentially the same results, except perhaps for digit clusters with leading zeros
use experimental :collation;
# Ideally, numerics would sort before alphabetics; the UCA sorts
# numbers after letters so that may not be practical.
(cb e4ae25d 1e3716ac46 5 83cc4a 677ced28 2456 7048bbe40 7098 7290ddc7)
(cb e4ae25d 1e3716ac46 5 83cc4a 677ced28 7098 2456 7048bbe40 7290ddc7)
------------------------------------------------------------
(a bc9835 1ea5969c5 3 3a7c47a33 8c654 80 83c3358 317995 632923)
(a bc9835 1ea5969c5 3 3a7c47a33 8c654 80 83c3358 632923 317995)
------------------------------------------------------------
(ada4 c e3 e9836a 6e 34e093 95 142a47c 513c2b 79388a0d)
(ada4 c e3 e9836a 6e 34e093 95 513c2b 142a47c 79388a0d)
------------------------------------------------------------
(da9 db39c67d3 e06d97 6750951822 5ab8a02 07be3e3a5c 32d 063 284c 9877ed6)
@thundergnat
thundergnat / ppm-convert-pipe.p6
Last active October 27, 2017 23:36
Problem writing to Proc :in on default filehandle
# When trying to read and write some PPM files through a pipe, able to read a Proc through a pipe using default filehandle '-'
# but not able to figure out the syntax to write to the default file handle throug a Proc pipe.
# The two scripts below demonstrate. Both require that imagemagick is installed and ready to use, specifically the 'convert' program.
# Using the camelia.png file from from:
# https://github.com/thundergnat/rc/blob/master/img/camelia.png
# downloaded to the same directory that the script is in; ppm-read.pipe.p6 succesfully reads a Proc on the
# default file handle to generate camelia.ppm, but ppm-convert.pipe.p6 is unable to write to a Proc on the
@thundergnat
thundergnat / xdosearch.p6
Created December 16, 2018 14:43
Having trouble figuring out calling conventions
use NativeCall;
class Xdo is export {
has Pointer $.id is rw;
submethod BUILD {
sub xdo_new(Str) returns Pointer is native('xdo') { * }
self.id = xdo_new('');
}
@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);
}
# 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 / 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
@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 / 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")