Skip to content

Instantly share code, notes, and snippets.

@jabney
jabney / binary-search.js
Last active May 6, 2017 02:04
Binary search with comparator
// The default comparator is for any values that can be
// compared with '==', '<', '<=', '>', '>='.
function defaultComparator(candidate, target) {
if (target < candidate) {
return -1
} else if (target > candidate) {
return 1
} else {
return 0
}
import os
import re
import gzip
import base64
import json
from glob import glob
from argparse import ArgumentParser
from KnoDB.crawler.scc import case_parser
from KnoDB.crawler import bcca
@jabney
jabney / post-transform-coords.js
Last active August 29, 2015 14:23
Javascript function to return post-transform coordinates of an element.
// Return the post-transform coords of an element,
// assuming that proper pre-transform coords are supplied.
function getElementCoords(element, coords) {
var ctm = element.getCTM(),
xn = ctm.e + coords.x*ctm.a,
yn = ctm.f + coords.y*ctm.d;
return { x: xn, y: yn };
};
var circle = document.getElementById('svgCircle'),
@jabney
jabney / factory-inheritance.js
Last active August 29, 2015 14:22
JavaScript Factory Inheritance
(function() {
'use strict';
// Create a factory function (a function that returns an object).
function aFactory() {
// Create a data store.
var _data = [];
// Add supplied arguments to the data store.
if (arguments.length)
@jabney
jabney / gen_fontawesome.py
Last active August 29, 2015 14:21
Python utility for converting Font Awesome cheatsheet to a JSON file
"""
Generate fontawesome icon set in JSON format from website cheatsheet.
http://fortawesome.github.io/Font-Awesome/cheatsheet/
Usage: python gen_fontawesome.py
Output: font-awesome.json
{icon_name: entity_reference, ...}
Author: James Abney
Date: 2015-05-21
@jabney
jabney / font-awesome.json
Created May 22, 2015 03:57
Font Awesome JSON Entity Reference
{"cc": "&#xf20a;", "bookmark": "&#xf02e;", "venus-mars": "&#xf228;", "arrow-circle-o-down": "&#xf01a;", "comment-o": "&#xf0e5;", "long-arrow-left": "&#xf177;", "arrow-right": "&#xf061;", "delicious": "&#xf1a5;", "chevron-circle-left": "&#xf137;", "bullhorn": "&#xf0a1;", "outdent": "&#xf03b;", "jpy": "&#xf157;", "drupal": "&#xf1a9;", "hdd-o": "&#xf0a0;", "hand-o-left": "&#xf0a5;", "pinterest": "&#xf0d2;", "plane": "&#xf072;", "question": "&#xf128;", "child": "&#xf1ae;", "circle-o": "&#xf10c;", "italic": "&#xf033;", "meanpath": "&#xf20c;", "subway": "&#xf239;", "google-plus": "&#xf0d5;", "angle-up": "&#xf106;", "star": "&#xf005;", "star-half-empty": "&#xf123;", "facebook-official": "&#xf230;", "youtube-square": "&#xf166;", "rss": "&#xf09e;", "toggle-off": "&#xf204;", "list-ol": "&#xf0cb;", "dot-circle-o": "&#xf192;", "copyright": "&#xf1f9;", "user": "&#xf007;", "key": "&#xf084;", "minus-square-o": "&#xf147;", "mobile": "&#xf10b;", "table": "&#xf0ce;", "columns": "&#xf0db;", "bolt": "&#xf0e7;", "fighter-jet": "&
@jabney
jabney / augment.js
Last active August 29, 2015 14:19
Javascript augmentation pattern for a factory method
// This augmentation style is appropriate for a factory-style function,
// which might be something that was imported as part of a library.
var factory = function factory() {
var data = [];
// Return an object with one or more methods.
return {
init: function() {
data.push.apply(data, arguments);
@jabney
jabney / containers.js
Last active August 29, 2015 14:13
Containers deque, bag, stack, queue, priority queue, and set
/*
containers.js
MIT License © 2015 James Abney
http://github.com/jabney
Containers: bag, stack, queue, double-ended queue (deque),
priorityQueue, and set.
See documentation at http://github.com/jabney/containers.js
*/
@jabney
jabney / graph.js
Last active August 29, 2015 14:12
Graph abstract data type and some associated algorithms
// graph.js © 2015 James Abney http://github.com/jabney
(function(ex, undefined) {
'use strict';
var gr = ex.graph || (ex.graph = Object.create(null));
// ---------------------------------------------------------------
// The graph abstract data type.
// ---------------------------------------------------------------
gr.graph = function graph(numVerts) {
@jabney
jabney / huffman.js
Last active August 29, 2015 14:11
A toy implementation of Huffman compression
// huffman.js © 2014 James Abney http://github.com/jabney
// Huffman coding was invented by David A. Huffman of MIT, 1952.
(function() {
'use strict';
// A toy implementation of Huffman compression. The result of
// encoding is a bit string. In this form it's not truly compressed,
// but the bit string represents the binary values after compression.
function huffman(str) {
var ft = frequencyTable(str), ht = huffTree(ft);