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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
… |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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←{{⍵÷(∨/⍵)}-⍺-⍵} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
⎕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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
⎕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←⍵ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(Debug)] | |
pub struct LinkedList { | |
head: Option<Box<Node>>, | |
tail: Option<*mut Node>, | |
} | |
#[derive(Debug)] | |
struct Node { | |
value: i32, | |
next: Option<Box<Node>>, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun factorial (n) | |
(if (zerop n) 1 (* n (factorial (1- n))))) |