Skip to content

Instantly share code, notes, and snippets.

View apmckinlay's full-sized avatar

Andrew McKinlay apmckinlay

View GitHub Profile
@apmckinlay
apmckinlay / charmatch1.go
Created April 19, 2014 18:55
second approach
type CharMatch struct {
fn func(rune) bool
}
func (cm CharMatch) Match(c rune) bool {
return cm.fn(c)
}
func Is(c rune) CharMatch {
return CharMatch{ func (c2 rune) bool { return c == c2 }}
@apmckinlay
apmckinlay / charmatch0.go
Created April 19, 2014 18:48
first approach
type CharMatch interface {
Match(c rune) bool
}
type is_cm struct {
c rune
}
func (cm is_cm) Match(c rune) bool {
return c == cm.c
// Package lex implements the lexical scanner for Suneido
package lex
import (
"bytes"
"strings"
"unicode"
"unicode/utf8"
)
@apmckinlay
apmckinlay / hamcrest.go
Last active August 29, 2015 13:58
A prototype of a Go implementation of Hamcrest style matchers
/*
hamcrest implements very basic hamcrest style asserts
for example:
func TestStuff(t *testing.T) {
Assert(t).That(2 * 4, Equals(6))
}
*/
package hamcrest
import "fmt"
// Package float10 implements decimal floating point numbers
// using uint64 to hold the coefficient.
package float10
import "strconv"
import "errors"
import "strings"
import "math"
// value is -1^sign * coef * 10^exp
max_matches: 30
GetMatches(prefix)
{
list = Suneido.LibLocate.list
prefix = prefix.Tr("_").Lower()
from = list.LowerBound(prefix)
to = Min(from + .max_matches, list.LowerBound(prefix.RightTrim() $ "~"))
matches = list[from .. to]
matches.Map!({ it.AfterFirst("=") })
matches.Sort!().Unique!()
getLibsNames(libs)
{
list = Object()
for (i = 0; i < libs.Size(); ++i)
{
lib = libs[i]
li = i.Pad(2)
for x in QueryList(lib $ " where group = -1", "name")
{
list.Add(x.Tr("_").Lower() $ "=" $ x $ ":" $ li)
@apmckinlay
apmckinlay / gist:4278101
Created December 13, 2012 17:23
An example of using CommaStringBuilder
CommaStringBuilder csb = new CommaStringBuilder("(");
for (Object x : list)
csb.add(fn(x));
return csb.append(")").toString();
@apmckinlay
apmckinlay / CommaStringBuilder
Created December 13, 2012 17:14
A utility class to build comma separated lists.
public class CommaStringBuilder implements Appendable {
private final StringBuilder sb;
private boolean first = true;
public CommaStringBuilder() {
sb = new StringBuilder();
}
public CommaStringBuilder(String s) {
sb = new StringBuilder(s);
@apmckinlay
apmckinlay / gist:4278026
Created December 13, 2012 17:10
One method for creating a comma separated list.
if (list.isEmpty())
return "";
for (Object x : list)
sb.append(",").append(fn(x));
return sb.substring(1);