Skip to content

Instantly share code, notes, and snippets.

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 / demo.tsx
Created Feb 10, 2021
React Hooks demo with TypesScript and no funny module-loader business
View demo.tsx
function Counter()
{
const [count, setCount] = React.useState(0);
return (
<div>
<p>Count = {count}</p>
<button onClick={() => setCount(count + 1)}>
Click me! Click me! Do it now, you son of a bitch!
</button>
</div>
@derrickturk
derrickturk / iso.pie
Created Jan 11, 2021
Fun with isomorphisms in Pie
View iso.pie
#lang pie
(claim Vect
(→ U Nat
U))
(define Vect
(λ (A n)
(iter-Nat
n
Trivial
@derrickturk
derrickturk / ackermann.pie
Created Jan 11, 2021
Programs from "The Little Typer"
View ackermann.pie
#lang pie
(claim + (→ Nat Nat Nat))
(define +
(λ (x y)
(iter-Nat x
y
(λ (acc) (add1 acc)))))
(claim repeat
@derrickturk
derrickturk / append_loop_vs_list_comprehension.py
Created Dec 30, 2020
Python - the devil is in the details
View append_loop_vs_list_comprehension.py
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import dis
>>> def f_list():
... xs = list()
... for i in range(10):
... xs.append(i * i)
... return xs
...
>>> def f_comp():
@derrickturk
derrickturk / csv_merge.py
Created Dec 16, 2020
merge structurally similar CSV files in "per-case" directories, tagging rows
View csv_merge.py
import sys
import os
import os.path
import csv
from typing import List, Optional, Set
# given a list of directories d1, d2, d3
# each containing a common set of CSV files with the same columns:
# d1/a.csv d1/b.csv
View badlist.hs
{-# LANGUAGE RankNTypes #-}
newtype Listy a = Listy { runListy :: forall b . (a -> b -> b) -> b -> b }
nil :: Listy a
nil = Listy $ \_ init -> init
cons :: a -> Listy a -> Listy a
cons x xs = Listy $ \fn init -> fn x (runListy xs fn init)
@derrickturk
derrickturk / badlist.py
Created Dec 15, 2020
a bad representation for lists
View badlist.py
def cons(x, lis):
return lambda t: x if t else lis
def nil():
return None
def foldr(fn, lis, init):
if lis is None:
return init
head = lis(True)
@derrickturk
derrickturk / windows.hs
Created Dec 9, 2020
Sliding-windows in Haskell
View windows.hs
takeExactly :: Int -> [a] -> Maybe [a]
takeExactly 0 _ = Just []
takeExactly n [] = Nothing
takeExactly n (x:xs) = (x:) <$> takeExactly (n - 1) xs
windows :: Int -> [a] -> [[a]]
windows n xs = case takeExactly n xs of
Nothing -> []
Just window -> window:windows n (tail xs)
@derrickturk
derrickturk / typecase.idr
Created Dec 5, 2020
Idris 2 - looks like typecase is back on the menu, boys!
View typecase.idr
perverse : {a : Type} -> a -> a
perverse {a = Integer} x = x + 1
perverse {a = String} x = "hello " <+> x
perverse {a = _} x = x
main : IO ()
main = do
printLn $ perverse 5
printLn $ perverse "world"
printLn $ perverse True
@derrickturk
derrickturk / treesoup.py
Last active Nov 19, 2020
walking the tree of soup
View treesoup.py
import requests
import sys
from bs4 import BeautifulSoup # type: ignore
from bs4.element import Tag, NavigableString # type: ignore
from typing import Callable, Dict, List
def retrieve(url: str) -> BeautifulSoup:
rq = requests.get(url)
rq.encoding = 'utf-8'