Last active
December 12, 2015 00:39
-
-
Save dyoo/4685464 to your computer and use it in GitHub Desktop.
for/string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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