Skip to content

Instantly share code, notes, and snippets.

@gwatt
gwatt / sqlscan.go
Last active August 29, 2015 14:23
Easy Struct scanning from SQL.
import (
"database/sql"
"reflect"
)
/*
* You can pass in an unaltered pointer to sql.Row, sql.Rows as a "RowScanner"
*/
type RowScanner interface {
Scan(dest ...interface{}) error
@gwatt
gwatt / wait.go
Created January 22, 2016 15:42
wait for all channels to receive a value
package wait
import (
"fmt"
"reflect"
)
type ReadChan struct {
Var interface{}
Chan interface{}
#!/bin/ksh
set -eu
typeset -A tracked
export SUBDIRECTORY_OK=1 NONGIT_OK=0 OPTIONS_SPEC='' LONG_USAGE='' USAGE='' GIT_TEXTDOMAINDIR='' GIT_GETTEXT_POISON=''
export GIT_TEST_GETTEXT_POISON=''
source "$(git --exec-path)/git-sh-setup"
@gwatt
gwatt / curry.scm
Last active February 14, 2017 00:56
define-curried/lambda-curried for r6rs scheme
(library (curry)
(export define-curried lambda-curried)
(import (rnrs))
(define-syntax lambda-curried
(lambda (x)
(syntax-case x ()
[(_ () b b* ...) #'(lambda () b b* ...)]
[(_ arg* b b* ...) (identifier? #'arg*) #'(lambda arg* b b* ...)]
[(_ (arg* ...) b b* ...)
(library (html)
(export html display-html)
(import (rnrs))
(define short-tags (make-enumeration '(br img link meta)))
(define (short-tag? t)
(enum-set-member? t short-tags))
(define (->string obj)
@gwatt
gwatt / param-args.ss
Last active February 10, 2018 17:51
Store command line arguments in parameters
(define-syntax param-args
(syntax-rules ()
[(_ arg-list (opt param) ...)
(let loop ([args arg-list])
(if (null? args)
'()
(case (car args)
[(opt) (if (null? (cdr args))
(errorf 'param-args "Missing required argument for ~a" opt))
(param (cadr args))
@gwatt
gwatt / fn-examples.ss
Last active March 1, 2018 21:57
allows defining functions with constraints on arguments as well as default values
(define iota
(fn ((count :: integer? exact? nonnegative?)
(start :: number? = 0)
(step :: number? = 1))
(if (zero? count)
'()
(cons start
(iota (- count 1) (+ start step) step)))))
(define member
@gwatt
gwatt / future.ss
Last active March 21, 2018 21:05
awaitable futures for ChezScheme
#!chezscheme
(library (future)
(export spawn sync let-futures)
(import (chezscheme))
(define-record-type future
(fields (immutable lock)
(mutable completion)
(mutable result))
(library (documentation)
(export define/document define-syntax/document
document describe)
(import (rnrs))
(define doc-list '())
(define (document thing description)
(set! doc-list (cons (cons thing (syntax->datum description)) doc-list)))
@gwatt
gwatt / let+.ss
Last active February 23, 2019 16:22
Extended let
;;;
;;; This acts like a letrec*, with the ability to bind multiple values and destructure lists and vectors
;;;
;;; usage:
;;;
;;; (let+ ((a 1)
;;; (b (+ a 1))
;;; ((values c d) (values (+ a b) (* a b)))
;;; ((vector e f g h) (vector a b c d))