Skip to content

Instantly share code, notes, and snippets.

View bradclawsie's full-sized avatar

Brad Clawsie bradclawsie

View GitHub Profile
@bradclawsie
bradclawsie / testlock.go
Created March 4, 2013 06:54
a minimal program that obtains a golock lock. compile to "testlock" with go build testlock.go
package main
import (
"os"
"fmt"
"time"
"github.com/bradclawsie/golock"
)
func main() {
@bradclawsie
bradclawsie / run-exclusive.sh
Created March 4, 2013 07:00
a wrapper to handle graceful lock breaking (when needed) of golock programs
#!/bin/zsh
# call like:
# $ run-exclusive.sh /path/of/progname /path/of/lockfile
# e.g.
# $ run-exclusive.sh ./testlock t.lock
# clearly you want progname to be something that will only match once.
# don't use a short string that is a substring match of something else running
export PROGNAME=$1
@bradclawsie
bradclawsie / sample-run-exclusive.txt
Created March 4, 2013 07:02
how to invoke run-exclusive.sh
$ run-exclusive.sh ./testlock t.lock
;; blog.jazzychad.net/2012/08/01/array-iteration-problem.html
(use srfi-1)
(: f ((list-of number) number number -> (list-of number)))
(define f
(lambda (arr n i)
(cond [(< n 0)
;; if we are going right-to-left, reverse the list and use the abs count
(reverse (f (reverse arr) (abs n) i))]
@bradclawsie
bradclawsie / naive_struct.go
Created June 5, 2013 05:22
naive attempt at matching json
type Foo struct {
MandatoryString string
OptionalUInt64 uint64
OptionalString string
}
@bradclawsie
bradclawsie / accepted.json
Created June 5, 2013 05:26
a json structure that accepts some data as optional
{ "MandatoryString":"somestring", "OptionalUInt64":1, "OptionalString":"anotherstring" }
{ "MandatoryString":"somestring", "OptionalUInt64":1, "OptionalString":null }
{ "MandatoryString":"somestring", "OptionalUInt64":null, "OptionalString":"anotherstring" }
{ "MandatoryString":"somestring", "OptionalUInt64":null, "OptionalString":null }
{ "MandatoryString":"somestring", "OptionalUInt64":1 }
@bradclawsie
bradclawsie / nullables.go
Created June 5, 2013 05:37
some "nullable" base types for go
type NullableString string
func (n NullableString) MarshalJSON() ([]byte, error) {
sn := string(n)
if sn == "" {
var i interface{}
return json.Marshal(i)
}
return json.Marshal(sn)
}
@bradclawsie
bradclawsie / better.go
Created June 5, 2013 05:41
this type can be serialized to json so that uninitialized optional fields are null
type Foo struct {
MandatoryString string
OptionalUInt64 NullableUInt64
OptionalString NullableString
}
@bradclawsie
bradclawsie / honeycomb.java
Created December 4, 2013 06:31
a java solution to the "honeycomb" puzzle
package org.b7j0c.puzzles;
import java.util.EnumMap;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
// the Hex class represents an id with six neighbors. the orientation of the neighbors is irrelevant
// but it is useful to describe them with compass directions as such:
package org.b7j0c.puzzles;
// http://programmingpraxis.com/2013/11/15/twitter-puddle/
public final class Puddles {
public static void main(String[] args) {
int[] c = {2,5,1,2,3,4,7,7,6,1,1,9,8,8,9,1,1};
int cl = c.length;
int w = -1;
int sub = 0;