Skip to content

Instantly share code, notes, and snippets.

View codesections's full-sized avatar

Daniel Sockwell codesections

View GitHub Profile
codesections / omni-sigil.raku
Created December 21, 2022 14:26
A Raku class that implements all three of the Positional, Associative, and Callable roles
#| A class that implements the Positional, Associative, and Callable roles.
#| Accordingly, this type can be bound variables with any sigil
class Seuss does Positional does Associative does Callable does Iterable {
= Nil, '🐠', '<°))))><';
= (:blue<🐟>, :red<🐡>);
method of { Mu }
codesections / Handles.rakumod
Created February 21, 2022 20:22
Grammar::Handles, a Raku module for grammar delegation
# Grammar::Handles
my module Grammar::Handles::Helpers {
class X::Grammar::Can'tHandle is Exception {
has $.type is required;
multi method CALL-ME(|c) { die|c)}
method message { q:to/§err/.trim.indent(2);
The `handles` grammar trait expects a Grammar, the name
of a Grammar, a Pair with a Grammar value, or a list of
any of those types. But `handles` was called with:
codesections / Subgrammar.rakumod
Created February 16, 2022 07:07
A role for using multiple grammars/action objects at once in Raku
role Subgrammar[::G :$grammar, :$actions] {
multi method slag(G) {
my $old-actions = self.actions;
self.set_actions: $actions;
my @wrapped = G.^methods(:local).map: -> &m {
with self.^methods.first({.name eq &}) {
next if $_ === &m or .name eq 'BUILDALL';
.wrap: method (|c) { m self, |c } }
codesections /
Created December 7, 2021 18:35
Most depended on Raku packages (2021-11-24)

This list was generated using the Citation Indices module by Richard Hainsworth (finanalyst) and lists the 30 most-often-depended-modules in the Raku ecosystem as of 2021-11-24 (including both direct and transitive dependencies). For more info, see the ModuleCitation module; thanks, Richard!

Module Name Recursive Citation Index Module Description
JSON::Fast 42.37
MIME::Base64 31.45 Encoding and decoding Base64 ASCII strings
File::Directory::Tree 30.89 Port of File::Path::Tiny - create and delete directory trees
URI 28.44 A URI implementation using Raku grammars to implement RFC 3986 BNF
OpenSSL 24.11 OpenSSL bindings
File::Temp 23.35
codesections / immitating-julia.raku
Created September 7, 2021 18:49
Raku is pretty flexible
constant gcd = &[gcd];
constant collect = *.bounds;
constant extrema = &minmax;
my &find_gcd = gcd ∘ collect ∘ extrema;
codesections /
Last active June 2, 2021 03:22
Grant Proposal for Persistent Data Types

Persistent Data Structures for Raku


Immutable, persistent data structures give a program certain superpowers that it's very hard to have in any other way: they allow the program to "time travel" (view previous application state); they allow let the program share data across threads or asynchronously save it to disk without needing locks; they enable a much more purely functional style of programming – which results in code that many software developers find much easier to reason about. Because of these benefits, many languages with strong support for functional programming (Clojure, Elm, Haskell, etc) offer

codesections / try-catch.raku
Created March 8, 2021 21:13
Comparing `try` to a CATCH block in Raku
my $result = do try {
# some code that may throw X::This
# some code that may throw X::NotSpecified (default)
# some code that may throw X::Something
# some code that may throw X::This or X::That
sub postfix:<‽>($a) { $ = $a};
class A { method n { Nil };
method fine { self }
method non-nil { 42}
codesections / 10.dyalog
Created December 11, 2019 03:23
Advent of Code day 10
in←↑⊃⎕nget '10.input' 1 ⋄ p←'#'=in
0=+/¯1↓{p[⍵[0];⍵[1]]}¨⍺ els_btwn ⍵ }
els_btwn←{ ⍺≡⍵:⍬
n←⍺+(⍺ slope_btwn ⍵)
(⊂n),(n ∇ ⍵) }
codesections / 09.dyalog
Created December 10, 2019 00:36
Advent of Code day 9
⍺←0 0 ⋄ (i rb)←⍺ ⋄ val←⊃⍵[0] ⋄ addr←⊃⍵[1]
get←{ ⍵<0: ⎕←'ERR: invalid negative index'
addr,←⍵ ⋄ val,←0 ⋄ ∇ ⍵ }
set←{ (addr⍳⍺)<(≢addr):((addr⍳⍺)⌷val)←⍵
addr,←⍺ ⋄ val,←⍵ }
instr←(10 10 10 10 10)⊤(get i) ⋄ opcode←(10 10)⊥¯2↑instr ⋄ p_modes←⌽3↑instr