Skip to content

Instantly share code, notes, and snippets.

#lang racket/base
;; Playing with input-port-append and transplant-input-port,
;; with the idea of adding the #lang line for arbitrary text
;; without screwing up the original input port's locations.
(provide prepend-lang-line)
(require racket/port)
@dyoo
dyoo / dir.rkt
Created March 28, 2013 21:34
A dir-like function that shows several functions for dynamic evaluation in Racket.
#lang racket
(provide dir)
(define ns (make-base-namespace))
;; Provides a "dir"-like function on a module.
(define (dir module-path)
(parameterize ([current-namespace ns])
(dynamic-require module-path (void)) ;; make sure _not_ to cause the module to evaluate.
@dyoo
dyoo / gist:5266913
Created March 28, 2013 21:20
More play with the dynamic evaluation tools
#lang racket
(define ns (make-base-namespace))
(define (play module-path)
(parameterize ([current-namespace ns])
(dynamic-require module-path #f)
(define-values (module-variable-exports module-syntax-exports)
(module->exports module-path))
@dyoo
dyoo / huffman.rkt
Last active December 15, 2015 09:58
another version of the huffman code that uses bit-vectors
#lang racket
(require data/heap
data/bit-vector)
;; A node is either an interior, or a leaf.
;; In either case, they record an item with an associated frequency.
(struct node (freq) #:transparent)
(struct interior node (left right) #:transparent)
(struct leaf node (val) #:transparent)
#lang racket
(syntax-case #'(this is a vector: #(3 4 5)) ()
[(a b c d #(x y z))
(list #'x #'y #'z)])
@dyoo
dyoo / gist:5201722
Last active December 15, 2015 04:29
#lang racket/base
(require racket/port)
(require racket/tcp)
(define (start-irc #:irc-hostname [irc-hostname "chat.freenode.net"]
#:irc-portno [irc-portno 6667]
#:irc-nick [irc-nick "nyuracket"]
#:irc-user [irc-user "racket"]
#:irc-realname [irc-realname "nyuracket"])
@dyoo
dyoo / gist:5199615
Created March 19, 2013 20:08
simple repl example
#lang racket
(define ns (make-base-namespace))
(parameterize ([current-namespace ns])
(read-eval-print-loop))
@dyoo
dyoo / example-lang.rkt
Created March 18, 2013 20:36
An example of filtering out definitions in a language
#lang racket
(require racket/provide
(prefix-in racket: racket))
(define count 'count)
(provide (except-out
(filtered-out (lambda (name)
(and (regexp-match? #rx"^racket:" name)
(regexp-replace #rx"^racket:" name "")))
(all-from-out racket))
racket:count)
#lang racket
(define (my-expand-path-with-env a-path-string)
(define (lookup var)
(getenv (substring var 1)))
(regexp-replace* #px"\\$\\w+" a-path-string lookup))
(my-expand-path-with-env "$HOME/foo/bar")
@dyoo
dyoo / gist:5130815
Created March 10, 2013 22:37
date string parsing example for rich1
#lang racket
(define (parse-date-string str)
(match str
[(pregexp #px"^(\\d{4})-(\\d{2})-(\\d{2})$" (list _ year month day))
(list (string->number year)
(string->number month)
(string->number day))]))
(parse-date-string "2013-03-10")