From d6f1247749c9fb2993fb7d4662e7c9da620d8e41 Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Tue, 21 Jun 2011 21:51:56 +0200 Subject: Factor flip-flop-map out of classify-chunks. --- json-template.rkt | 25 +++++++++++++++---------- 1 file 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 -- cgit v1.2.3