From 502e564fc74969cb2943a939f400ebca0bb29c6e Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Tue, 14 Apr 2020 15:57:26 +0200 Subject: [PATCH] Add map-sexp. --- utils-tests.rkt | 1 + utils.rkt | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/utils-tests.rkt b/utils-tests.rkt index 7fc79fc..dae0132 100644 --- a/utils-tests.rkt +++ b/utils-tests.rkt @@ -43,6 +43,7 @@ (check-equal? (hash-ref mp 'b) "(not b)")) (check-equal? (string->any "(or b (not a))") '(or b (not a))) (check-equal? (string->any "14") 14) + (check-equal? (map-sexp add1 '(1 2 (4 10) 3)) '(2 3 (5 11) 4)) (check-equal? (read-org-sexp "((\"a\" \"(and a b)\") (\"b\" \"(or b (not a))\"))") '(("a" "(and a b)") ("b" "(or b (not a))"))) (check-equal? (read-org-sexp "(#t \"#t\" \"#t \" '(1 2 \"#f\"))") diff --git a/utils.rkt b/utils.rkt index ef9ddae..3c27d61 100644 --- a/utils.rkt +++ b/utils.rkt @@ -16,6 +16,7 @@ [stringify-variable-mapping (-> variable-mapping? string-variable-mapping?)] [string->any (-> string? any/c)] [read-org-sexp (-> string? (listof any/c))] + [map-sexp (-> procedure? any/c any/c)] [unorg (-> string? (listof any/c))] [unstringify-pairs (-> (listof (general-pair/c string? any/c)) (listof (general-pair/c symbol? any/c)))] @@ -210,6 +211,17 @@ [((? list?)) (map handle-org-booleans datum)] [ (_) datum]) +;;; Given a sexp, applies the given function to any object which is +;;; not a list. +;;; +;;; The contract of this function will not check whether func is +;;; indeed applicable to every non-list element of the sexp. If this +;;; is not the case, a contract violation for func will be generated. +(define (map-sexp func sexp) + (match sexp + [(? list?) (map ((curry map-sexp) func) sexp)] + [datum (func datum)])) + ;;; Reads a sexp from a string produced by Org-mode for a named table. ;;; See example.org for examples. (define read-org-sexp (compose handle-org-booleans string->any))