Skip to content

Instantly share code, notes, and snippets.

@samth
Created June 18, 2013 13:40
Show Gist options
  • Save samth/5805444 to your computer and use it in GitHub Desktop.
Save samth/5805444 to your computer and use it in GitHub Desktop.
#lang racket
(require (prefix-in r: racket))
(define-match-expander seq
(λ (stx)
(syntax-case stx ()
[(_ p ...)
#'(app sequence->list (list p ...))])))
(define-match-expander dict
(λ (stx)
(syntax-case stx ()
[(_ #:only [pk pv] ...)
#'(app dict->list (list (cons pk pv) ...))]
[(_ [pk pv] ...)
#'(app dict->list (list-no-order (cons pk pv) ... rst (... ...)))])))
(match (list (cons 1 2) (cons 2 3))
[(dict [1 2] [2 4]) 'a]
[(dict #:only [1 2]) 'b]
[(dict #:only [1 2] [2 3]) 'c])
(match (hash 1 2 2 3)
[(dict [1 2] [2 4]) 'a]
[(dict #:only [1 2]) 'b]
[(dict #:only [1 2] [2 3]) 'c])
(match (vector 7 2 4)
[(dict [1 2] [2 4]) 'a]
[(dict #:only [1 2]) 'b]
[(dict #:only [1 2] [2 3]) 'c])
(match (list 1 2 3)
[(seq 1 2 3) 'a])
(match (vector 1 2 3)
[(seq 1 2 3) 'a])
(match (stream 1 2 3)
[(seq 1 2 3) 'a])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment