This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn deep-merge-with | |
"Like merge-with, but preserves shared key paths, recursively." | |
[f & maps] | |
(letfn [(merge-elem [m e] | |
(let [k (key e) v (val e) src (m k)] | |
(if src | |
(if (and (map? src) (map? v)) | |
(assoc m k (merge2 src v)) | |
(assoc m k (f src v))) | |
(assoc m k v)))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import partial | |
def identity(e): | |
return e | |
def walk(inner, outer, coll): | |
if isinstance(coll, list): | |
return outer([inner(e) for e in coll]) | |
elif isinstance(coll, dict): | |
return outer(dict([inner(e) for e in coll.iteritems()])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def take_while(fn, coll): | |
"""Yield values from coll until fn is False""" | |
for e in coll: | |
if fn(e): | |
yield e | |
else: | |
return | |
def partition(n, coll, step=None): | |
return take_while(lambda e: len(e) == n, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def categorize(fn, coll): | |
"""Like group_by, but fn returns multiple keys""" | |
acc = {} | |
for e in coll: | |
keys = fn(e) | |
for key in keys: | |
if key not in acc: | |
acc[key] = [] | |
acc[key].append(e) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SPECIAL_DELIM = [("[{}[".format("="*n), "]{}]".format("="*n)) for n in range(10)] | |
def type_of(v, *types): | |
return any(isinstance(v, t) for t in types) | |
def get_delim(s): | |
if '"' not in s: and "\n" not in s: | |
return ('"', '"') | |
for op, cl in SPECIAL_DELIM: | |
if op not in s and cl not in s: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import csv | |
import argparse | |
from itertools import islice | |
def load_csv(filename, headers=None): | |
reader = csv.reader(open(filename, "rU")) | |
if headers is None: | |
headers = next(reader) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
import time | |
import contextlib | |
def format_checkpoints(cps): | |
pairs = [cps[i:i+2] for i in range(len(cps)-1)] | |
fmts = ["{0[0]} to {1[0]} in {2:.04f}s".format(start, end, end[1] - start[1]) | |
for start, end in pairs] | |
return ", ".join(fmts) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import copy | |
class ReducedException(Exception): | |
def __init__(self, reduced_value): | |
super(ReducedException, self).__init__() | |
self.reduced_value = reduced_value | |
def conj(coll, *es): | |
"""Conjoin elements to coll. Works correctly for most collection types""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
import time | |
import functools | |
import contextlib | |
LOG_FMT = "{name}: {cp} @{total:0.4f}s ({dur:0.4f}s since {prev})" | |
def coroutine(f): | |
@functools.wraps(f) | |
def coro(*args, **kwargs): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Enumerable(object): | |
def map(self, fn): | |
acc = [] | |
self.each(lambda xs: acc.append(fn(xs))) | |
return self.__class__(acc) | |
collect = map | |
def filter(self, pred): | |
acc = [] |