diff options
author | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2011-06-21 21:51:56 +0200 |
---|---|---|
committer | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2011-06-21 21:51:56 +0200 |
commit | d6f1247749c9fb2993fb7d4662e7c9da620d8e41 (patch) | |
tree | 420462e79218656a7291fe011c690a4ac158ed59 | |
parent | abdb3028d8933be717a8861b02fbc2c28a747351 (diff) |
Factor flip-flop-map out of classify-chunks.
-rwxr-xr-x | json-template.rkt | 25 |
1 files changed, 15 insertions, 10 deletions
diff --git a/json-template.rkt b/json-template.rkt index 06f47eb..b95215b 100755 --- a/json-template.rkt +++ b/json-template.rkt @@ -12,17 +12,22 @@ (string-append "(" meta-left-re ")|(" meta-right-re ")"))]) (regexp-split re input))) +(define (flip-flop-map f1 f2 lst) + "Like map, but alternate between f1 and f2 as the function to apply." + (define (flip items) + (match items + ['() '()] + [(list* x xs) (cons (f1 x) (flop xs))])) + (define (flop items) + (match items + ['() '()] + [(list* x xs) (cons (f2 x) (flip xs))])) + (flip lst)) + (define (classify-chunks chunks format-char) - (define (classify-text-chunk xs) - (if (null? xs) - '() - (cons (car xs) (classify-directive-chunk (cdr xs))))) - (define (classify-directive-chunk xs) - (if (null? xs) - '() - (cons (parse-directive (car xs) format-char) - (classify-text-chunk (cdr xs))))) - (classify-text-chunk chunks)) + (flip-flop-map (λ (x) x) + (λ (x) (parse-directive x format-char)) + chunks)) (define (parse-directive directive format-char) (match directive |