Skip to content

Instantly share code, notes, and snippets.

Avatar

darkf

  • Zero page
View GitHub Profile
View lolnominaltyping.ts
interface Point2D {
x: number;
y: number;
}
interface Point3D extends Point2D {
z: number;
}
function f(pt: Point2D) {}
View HufmanBenchBS.hs
{-# 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 Oct 11, 2015
Simple Python hash table and Bloom filter
View bloom.py
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 Oct 11, 2015
Simple BK-tree implementation in Haskell
View BKTree.hs
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 Jun 5, 2015
Useless parallel stack allocator in C++
View alloc_task.cpp
#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 Dec 26, 2014
AABB Box Collision Test
View box_collide.js
// 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 Oct 31, 2014
JS-style lookups for Python dicts
View dotdict.py
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 Sep 9, 2014
A Haskell Concurrency pattern for servers
View concurrent.hs
{-# 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 Jul 7, 2014
Simple RISC-y VM in suboptimal Haskell
View Main.hs
module Main where
import Test.Hspec
import VM
($=) = shouldBe
main :: IO ()
main = hspec $ do
describe "runVM" $ do
@darkf
darkf / closureobjs.rkt
Last active Aug 29, 2015
Closures as Stateful Objects
View closureobjs.rkt
#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)
You can’t perform that action at this time.