Skip to content

Instantly share code, notes, and snippets.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdint.h>
#include "udpx.pb-c.h"
@chrisdew
chrisdew / State.java
Created July 24, 2013 08:05
enum state machine
interface IState {
public void GuiBookOn() throws InvalidStateException;
public void GuiBookOff() throws InvalidStateException;
public void NetBookedOn() throws InvalidStateException;
public void NetBookedOff() throws InvalidStateException;
}
public enum State implements IState {
BOOKED_OFF {
@Override
it('should provide a many to one relation', function() {
var ic = util.ManyToOne('imei', 'callsign');
ic.add('345123123123', 'ROMEO1');
ic.add('345456456456', 'DELTA2');
ic.add('345678678678', 'ROMEO1');
assert.equal(ic.imei_to_callsign['345123123123'], 'ROMEO1');
assert.equal(ic.imei_to_callsign['345456456456'], 'DELTA2');
assert.equal(ic.imei_to_callsign['345678678678'], 'ROMEO1');
assert.equal(ic.callsign_to_imeis['ROMEO1'], ['345123123123', '345678678678']);
assert.equal(ic.callsign_to_imeis['DELTA2'], ['345678678678']);
// A task on a task list (and also possibly on an outstanding_task_list.
struct task {
int priority;
struct list_node task_list;
const char *task_name;
struct list_node outstanding_task_list;
};
void list() {
@chrisdew
chrisdew / multimethod-adventure.clj
Created October 18, 2012 12:00
Adventures with multimethods.
(def class-keyword-map (atom {}))
(defn class-keyword
[instance]
(@class-keyword-map (class instance)))
(defmacro defadrec
[klass & body]
`(do
; create the record as normal
(defrecord ~klass ~@body)
; and add the type to the keyword lookup
@chrisdew
chrisdew / Main.hs
Created June 15, 2012 15:38
A working client to server sending of a message in protobuf format.
module Main where
import Network.Socket hiding (send, sendTo, recv, recvFrom)
import Network.Socket.ByteString
import qualified Data.ByteString.Lazy as LBS
import qualified Data.ByteString as BS
import Text.ProtocolBuffers.WireMessage (messageGet)
import AddressBookProtos.Person
@chrisdew
chrisdew / cow.c
Created June 13, 2012 14:28
attempt at copy-on-write, fails with "bus error"
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define SIZE 4096
int main(void) {
@chrisdew
chrisdew / flashOnChange.js
Created November 7, 2011 17:07
flashOnChange
var flashTime = new Date();
ko.bindingHandlers.flashOnChange = {
update: function (element, valueAccessor) {
// If the page has just loaded, don't flash all the data.
if (new Date().getTime() < flashTime.getTime() + 1000) return;
// Whenever the value subsequently changes, flash it
var value = valueAccessor();
$(element).addClass('new');
setTimeout(function() { $(element).removeClass('new'); }, 2000);
}
@chrisdew
chrisdew / example.html
Created June 16, 2011 08:43 — forked from joelnet/example.html
Unobtrusive Knockout support library for jQuery
Choose a ticket class: <select id="tickets"></select>
<p id="ticketOutput"></p>
<script id="ticketTemplate" type="text/x-jquery-tmpl">
{{if chosenTicket}}
You have chosen <b>${ chosenTicket().name }</b>
($${ chosenTicket().price })
<button data-bind="click: resetTicket">Clear</button>
{{/if}}
@chrisdew
chrisdew / gist:1018787
Created June 10, 2011 13:00
hacky KnockoutJS validator - do not use
// This is a hacked-together form validation KnockoutJS plugin.
// The formatting is a bit messed up in places too.
// TODO: Refactor to allow more than one form on a page!
console.log("baz");
(function() {
var handlers = { required