Skip to content

Instantly share code, notes, and snippets.

View SegFaultAX's full-sized avatar

Michael-Keith Bernard SegFaultAX

View GitHub Profile
@SegFaultAX
SegFaultAX / gist:9303938
Last active August 29, 2015 13:56
functional.js
function argList(args) {
return Array.prototype.slice.call(args);
}
function reduce0(fn, l, init) {
for(var i = 0; i < l.length; i++) {
init = fn(init, l[i]);
}
return init;
}
@SegFaultAX
SegFaultAX / gist:9615203
Created March 18, 2014 07:34
processing.js - simple grid test
"use strict";
var H_PADDING = 10;
var V_PADDING = 10;
var ROWS = 20, COLUMNS = 20;
function app(processing) {
var midX, midY, colWidth, rowHeight;
var colors = {
@SegFaultAX
SegFaultAX / gist:9615583
Created March 18, 2014 08:10
Functional sets in Javascript
function empty() {
return function() {
return false;
};
}
function singleton(e0) {
return function(e) {
return e0 === e;
};
@SegFaultAX
SegFaultAX / gist:9732254
Last active August 29, 2015 13:57
Almost snake.js...
"use strict";
var H_PADDING = 10;
var V_PADDING = 10;
var ROWS = 20,
COLUMNS = 20;
var KEYCODES = {
37: "left",
38: "up",
@SegFaultAX
SegFaultAX / gist:10921677
Last active August 29, 2015 13:59
General purpose tuple parser for Python
from decimal import Decimal
def _wrap_converter(fn):
def _wrapper(elem):
try:
return fn(elem)
except Exception as e:
msg = "Failed to convert value {0}: {1}".format(
elem, e)
raise ValueError(msg)
@SegFaultAX
SegFaultAX / gist:10939429
Created April 16, 2014 22:34
Dotted path expansion in Clojure
(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))))
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)
@SegFaultAX
SegFaultAX / gist:629a3a8c15b0fd188000
Last active August 29, 2015 14:01
Python -> Lua serialization
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:
@SegFaultAX
SegFaultAX / gist:f48fe6930d18b52118ab
Last active August 29, 2015 14:05
Reducers in Python [incomplete]
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"""
@SegFaultAX
SegFaultAX / gist:520fe589dfe22ec10ae8
Created March 7, 2015 09:14
Internal Iterators in Python
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 = []