Last active
December 21, 2015 17:08
-
-
Save NalaGinrut/6338046 to your computer and use it in GitHub Desktop.
a Python3 style string-template for Scheme
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
(define *stpl-SRE* '(or (=> dollar "$$") | |
(: "${" (=> name (+ (~ #\}))) "}"))) | |
(define (make-string-template str . opts) | |
(define ll '()) | |
(define lv '()) | |
(define template | |
(irregex-replace/all | |
;;"(\\$\\{([^$])+\\})" | |
*stpl-SRE* str | |
(lambda (m) | |
(if (irregex-match-substring m 'dollar) | |
"$" | |
(let* ((key (symbol->keyword (string->symbol | |
(irregex-match-substring m 'name)))) | |
(v (kw-arg-ref opts key))) | |
(and v (set! lv (cons (cons key v) lv))) ; default value | |
(set! ll (cons key ll)) | |
"~a"))))) | |
(lambda args | |
(let ((vals (map (lambda (x) (or (kw-arg-ref args x) (assoc-ref lv x) "NONE")) ll))) | |
(format #f "~?" template (reverse vals))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
and for default value