Skip to content

Instantly share code, notes, and snippets.

@jtacoma
jtacoma / golang_interface_extension.go
Created September 26, 2012 03:41
Adding Methods to an Interface in Go
// This package demonstrates a technique for (very nearly) adding methods
// to an interface. The code works and runs as is.
package main
import "fmt"
// A simple custom type
type Cell int
// CellSetMinimal is a (very) minimal interface to be easily implemented
@jtacoma
jtacoma / bootstrap.js
Created December 4, 2011 03:31
Literate programming with HTML source files using Node and jQuery.
var $ = require('jquery');
function chunkify(rawHtml, chunks) {
var woven = $(rawHtml).find("code[title!='']");
for (var i = 0; i < woven.length; ++i) {
var element = $(woven[i]);
var content = element.text();
var name = element.attr("title");
if (chunks[name])
chunks[name] = chunks[name].concat(content);
@jtacoma
jtacoma / usage
Created November 16, 2011 03:43
Some static site generation ideas based on jekyll, blosxom, and something from 1997 I forget the name of, that did all kinds of crazy stuff.
$ cat <<EOF > sample.txt
> title: Sample!
>
> = {{ title }} =
> EOF
$ yaml-assemble sample.txt | mustache | creole2html
<h1>Sample!</h1>
@jtacoma
jtacoma / html2json.html
Created February 5, 2011 00:36
Converting HTML to JSON with jQuery. Just a gist...
<dl>
<dt>selector</dt>
<dd>dl</dd>
<dt>apply</dt>
<dd>
<ul>
<li>
<dl>
<dt>selector</dt>
<dd>&gt; dt</dd>
@jtacoma
jtacoma / spec2class.js
Created February 4, 2011 22:09
One way to translate a spec(ification), that is a plain old object, into a proper class. The resulting class is to be instantiated from an object with a fixed set of properties (e.g. a table row) and to calculate other properties based on those received.
// Given a prototype-like object, create a class based on that object's
// properties.
function buildClass(spec) {
// Throw an exception of the argument is inappropriate:
if (typeof(spec) != 'object' || spec === null)
throw 'expected an object.';
var empty = true;
for (var name in spec) { empty = false; break; }
if (empty)
throw 'expected a non-empty object.';