Skip to content

Instantly share code, notes, and snippets.

@dyoo
Last active December 12, 2015 00:39
Show Gist options
  • Save dyoo/4685464 to your computer and use it in GitHub Desktop.
Save dyoo/4685464 to your computer and use it in GitHub Desktop.
for/string
#lang racket
(define-syntax (for/string stx)
(syntax-case stx ()
[(_ clauses . defs+exprs)
(with-syntax ([original stx])
(syntax/loc stx
(apply string-append
(reverse
(for/fold/derived original
([string-chunks '()])
clauses
(define char-or-string
(let () . defs+exprs))
(unless (or (char? char-or-string)
(string? char-or-string))
(raise-argument-error 'for/string "character or string"
char-or-string))
(cons (if (char? char-or-string)
(string char-or-string)
char-or-string)
string-chunks))))))]))
(module+ test
(require rackunit)
(define subst (hash "hello" "goodbye" "sunlight" "moonlight"))
(check-equal? (for/string ([x '("hello" "bright" "sunlight")])
(hash-ref subst x x))
"goodbyebrightmoonlight")
(check-equal? (for/string ([x "hello world"])
(char-upcase x))
"HELLO WORLD"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment