Skip to content

Instantly share code, notes, and snippets.

View samth's full-sized avatar

Sam Tobin-Hochstadt samth

View GitHub Profile
@samth
samth / box-chaperone.rkt
Created August 30, 2012 19:56
Chaperones for abstract data types
#lang racket
(struct mbox ([v #:mutable]))
(define (box x) (mbox x))
(define (unbox x)
(cond [(ip? x)
(define imp? (vector-ref (ip-m x) 2))
(define ub (vector-ref (ip-m x) 0))
@samth
samth / define-inline.rkt
Created September 27, 2012 16:13
Auto-inlining
#lang racket
(require (for-syntax syntax/parse racket/syntax))
(provide define-inline)
(define-syntax (define-inline stx)
(syntax-parse stx
[(_ (i:id . args) (~optional j:id #:defaults ([j (format-id #'i "~a-inline" #'i)])) rhs)
#'(begin
@samth
samth / sp.rkt
Created October 3, 2012 17:17
syntax-parse is slow
cpu time: 2269 real time: 2270 gc time: 1416
cpu time: 416 real time: 417 gc time: 0
cpu time: 184 real time: 184 gc time: 0
#lang racket
(require syntax/parse/define)
(define-simple-macro (if (~seq test result) ... final)
(cond [test result] ... [else final]))
(if (even? 7) "seven" (odd? 8) "eight" "neither")
@samth
samth / password.rkt
Created October 15, 2012 15:10
Generating XKCD-style passwords in Racket
#lang racket
(define dict "/etc/dictionaries-common/words")
(define (choose l) (list-ref l (random (length l))))
(define words (for/list ([i (file->lines dict)]
#:when (regexp-match? "^[a-z]*$" i)
#:when (3 . < . (string-length i))
#:when (10 . > . (string-length i)))
i))
#lang racket/load
(module o racket
(define c% (class object% (init-field p) (define/public (m) (p 0)) (super-new)))
(provide c%))
(module m typed/racket
(require/typed 'o [c% (Class () ([p Any]) ([m (-> Any)]))])
(: f : String -> String)
#lang racket/load
(module m typed/racket
(define: v : Any (exn "" (current-continuation-marks)))
(provide v))
(module n racket
(require 'm)
v)
@samth
samth / pvar.rkt
Created November 1, 2012 14:29
Like `syntax`, but only accepts pattern variables
#lang racket
(require (for-syntax racket/private/sc))
(define-syntax (pvar stx)
(syntax-case stx ()
[(_ i)
(identifier? #'i)
(let ([v (syntax-local-value #'i (λ _ #f))])
(unless (syntax-pattern-variable? v)
@samth
samth / gist:3994115
Created November 1, 2012 14:55
cheating
#lang racket/load
(module m typed/racket
(: f : String -> String)
(define (f x) x)
(provide f))
(module n racket
(begin-for-syntax
(require typed-racket/utils/tc-utils)
@samth
samth / error.rktl
Created November 5, 2012 19:33
macro stepper error
`(
(start #f)
(visit
,(datum->syntax
#f
'(module anonymous-module racket
(#%module-begin
(require racket/place/distributed racket/place)
(provide hello-world)
(define (hello-world)