Skip to content

Instantly share code, notes, and snippets.

interface Point2D {
x: number;
y: number;
}
interface Point3D extends Point2D {
z: number;
}
function f(pt: Point2D) {}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}
import Data.List
import Data.Ord (comparing)
import Control.Applicative ((<|>))
import Criterion.Main
import GHC.Generics (Generic)
import Control.DeepSeq
@darkf
darkf / bloom.py
Created October 11, 2015 11:51
Simple Python hash table and Bloom filter
def sillyhash(x):
return x % 16
class BloomFilter:
SIZE = 32
HASH_FNS = [hash, sillyhash]
def __init__(self):
self.n = BloomFilter.SIZE
self.bitset = [0]*self.n
@darkf
darkf / BKTree.hs
Last active October 11, 2015 09:58
Simple BK-tree implementation in Haskell
data BKTree a = Node a [(Int, BKTree a)] deriving (Eq, Show)
lev :: Eq a => [a] -> [a] -> Int
lev [] b = length b
lev a [] = length a
lev a b = minimum [ lev (init a) b + 1
, lev a (init b) + 1
, lev (init a) (init b) + (if last a == last b then 0 else 1)
]
@darkf
darkf / alloc_task.cpp
Created June 5, 2015 05:37
Useless parallel stack allocator in C++
#include <cstring>
#include <iostream>
#include <thread>
#include <future>
#include <queue>
std::mutex mqMutex;
std::queue<std::tuple<size_t, std::promise<void*>&>> mq;
void * alloc(size_t len) {
@darkf
darkf / box_collide.js
Created December 26, 2014 10:38
AABB Box Collision Test
// So I don't lose it
function boxCollide(a, b) {
if ((a.y+a.h <= b.y) || (a.y >= b.y+b.h)) return false;
if ((a.x+a.w <= b.x) || (a.x >= b.x+b.w)) return false;
return true;
}
@darkf
darkf / dotdict.py
Created October 31, 2014 09:55
JS-style lookups for Python dicts
class dotdict(dict):
def __getattribute__(self, key):
if key in self:
return self[key]
return dict.__getattribute__(self, key)
d = dotdict(a=1, b=2)
print d.a, d.b
print d.c
@darkf
darkf / concurrent.hs
Created September 9, 2014 12:50
A Haskell Concurrency pattern for servers
{-# LANGUAGE LambdaCase #-}
import Control.Concurrent
import Control.Concurrent.Chan
import Control.Monad
import Network.Socket
import System.IO
data MainMessage = Accept Socket SockAddr
| MsgReceived String
@darkf
darkf / Main.hs
Created July 7, 2014 02:19
Simple RISC-y VM in suboptimal Haskell
module Main where
import Test.Hspec
import VM
($=) = shouldBe
main :: IO ()
main = hspec $ do
describe "runVM" $ do
@darkf
darkf / closureobjs.rkt
Last active August 29, 2015 14:01
Closures as Stateful Objects
#lang racket
(define (obj state msg)
(match msg
['init (cons state (curry obj state))]
['get-x (cons state (curry obj state))]
[(list 'set-x x) (cons x (curry obj x))]
['ping (cons "pong" (curry obj state))]))
(define-syntax-rule (send! obj msg)