summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Andreas Benkard <code@mail.matthias.benkard.de>2011-06-21 21:51:56 +0200
committerMatthias Andreas Benkard <code@mail.matthias.benkard.de>2011-06-21 21:51:56 +0200
commitd6f1247749c9fb2993fb7d4662e7c9da620d8e41 (patch)
tree420462e79218656a7291fe011c690a4ac158ed59
parentabdb3028d8933be717a8861b02fbc2c28a747351 (diff)
Factor flip-flop-map out of classify-chunks.
-rwxr-xr-xjson-template.rkt25
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