Skip to content

Instantly share code, notes, and snippets.

@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.';
@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 / 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 / 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 / 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 / echo.go
Last active December 12, 2015 04:09
A sample gozdcf-based application.
// Though it's not necessary, you might want to run this with environment
// variable GOMAXPROCS set to the number of CPUs on your machine.
//
// For example, at a bash prompt:
//
// $ GOMAXPROCS=4 go run echo.go
//
package main
import (
@jtacoma
jtacoma / .gitignore
Last active December 13, 2015 20:18
gozmqgen: some scripts for generating ZMQ-related Go code.
.cache
getsockopt-*.go
setsockopt-*.go
socket*.go

Changing the Public API

When I started looking for ways to help on the "github.com/alecthomas/gozmq" package a few months ago, a recurring topic in discussions was that an earlier decision to provide a hidden struct type through a public interface type had become restrictive. We couldn't just drop the interface and make the struct public because that would be backwards-incompatible. Or could we?

No

This had to be the first considered option, and was the standing decision at the time. If we could continue providing what the package aims to provide without breaking backwards compatibility, then by all means, we should not break it.

Unfortunately this meant we couldn't provide all that we aimed to provide. Or could we?

package main
import (
"log"
"time"
zmq "github.com/alecthomas/gozmq"
)
func main() {
@jtacoma
jtacoma / bar.go
Last active December 19, 2015 19:19
foo to bar
// Package bar provides thneeds (things everyone needs).
//
// Those switching from "example.com/pkg/foo" to bar will have to run the
// following commands (which are intended to be idempotent).
//
// go fmt -r 'a.ChopTruffulaTree(b) -> a.ChopTruffulaTrees(b, b, b, b, b)'
//
// Of course, if you're using sufficiently smart tools, your code may already
// be updated.
//