Skip to content

Instantly share code, notes, and snippets.

View danking's full-sized avatar

Dan King danking

View GitHub Profile
@danking
danking / gist:7827976
Last active December 30, 2015 12:19
My Typed Racket Fantasy
(: some-function : (All ([T = (TerseType A B C D)])
((State T)
A
B
->
(State T))))
(: some-other-function : (All (T)
(State T)
->
(struct: (state StackPeek InputPeek Stack Input)
([name : String]
[description : String]
[id : Natural]
[transition-table : [TT StackPeek InputPeek Stack Input]]
[predecessors : [Setof (state StackPeek InputPeek Stack Input)]]))
(define-type [State StackPeek InputPeek Stack Input]
(state StackPeek InputPeek Stack Input))
#lang racket
(define-syntax custodian-protect
(syntax-rules ()
((custodian-protect main-body ...)
(let ((protection #f)
(original #f))
(dynamic-wind (lambda ()
(set! original (current-custodian))
(set! protection (make-custodian original))
(current-custodian protection))
#lang typed/racket
(require math)
(define-type w-space [Vector Real
Real Real Real
Real Real Real
Real Real Real])
(: ten-dim-normal-pdf : (case-> (w-space -> Flonum)
(: scale-vector ((Vectorof Natural) -> (Vector of Natural)))
(define (scale-vector vec k)
  (for/vector: : [Vectorof Natural] ([element vec])
     (* k vec)))

But does it really matter that I'm using Naturals? I could replace Natural with any other numeric type that is closed under multiplication (Integer, Rational, Real, Complex) and this should still type check.

(In an earlier revision I said addition)

(: normalize-histogram ((Vectorof Real) -> (Vectorof Real)))
(define (normalize-histogram hist)
  (let: ((sum (for/sum: : Real ([value : Real (in-vector hist)])
                value)))
    (for/vector: : (Vectorof Real) ([value : Real (in-vector hist)])
      (/ value sum))))

I should be able to say ((Vectorof Natural) -> (Vectorof Rational))

@danking
danking / gist:6153980
Last active December 20, 2015 15:28
Define values which ignores excess values
#lang racket
(define-syntax (my-define-values stx)
(syntax-case stx ()
((_ (ids ...) expression)
(with-syntax (((indices ...)
(datum->syntax stx
(build-list (length
(syntax->list #'(ids ...)))
@danking
danking / gist:5844659
Last active December 18, 2015 20:59
Beijing Air Quality Formatter for xmobar
#!/bin/zsh
AQI=$(curl -s http://iphone.bjair.info/m/beijing/mobile \
| grep -E '<h1>[0-9]+</h1>' \
| sed 's:.*<h1>\([0-9]*\)</h1>.*:\1:')
if [[ $AQI -lt 50 ]]; then
echo -n "<fc=green>"
elif [[ $AQI -lt 100 ]]; then
echo -n "<fc=yellow>"
#lang racket
;; Report each unique line from stdin
(let ([saw (make-hash)])
(for ([line (in-lines)])
(unless (hash-ref saw line #f)
(displayln line))
(hash-set! saw line #t)))
;; do it purely.
(for/fold ([seen (set)])
danking@beverly # drracket
set-loaded-mask method in bitmap%: contract violation
expected: (is-a?/c bitmap%)
given: #f
in: the 1st argument of
the set-loaded-mask method in
bitmap%/c
contract from: <collects>/racket/draw.rkt
blaming:
<collects>/drracket/private/frame-icon.rkt