Skip to content

Instantly share code, notes, and snippets.

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 / 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 $actions = Moedict::Actions.new();
my @objects;
my @data = .lines # or whatever
(^@data).hyper.map: {
@objects[$_] = Moedict.parse(@data[$_], :actions($actions)) or die "$!";
}
@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 }
@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 }
use NativeCall;
use SDL2::Raw;
my int ($w, $h) = 320, 240;
SDL_Init(VIDEO);
my SDL_Window $window = SDL_CreateWindow(
"White Noise - Raku",
SDL_WINDOWPOS_CENTERED_MASK, SDL_WINDOWPOS_CENTERED_MASK,
multi expand-tree ( Bag $tree ) {
bag(bag(bag()) (+) $tree) (+)
[(+)] (
$tree.keys ==> map {
$^a.&expand-tree.map: * (+) ( $tree (-) bag($^a) )
}
);
}
multi expand-trees ( Bag $trees ) {
@thundergnat
thundergnat / Language_links.user.js
Last active January 23, 2023 01:07
Tampermonkey / Greasemonkey script to add Language link parameters to Rosettacode Language Category pages
@thundergnat
thundergnat / Toggle_syntax_highlighting.user.js
Last active August 30, 2022 00:24
Toggle syntax highlighting on Rosettacode task entries
// ==UserScript==
// @name Toggle syntax highlighting
// @namespace http://tampermonkey.net/
// @version 0.2
// @description toggle syntax highlighting on task pages
// @author thundergnat
// @match *://rosettacode.org/wiki/*
// @icon https://www.google.com/s2/favicons?domain=rosettacode.org
// @grant none
my %*SUB-MAIN-OPTS = :named-anywhere;
use Text::Sorensen :sorensen;
use JSON::Fast;
my $hashfile = './Sorenson-chars.json';
unit sub MAIN ( $phrase, $head = 10, :$ge = 0.5 );
my %out;