Instantly share code, notes, and snippets.

from parson import Grammar
g = r"""
_: /\s*/.
ns: /[0-9]+/.
num: {ns ('.' ns | '.') | '.' ns | ns} _ .
e: num {'**'} _ num :to_prefix | num.
m: e {'/'|'*'} _ m :to_prefix | e.
a: m {'+'|'-'} _ a :to_prefix | m.
from __future__ import division
# layout: group keys that are typed by the same finger.
dvorak_layout = '`1!\'a; 2@,oq 3#ej $4puk%5yix ^6fdb&7ghm *8ctw (9rnv )0lsz[{]}/?+=|\\-_'.split()
qwerty_layout = '''`~!1qaz @2wsx #3edc $4rfv%5tgb ^6yhn&7ujm *8ik,< (9ol.> )0p;:?/-_[{'"=+]}|\\'''.split()
layout = qwerty_layout #dvorak_layout
finger = {ch: i
for i, chars in enumerate(layout)
for ch in chars}
from __future__ import division
from collections import defaultdict
# ... rest of original code from Norvig, ...
class Pdist(dict):
"A probability distribution estimated from counts in datafile."
def __init__(self, data=[], N=None, missingfn=None):
for key,count in data:
self[key] = self.get(key, 0) + int(count)
# h/t Dave Long
from collections import namedtuple
data = lambda s: {ws[0]: namedtuple(ws[0], ws[1:])
for term in s.split('|') for ws in [term.split()]}
Data = lambda s: globals().update(data(s))
Data("Nil | Cons head tail")
from parson import Grammar
decode = Grammar(r""" seq :end.
seq: part* :join.
part: /(\d+)/ '[' seq ']' :repeat
View gist:16bf45a4f25343484e775a5735e7a5a6
;; Max path sum in a binary tree.
;; tree ::= {leaf} | {branch value tree tree}
;; Coded in
(define (max-path-sum tree)
(let (best best-to-root) (solve tree))
(define (solve tree)
(match tree
View sort_of_overboard.hs
-- Like nfa.hs, but with numbered states, so that we can compare them
-- by index, to deduplicate them without looping forever.
-- (It does still loop on many (many (lit 'a')).)
-- Thanks to Libby Horacek for a bunch of help,
-- and Tom Murphy for pointing out Data.Vector.
import Data.List (nub)
import qualified Data.Vector as V
matches re cs = 0 `elem` foldl step starts cs
View nfa.hs
import qualified Data.Array as A
import Data.List (nub)
import Data.Monoid ((<>))
spec = do
print (many (lit 'a') `shouldMatch` "aaa")
print (many (lit 'a' `alt` lit 'a') `shouldNotMatch` "aaacf")
print (many (lit 'a' `alt` lit 'b') `shouldMatch` "abababa")
print ((lit 'a' . many (lit 'b' `alt` lit 'c') . lit 'd') `shouldMatch` "abcbd")
View regexercise-tests
1:(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1):0110001100011000110 how are you?
1:(aa|a)(aa|a)(aa|a)(aa|a)(aa|a)(aa|a)(aa|a)(aa|a)(aa|a)(aa|a)(aa|a)(aa|a)(aa|a)(aa|a)(aa|a)(aa|a)(aa|a)(aa|a)(aa|a)(aa|a)(aa|a)(aa|a)(aa|a)(aa|a)(aa|a)(aa|a)(aa|a)(aa|a)(aa|a)(aa|a)(aa|a)(aa|a)(aa|a)(aa|a)(aa|a)(aa|a)(aa|a)(aa|a)(aa|a)(aa|a):aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa b
View RABBIT.scm
;;- -*-scheme-*-
;;; rabbit compiler
;;- This is the source code to the RABBIT Scheme compiler, by Guy Steele,
;;- taken from the Indiana Scheme repository and annotated by me, Darius
;;- Bacon. I converted it from all-uppercase to all-lowercase and
;;- reindented it with Emacs for better readability. I've added the
;;- comments starting with either ;- or ;*. Other comments are by Steele.
;;- The ;- comments were things I'd figured out, while ;* denoted things
;;- for me to look into. (Sometimes I didn't bother to type in the answer