Skip to content

Instantly share code, notes, and snippets.

View codesections's full-sized avatar

Daniel Sockwell codesections

View GitHub Profile
@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]
@codesections
codesections / 05.dyalog
Last active December 6, 2019 04:13
Advent of Code day 5
⎕IO←0
intcode←{
⍺←0 ⋄ i←⍺ ⋄ s←⍵ ⋄ instr←'0'(,⍣{4<≢⍺})⍕⍺⌷⍵ ⋄ opcode←⍎¯2↑instr ⋄ p_modes←⍎¨⌽3⍴instr
p←{mode←p_modes[⍵-1]
mode=0:s[i+⍵]
mode=1:i+⍵
'ERR: Unknown parameter mode',mode}
i≥≢s:'ERR: no HALT opcode'
@codesections
codesections / 02.dyalog
Last active December 3, 2019 02:30
Advent of Code day 2
⎕IO←0
find←{
⍺←0
in←(⊃⍺⌷,(⍳100)∘.,⍳100) set ⊃0⌷⍵
(⊃1⌷⍵)=0⌷intcode in:in[1 2]
(⍺+1)∇in (⊃1⌷⍵)
}
set←{s←⍵ ⋄ s[1 2]←⍺[0 1] ⋄ s}
intcode←{
⍺←0 ⋄ s←state←⍵
@codesections
codesections / xmodmap.sh
Created August 21, 2019 22:26
Output of `xmodmap -pke`
keycode 8 =
keycode 9 = Escape NoSymbol Escape
keycode 10 = 1 exclam 1 exclam
keycode 11 = 2 at 2 at
keycode 12 = 3 numbersign 3 numbersign
keycode 13 = 4 dollar 4 dollar
keycode 14 = 5 percent 5 percent
keycode 15 = 6 asciicircum 6 asciicircum
keycode 16 = 7 ampersand 7 ampersand
keycode 17 = 8 asterisk 8 asterisk
@codesections
codesections / linked_list.rs
Created January 6, 2019 23:02
A simple linked list implemented in Rust
#[derive(Debug)]
pub struct LinkedList {
head: Option<Box<Node>>,
tail: Option<*mut Node>,
}
#[derive(Debug)]
struct Node {
value: i32,
next: Option<Box<Node>>,
}
@codesections
codesections / factorial.lisp
Created February 23, 2019 16:43
Lisp example
(defun factorial (n)
(if (zerop n) 1 (* n (factorial (1- n)))))