Instantly share code, notes, and snippets.

View anotherparsonexample.py
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.
"""
View unmashing.py
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}
View bummed_segment2.py
from __future__ import division
from collections import defaultdict
# ... rest of original code from Norvig, ngrams.py ...
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)
View data_defs.py
# 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")
View leetcode_decode.py
"""
https://leetcode.com/problems/decode-string/
https://github.com/darius/parson
"""
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 https://github.com/darius/squeam
(define (max-path-sum tree)
(let (best best-to-root) (solve tree))
best)
(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
1:a(b|c)(b|c)*d:addomen
1:ab*c:abd
1:ab:aab
1:ba*:
1:bababy:babababyish
1:X:
1:X:wheee
1:X:yX
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