Skip to content

Instantly share code, notes, and snippets.

@darkf
darkf / lc.hs
Created March 5, 2014 01:58
Simply typed lambda calculus interpreter in Haskell
import Data.Maybe (fromJust)
data Type = IntTy
| ArrowTy Type Type
deriving (Show, Eq)
data Term = Const Int
| Var String
| Abs String Type Term
| App Term Term
@darkf
darkf / event.cpp
Created February 8, 2014 11:57
Simple event system in C++
#include <functional>
#include <map>
#include <typeinfo>
#include <iostream>
struct Event {
virtual ~Event() {}
};
struct TestEvent : Event {
std::string msg;
@darkf
darkf / sdl_test.nim
Last active December 31, 2015 05:18
SDL test in Nimrod
import math
import sdl
const
WIDTH = 1024
HEIGHT = 768
TIMESTEP = 1000 div 30 # 30 fps
THING_WIDTH = 32
@darkf
darkf / interp_pattern.hs
Created October 18, 2013 10:15
Interpreter for a simple dynamically typed pattern matching language
-- Interpreter for a simple dynamically typed pattern matching language
-- Copyright (c) 2013 darkf
-- Written on 10/18/2013
import Control.Monad.State (State, runState, evalState, get, put)
import qualified Data.Map as M
data AST = Add AST AST
| Def String AST
| Var String
@darkf
darkf / interp.hs
Last active December 25, 2015 17:49
Barebones interpreter in Haskell showing `State s a`
import Control.Monad.State (State, runState, evalState, get, put)
import qualified Data.Map as M
data AST = Add AST AST
| Def String AST
| Var String
| StrConst String
| IntConst Int
deriving (Show, Eq)
@darkf
darkf / possumparens.md
Created July 3, 2013 01:56
Proposal for optional parentheses in Possum

If we extend the Possum language to include optional parentheses, which group together tokens (atoms and constants), and when evaluated act as function applications, and when quoted act as homoiconic representations of a program.

Consider the following pseudo-Possum program:

defun compose f g is
  -> x is
    f g x
  end
end
@darkf
darkf / eval.hs
Last active December 18, 2015 07:00
Haskell 3-stage Brainfuck parser/reducer/evaluator
module Eval (bf_eval) where
import qualified Data.Vector.Mutable as MV
import Control.Monad.ST (runST)
import Data.Char (chr)
import Reducer
bf_eval :: [ISC] -> String
bf_eval instrs =
runST $ bf_eval' instrs
where
@darkf
darkf / bf.ml
Created May 30, 2013 11:29
Brainfuck interpreter in OCaml
let eval str =
let cells = Array.make 4096 0 in
let ptr = ref 0 in
let len = String.length str in
let rec evalbf c =
if c >= len then
c
else
match String.get str c with
@darkf
darkf / ecs.py
Last active July 21, 2017 05:03
Hypothetical Entity-Component System in Python
# Let's define a hypothetical Entity-Component System for a one-dimensional text-based game.
# Copyright (c) 2013 darkf
# First authored 5/14/2013
# Licensed under the terms of the WTFPL
from magic import Game, Entity, Component, system, query
# Components are pure-data models
PositionC = Component(x=0)
@darkf
darkf / midsum.py
Last active December 16, 2015 21:28
Find the indices where the sum on both sides of the medians is equal, or something
import math
def sumPoint(nums, n):
acc = 0
for i,x in enumerate(nums):
acc += x
if acc == n:
return i
return None