Skip to content

Instantly share code, notes, and snippets.

tokenized_string = "2 + 3 * 4".split()
# parsing arithmetic expressions with + and *
# A parser returns (parsed_thing, new_index) on success.
def fail(): raise Exception("failed to parse")
def is_num(x):
try: int(x)
except ValueError: return False
module Main where
import Control.Monad (mapM_)
import Data.List (intersperse)
import System.CPUTime
import Text.Printf
import qualified Data.Set as Set
import Text.Derp
import subprocess
import threading
import sys
the_cvar = threading.Condition(threading.RLock())
turn = 0
NTHREADS = 20
WINDOW = 1
ITERS = 100
@rntz
rntz / gist:9fcab35ba7baa428c5f1
Last active October 27, 2015 14:56 — forked from jamii/gist:4107eb8dce3b90f55fff
expression-oriented syntax for imp/datalog
?an:id can fly
= ?an is a bird
- ?an is a penguin
+ ?an is Harry the Rocket Penguin
// nb. left-associative; a - b + c = (a - b) + c.
safe path from ?a to ?c
= edge from ?a to ?c
+ (edge from ?a to ?b
- ?b is dangerous
{-# LANGUAGE FlexibleInstances, DeriveGeneric #-}
-- http://design.perl6.org/S09.html#Junctions
module Junction where
import GHC.Generics
import Test.QuickCheck
import Control.Applicative
data Junction a = Pure a
@rntz
rntz / fix.mli
Last active April 24, 2016 21:31
a weird sort of recursive dataflow system
(* a weird sort of recursive dataflow system? *)
module type FLOW = sig
type key
type 'a exp
(* applicative structure *)
val pure : 'a -> 'a exp
val (~) : ('a -> 'b) exp -> 'a exp -> 'b exp
(* lazily computing a fixed-point map *)
// each state element is of the form [value, bitmask, expression]
// - `expression` is a string representing an arithmetic expressoin
// - `value` is the arithmetic value of `expression`
// - `bitmask` is a bitmask recording which of the 4 numbers (6,6,5,2) we've
// used
const state = [[6,1,"6"],[6,2,"6"],[5,4,"5"],[2,8,"2"]];
// combines compatible states using arithmetic operations to produce new states.
//
// x and y are states (see above)
{-# LANGUAGE DatatypeContexts #-}
module Tmp where
data Ord a => Set a = Empty
| Single a
| Union (Set a) (Set a)
instance Monad Set where
return x = Single x -- type errors b/c can't find Ord instance
module Arithmetic where
import Data.Set hiding (map)
solve target inputs = [e | e <- exprs inputs, Just target == eval e]
-- Expressions
data Expr = Num Rational | Op Op Expr Expr deriving (Eq, Ord)
instance Show Expr where
show (Num n) = show n