Skip to content

Instantly share code, notes, and snippets.

View derrickturk's full-sized avatar
💭
(Come in under the shadow of this red rock)

Derrick Turk derrickturk

💭
(Come in under the shadow of this red rock)
View GitHub Profile
@derrickturk
derrickturk / vecs.kk
Created July 29, 2022 02:30
Fun with heap and vector in Koka
fun iota_vec(n: int): vector<int>
vector-init(n, id)
fun oddity(n: int): exn vector<int>
var v := iota_vec(n)
for(0, n) fn(i)
v[i] := v[i] + 3
v
fun space-oddity(n: int): exn vector<int>
@derrickturk
derrickturk / ext.kk
Created July 29, 2022 02:06
Extensible variant types (I think? they're not documented except in the grammar) in Koka
open type openvar
A
B
extend type openvar
C
fun do_it_try_it(v: openvar): console ()
match v
A -> println("got an A")
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@derrickturk
derrickturk / save.eff
Created July 23, 2022 03:47
now this is podracing
(* yeah, so, Eff doesn't actually support polymorphic effects *)
effect Save: string -> int
effect Load: string * int -> empty
let save name = perform (Save name)
let load name v = absurd (perform (Load (name, v)))
let savegames = handler
| effect (Save name) k ->
@derrickturk
derrickturk / quicksave.eff
Last active July 23, 2022 03:32
play a game of "Guess or Die", with aggressive save-scumming
effect QuickSave: int option
effect QuickLoad: int -> empty
let quicksave () = perform QuickSave
let quickload n = absurd (perform (QuickLoad n))
let quicksaving = handler
| effect QuickSave k ->
let rec quickloading () = handler
| effect (QuickLoad n) _ ->
@derrickturk
derrickturk / revtree.pl
Created July 11, 2022 20:25
Stunting on Dumb Twitter Coding Interview guy, in Prolog
tree(nil).
tree([_, Left, Right]) :- tree(Left), tree(Right).
revtree(nil, nil).
revtree([Data, RLeft, RRight], [Data, Left, Right]) :-
revtree(RLeft, Right),
revtree(RRight, Left).
@derrickturk
derrickturk / ziphead.py
Created June 30, 2022 15:38
Dump the first N lines of CSV files inside a ZIP archive
import sys
from zipfile import ZipFile
LINES = 10
def main(argv: list[str]) -> int:
with ZipFile(argv[1]) as z:
for fn in z.namelist():
if not fn.endswith('.csv'):
continue
@derrickturk
derrickturk / fix_n.ml
Last active June 13, 2022 19:54
various n-adic fixed-point combinators
let rec fix f x = f (fix f) x
let fac =
let fac' f n = if n = 0 then 1 else n * f (n - 1) in
fix fac'
let rec fix2 f =
let rec a inp =
let (fix_a, _) = f (fix2 f) in
fix_a inp
@derrickturk
derrickturk / extensible_variants.ml
Created May 23, 2022 15:06
fun with extensible variants, including extensible GADTs
(* extensible variant types, including GADTs
* not to be confused with polymorphic variants
*)
type extend_me = ..
type extend_me += This
type extend_me += That of int
type extend_me += TheOther of string
use std::io::{self, BufRead};
fn rot13(s: &str) -> String {
s.chars().map(|c| {
if c.is_ascii_lowercase() {
((c as u8 - b'a' + 13) % 26 + b'a') as char
} else if c.is_ascii_uppercase() {
((c as u8 - b'A' + 13) % 26 + b'A') as char
} else {
c