Skip to content

Instantly share code, notes, and snippets.

/ script to expose an apparent bug in kona when combining
/ higher-order verbs.
/ j-style "monadic fork": our first higher order verb.
mf: {[f;g;h] { g[f[x]; h[x]] }}
/ cheq = check equality of two strings. (or complain if mismatched)
cheq: { :[ x~y; echo "."; echo "\nfailed: (", x, ") ~ (", y, ")\n"] }
@tangentstorm
tangentstorm / autohash.sql
Created February 4, 2016 21:59
automatic hash field in MySQL, just for fun.
DROP TABLE IF EXISTS x;
CREATE TABLE x (
id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
x_data CHAR(6) NOT NULL,
x_hash CHAR(6) UNIQUE NOT NULL);
-- Change the delimiter so we can use ';' in the trigger.
DELIMITER $$
theory QSort
imports Main
begin
fun qsort :: "'a::ord list ⇒ 'a list" where
"qsort [] = []"
| "qsort (x#xs) = (qsort (filter (λy. y≤x) xs))
@ x#(qsort (filter (λy. y>x) xs))"
theory SumOfRange
imports Main
begin
fun sum :: "nat list ⇒ nat" where
"sum xs = foldl (op+) 0 xs"
fun egnar :: "nat ⇒ nat list" where
"egnar x = (if x ≤ 0 then [] else x # egnar (x-1))"
@tangentstorm
tangentstorm / zeros.4th
Created January 3, 2016 03:18
mockup of how i might implement something to pre-initialize an array of zeros in forth
: make-array ( n -- ) cells allot ;
: fill-array ( count addr val -- )
( figure this out yourself )
;
: zeros ( n -- ) dup here swap make-array 0 fill-array ;
( usage example: )
defmodule QuickSort do
@doc "return a random 32-bit integer"
def rand(), do: round(:rand.uniform(round(:math.pow(2,31))) - :math.pow(2,30))
@doc "return a list of n random 32-bit integers"
def rand_nums_works(n) do for _ <- 1..n do rand end end
@doc "return a list of n random 32-bit integers"
def rand_nums_fails(n), do: for _ <- 1..n do rand end
@tangentstorm
tangentstorm / answer_converter.py
Created December 21, 2015 03:36
a python program to convert some test answer keys. merry christmas.
import os
def parse(filename):
data = {}
for line in open(filename).read().split("\n"):
if not line.strip(): continue
for pair in line.split(';'):
key, val = pair.split('.')
data[key]=val
return data
@tangentstorm
tangentstorm / Elixtris.exs
Last active December 20, 2015 00:21
start on learntris in elixir
defmodule Elixtris do
def main(buf) do
if buf == '' do main(IO.gets('') |> String.to_char_list)
else [ch|tail] = buf
unless [ch] == 'q' do
case [ch] do
'p' -> IO.puts("not yet")
' ' -> []
'\n' -> []
ch -> IO.puts('unknown command:' ++ [ch])
@tangentstorm
tangentstorm / output.txt
Last active December 9, 2015 04:51
shabits.py
goal: 66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925
mine: 66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925
from operator import add,mul
class nda(list):
"""n-dimensional arrays"""
rot=lambda xs,n: nda(xs[-n:] + xs[:-n][:n] + xs[n:-n]) # rotate
len=lambda xs : nda([len(xs)]) # length
wrp=lambda xs,n: nda([nda(xs[i:i+n]) for i in range(0,len(xs),n)]) # wrap
rsh=lambda xs,n,*ns: nda(sum([w.rsh(*ns) for w in xs.wrp(n)],[]) if ns else xs.wrp(n)) # reshape
prod=lambda xs: reduce(mul, xs) # product