Skip to content

Instantly share code, notes, and snippets.

@grondilu
grondilu / Metamath.rakumod
Last active October 26, 2023 23:53
Metamath raku grammar
# Metamath commentless grammar
# remove comments beforehand with
# .subst: / '$(' ~ '$)' .*? /, '', :g
unit grammar Metamath;
rule TOP { ^ <database> $ }
rule database { <outermost-scope-stmt>* }
rule outermost-scope-stmt {
@grondilu
grondilu / FR
Created October 20, 2023 12:44
rakudo Frenchification file
# This file contains that French "translation" of features of the
# Raku Programming Language.
#
# See tools/build/L10N/README.md for more explanation.
# KEY TRANSLATION
block-default défaut
block-else sinon
block-elsif ousi
block-for pour
@grondilu
grondilu / quadirrat.raku
Last active September 26, 2023 19:18
quadratic irrationals in raku
unit class QuadraticIrrational does Real;
#
# This role cannot meaningfully be converted
# into a core type, so we'll "burn the bridge"
method Bridge {!!!}
has UInt $.discriminant;
has Rat(Cool) ($.u, $.v);
submethod TWEAK {
@grondilu
grondilu / unum.rakumod
Last active September 24, 2023 15:33
unum in raku
unit module Unum;
=begin LICENSE
Copyright © 2017 John L. Gustafson
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sub-license, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
@grondilu
grondilu / rice.rakumod
Last active September 20, 2023 13:39
Rice coding in raku
unit module Rice;
# L<https://en.wikipedia.org/wiki/Golomb_coding>
our sub encode(Int $n, UInt :$k = 2) {
my $d = 2**$k;
my $q = $n div $d;
my $b = sign(1 + sign($q));
my $m = abs($q) + $b;
flat
$b xx $m, 1 - $b,
@grondilu
grondilu / bech32.raku
Created April 25, 2022 06:28
Bech32 in raku
grammar Bech32 {
constant @data-charset = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l'.comb;
constant %data-charset = @data-charset.pairs.invert;
sub polymod(@values --> UInt) {
constant @gen = 0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3;
my uint32 $chk = 1;
for @values -> $v {
my $b = $chk +> 25;
$chk = ($chk +& 0x1ffffff) +< 5 +^ $v;
@grondilu
grondilu / Ed25519.rakumod
Last active April 4, 2022 20:47
Ed25519 in raku
#!/usr/bin/env raku
unit module Ed25519;
sub sha512(blob8 $b) returns blob8 {
given run <openssl dgst -sha512 -binary>, :in, :out, :bin {
.in.write: $b;
.in.close;
return .out.slurp: :close;
}
}
@grondilu
grondilu / pbkdf2.bash
Last active September 21, 2023 22:42
PBKDF2 in bash
#!/usr/bin/env bash
# Translated from https://github.com/bitpay/bitcore/blob/master/packages/bitcore-mnemonic/lib/pbkdf2.js
# /**
# * PBKDF2
# * Credit to: https://github.com/stayradiated/pbkdf2-sha512
# * Copyright (c) 2014, JP Richardson Copyright (c) 2010-2011 Intalio Pte, All Rights Reserved
# */
declare hash_name="$1" key_str="$2" salt_str="$3"
declare -ai key salt u t block1 dk
@grondilu
grondilu / matrix.js
Last active May 28, 2020 19:56
Recursive matrix inversion
"use strict";
class Matrix {
constructor(a, b, c, d) {
if ([a,b,c,d].every(x => x instanceof Number || typeof(x) == "number")) {
this.n = 1;
} else if ([a,b,c,d].every(x => x instanceof Matrix && x.n == a.n)) {
this.n = a.n + 1;
} else throw new Error("wrong arguments type");
this.a = a;
@grondilu
grondilu / -
Created October 27, 2017 11:46
GATCACAGGTCTATCACCCTATTAACCACTCACGGGAGCTCTCCATGCATTTGGTATTTTCGTCTGGGGGGTATGCACGCGATAGCATTGCGAGACGCTGGAGCCGGAGCACCCTATGTCGCAGTATCTGTCTTTGATTCCTGCCTCATCCTATTATTTATCGCACCTACGTTCAATATTACAGGCGAACATACTTACTAAAGTGTGTTAATTAATTAATGCTTGTAGGACATAATAATAACAATTGAATGTCTGCACAGCCACTTTCCACACAGACATCATAACAAAAAATTTCCACCAAACCCCCCCTCCCCCGCTTCTGGCCACAGCACTTAAACACATCTCTGCCAAACCCCAAAAACAAAGAACCCTAACACCAGCCTAACCAGATTTCAAATTTTATCTTTTGGCGGTATGCACTTTTAACAGTCACCCCCCAACTAACACATTATTTTCCCCTCCCACTCCCATACTACTAATCTCATCAATACAACCCCCGCCCATCCTACCCAGCACACACACACCGCTGCTAACCCCATACCCCGAACCAACCAAACCCCAAAGACACCCCCCACAGTTTATGTAGCTTACCTCCTCAAAGCAATACACTGAAAATGTTTAGACGGGCTCACATCACCCCATAAACAAATAGGTTTGGTCCTAGCCTTTCTATTAGCTCTTAGTAAGATTACACATGCAAGCATCCCCGTTCCAGTGAGTTCACCCTCTAAATCACCACGATCAAAAGGAACAAGCATCAAGCACGCAGCAATGCAGCTCAAAACGCTTAGCCTAGCCACACCCCCACGGGAAACAGCAGTGATTAACCTTTAGCAATAAACGAAAGTTTAACTAAGCTATACTAACCCCAGGGTTGGTCAATTTCGTGCCAGCCACCGCGGTCACACGATTAACCCAAGTCAATAGAAGCCGGCGTAAAGAGTGTTTTAGATCACCCCCTCCCCAATAAAGCTAAAACTCACCTGAGTTGTAAAAAACTCCAGTTGACACAAAATAGACTACG