Skip to content

Instantly share code, notes, and snippets.

View apmckinlay's full-sized avatar

Andrew McKinlay apmckinlay

View GitHub Profile
@apmckinlay
apmckinlay / killer.go
Last active September 20, 2023 19:13
for {
time.Sleep(interval)
if ws.killClock.Add(1) > createDelay && nworkers.Load() > minWorkers {
ws.ch <- task{} // send poison pill to one worker
}
}
@apmckinlay
apmckinlay / worker.go
Last active September 20, 2023 17:43
timer := time.NewTimer(timeout)
for {
select {
case task = <-ws.ch:
if !timer.Stop() {
<-timer.C
}
timer.Reset(timeout)
run(task)
case <-timer.C:
@apmckinlay
apmckinlay / hmap.go
Last active February 13, 2023 17:56
type Hmap[K any, V any, H helper[K]] struct {
help H
// ...
}
type helper[K any] interface {
Hash(k K) uint64
Equal(x, y K) bool
}
@apmckinlay
apmckinlay / small_test.go
Created March 6, 2019 17:09
testing Go small structs and interfaces
package runtime
import "testing"
type intfc interface {
f() intfc
}
type byval struct {
x int
@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.
@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 / 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 / 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>
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 / 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
)