Skip to content

Instantly share code, notes, and snippets.

View apmckinlay's full-sized avatar

Andrew McKinlay apmckinlay

View GitHub Profile
@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 lex implements the lexical scanner for Suneido
package lex
import (
"bytes"
"strings"
"unicode"
"unicode/utf8"
)
@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
@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 / charmatch2.go
Last active August 29, 2015 14:00
third version
type CharMatch func(rune) bool
// Predefined CharMatch's
var (
SPACE CharMatch = AnyOf(" \t\r\n")
DIGIT CharMatch = InRange('0', '9')
LETTER CharMatch = unicode.IsLetter
LOWER CharMatch = unicode.IsLower
UPPER CharMatch = unicode.IsUpper
)
func TestIs(t *testing.T) {
cm := Is('x')
Assert(t).That(cm.Match('x'), Equals(true))
Assert(t).That(cm.Match('y'), Equals(false))
}
func TestAnyOf(t *testing.T) {
cm := AnyOf("abc")
Assert(t).That(cm.Match('b'), Equals(true))
Assert(t).That(cm.Match('x'), Equals(false))
@apmckinlay
apmckinlay / simpleCodeMirror.html
Last active August 29, 2015 14:22
Simplest possible CodeMirror example
<!doctype html>
<html>
<head>
<script src="http://codemirror.net/lib/codemirror.js"></script>
<link rel="stylesheet" href="http://codemirror.net/lib/codemirror.css">
<script type="text/javascript">
window.onload = function () {
CodeMirror(document.body);
};
</script>
@apmckinlay
apmckinlay / Curry
Last active November 23, 2017 18:38
Suneido Curry function
function (@args)
{
helper = class
{
New(.args)
{ }
Call(@args2)
{
args = .args
if not args2.Empty?()
@apmckinlay
apmckinlay / Bind
Last active November 23, 2017 18:37
Suneido Bind function
class
{
CallClass(@args)
{
ai = 0
fi = 1
mixed = args[1..].Map({ it is Bind ? 'a' $ ai++ : '.f' $ fi++ }).Join(',')
c = (.binder)(mixed)
return c(@args.Remove(Bind))
}
@apmckinlay
apmckinlay / htbl.h
Created January 7, 2018 23:03
hash table implementation
#pragma once
/*
* Hmap and Hset are hash based maps and sets
* implemented with: prime table sizes, open addressing, linear probing,
* robin hood hashing, and resize on probe limit.
* Htbl is the common code, it is not intended for external use.
* To avoid padding it uses an array of blocks each with BN (4) entries.
* But we still treat it as a single flat array.
* Within blocks keys, values, and distances are separate arrays to avoid padding.
* Uses uint16 for size so limited to 64k slots or about 48k elements.