Skip to content

Instantly share code, notes, and snippets.

View codesections's full-sized avatar

Daniel Sockwell codesections

View GitHub Profile
@codesections
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 {
has @.pos handles <AT-POS EXISTS-POS DELETE-POS ASSIGN-POS BIND-POS elems>
= Nil, '🐠', '<°))))><';
has %.asc handles <AT-KEY EXISTS-KEY DELETE-KEY ASSIGN-KEY BIND-KEY>
= (:blue<🐟>, :red<🐡>);
method of { Mu }
@codesections
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 self.new(|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
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 &m.name}) {
next if $_ === &m or .name eq 'BUILDALL';
.wrap: method (|c) { m self, |c } }
}
@codesections
codesections / top-dependencies_2021-11-24.md
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
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
codesections / PDT-grant.md
Last active June 2, 2021 03:22
Grant Proposal for Persistent Data Types

Persistent Data Structures for Raku

Synopsis

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
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}
};
say A.new.fine‽
.non-nil;
say A.new.n‽
.non-nil;
@codesections
codesections / 10.dyalog
Created December 11, 2019 03:23
Advent of Code day 10
in←↑⊃⎕nget '10.input' 1 ⋄ p←'#'=in
can_see←{⍺≡⍵:0
0=+/¯1↓{p[⍵[0];⍵[1]]}¨⍺ els_btwn ⍵ }
els_btwn←{ ⍺≡⍵:⍬
n←⍺+(⍺ slope_btwn ⍵)
(⊂n),(n ∇ ⍵) }
slope_btwn←{{⍵÷(∨/⍵)}-⍺-⍵}
@codesections
codesections / 09.dyalog
Created December 10, 2019 00:36
Advent of Code day 9
intcode←{
⍺←0 0 ⋄ (i rb)←⍺ ⋄ val←⊃⍵[0] ⋄ addr←⊃⍵[1]
get←{ ⍵<0: ⎕←'ERR: invalid negative index'
(addr⍳⍵)<(≢addr):(addr⍳⍵)⌷val
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
p←{mode←p_modes[⍵-1]