From b6417d2d07fa83935c8e30c2cd78c5a8c8556180 Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Sun, 23 Jan 2022 15:10:58 +0100 Subject: [PATCH] utils: Type handle-org-booleans. --- utils.rkt | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/utils.rkt b/utils.rkt index 7e3e87e..2221b46 100644 --- a/utils.rkt +++ b/utils.rkt @@ -15,7 +15,8 @@ (for-syntax syntax/parse racket/list)) (provide eval-with eval1-with auto-hash-ref/explicit auto-hash-ref/: - extract-symbols any->string stringify-variable-mapping string->any) + extract-symbols any->string stringify-variable-mapping string->any + handle-org-booleans) (define-type Variable Symbol) (define-type (VariableMapping A) (Immutable-HashTable Variable A)) @@ -150,6 +151,27 @@ (test-case "string->any" (check-equal? (string->any "(or b (not a))") '(or b (not a))) (check-equal? (string->any "14") 14))) + + ;;; Given a sexp, converts all "#f" to #f and "#t" to #t. + ;;; + ;;; When I read Org-mode tables, I pump them through a call to the + ;;; prin1 because the Elisp sexps seems incompatible with Racket. + ;;; On the other hand, Racket Booleans seem to upset Elisp a little, + ;;; so prin1 wraps them in additional double quotes. This function + ;;; removes those quotes. + (: handle-org-booleans (-> Any Any)) + (define/match (handle-org-booleans datum) + [("#t") #t] + [("#f") #f] + [((? list?)) (map handle-org-booleans datum)] + [ (_) datum]) + + (module+ test + (test-case "handle-org-booleans" + (check-equal? (handle-org-booleans "#t") #t) + (check-equal? (handle-org-booleans "#f") #f) + (check-equal? (handle-org-booleans '("#t" "#f")) '(#t #f)) + (check-equal? (handle-org-booleans "t") "t"))) ) (require 'typed) @@ -210,19 +232,6 @@ ;;; A string variable mapping is a mapping from variables to strings. (define (string-variable-mapping? dict) (hash/c symbol? string?)) -;;; Given a sexp, converts all "#f" to #f and "#t" to #t. -;;; -;;; When I read Org-mode tables, I pump them through a call to the -;;; prin1 because the elisp sexp seems incompatible with Racket. On -;;; the other hand, Racket Booleans seem to upset elisp a little, so -;;; prin1 wraps them in additional double quotes. This function -;;; removes those quotes. -(define/match (handle-org-booleans datum) - [("#t") #t] - [("#f") #f] - [((? list?)) (map handle-org-booleans datum)] - [ (_) datum]) - ;;; Given a sexp, applies the given function to any object which is ;;; not a list. ;;;