Skip to content

Instantly share code, notes, and snippets.

View samth's full-sized avatar

Sam Tobin-Hochstadt samth

View GitHub Profile
#lang racket
(module m typed/racket
;; the generated contract for this is
;;
;; (define-values:102 (generated-contract4:103)
;; (#%app:104
;; continuation-mark-key/c/proc:105
;; (#%app:106 flat-named-contract 'Integer exact-integer?)))
;;
@samth
samth / filters1.rkt
Created November 6, 2012 16:49
Dan Burton's bugs
#lang typed/racket
;; Typed Racket has some neat tricks
;; that allow it to type Racket programs.
;; One important trick is that it can pass "filter" information around
;; in branches of a cond expression,
;; as well as a few other expressions like cond.
;; Filters tell you additional type information about a value
module Post {
import Comment from Comment;
import Model from DS;
export get Post() { return _Post; }
class _Post extends Model {}
_Post.schema((post) => {
post.attr('title');
@samth
samth / try2.js
Created November 18, 2012 19:07 — forked from wycats/gist:4106771
module Post {
import Comment from Comment;
import Model from DS;
class Post extends Model {}
Post.schema((post) => {
post.attr('title');
post.attr('body');
post.hasMany('comments', (args...) => Comment(args...));
@samth
samth / test
Created December 3, 2012 22:21 — forked from takikawa/test
. Type Checker: untyped identifier apply-contract imported from module <private/base.rkt> in: #%module-begin
. Type Checker: untyped identifier coerce-contract imported from module <private/guts.rkt> in: #%module-begin
. Type Checker: untyped identifier flat-named-contract imported from module <racket/contract> in: #%module-begin
. Type Checker: untyped identifier build-source-location imported from module <syntax/srcloc> in: #%module-begin
. Type Checker: Summary: 4 errors encountered in:
#%module-begin
#%module-begin
#%module-begin
#%module-begin
@samth
samth / stocking.rkt
Last active December 10, 2015 09:48
Stocking draws
#lang racket
(define people '(liz steve aj marti david ann pat annie mae helen jane carl
sam katie will lindsey
))
(define last-year '((sam aj) (katie will) (liz jane) (aj liz) (steve marti) (pat steve) (mae katie)
(ann annie) (helen mae) (carl helen) (jane david) (annie carl)
(will sam) (david pat) (marti ann)))
#lang racket
(require compiler/cm mzlib/compile unstable/logging)
(manager-compile-notify-handler displayln)
(define prefix "/home/samth/sw/plt/collects/tests/typed-racket/succeed/")
(define (del file)
@samth
samth / static-struct-info.rkt
Created January 23, 2013 22:48
The magic of static struct information.
#lang racket
(define-syntax m
(list #f #'cons #'pair? (list #'car #'cdr) (list #f #f) #t))
(match (cons 1 2)
[(m a b) (+ a b)])
;; => 3
#lang racket
(define (find-duplicates elts)
(define ht (make-hash))
(for/list ([x elts]
#:when (hash-update! ht x add1 0)
#:when (= 2 (hash-ref ht x)))
x))
(find-duplicates '(1 2 2 3 1 4 5 4 4 4))
@samth
samth / inf.rkt
Last active December 14, 2015 00:08
a pattern to optimize
#lang racket/base
(for ([i (in-range 5 +inf.0 2)]) (displayln i))