diff --git a/scribblings/utils.scrbl b/scribblings/utils.scrbl index af46df0..eb4978c 100644 --- a/scribblings/utils.scrbl +++ b/scribblings/utils.scrbl @@ -2,7 +2,7 @@ @(require scribble/example racket/sandbox (for-label racket graph (submod "../utils.rkt" typed) (only-in typed/racket/base - Any AnyValues))) + Any AnyValues Listof Symbol))) @title[#:tag "utils"]{dds/utils: Various Utilities} @@ -116,6 +116,17 @@ Note that only one expression can be supplied in the body. @section{Analysis of quoted expressions} +@defproc[(extract-symbols [form Any]) (Listof Symbol)]{ + +Produces a list of symbols appearing in the quoted expression +passed in the first argument. + +@examples[#:eval utils-evaluator +(extract-symbols '(1 (2 3) x (y z 3))) +] + +} + @section{Org-mode interoperability} Org-mode supports laying out the output of code blocks as tables, which is very diff --git a/utils.rkt b/utils.rkt index 4e1b8c7..4922b16 100644 --- a/utils.rkt +++ b/utils.rkt @@ -14,7 +14,8 @@ (require typed/graph typed/rackunit (for-syntax syntax/parse racket/list)) - (provide eval-with eval1-with auto-hash-ref/explicit auto-hash-ref/:) + (provide eval-with eval1-with auto-hash-ref/explicit auto-hash-ref/: + extract-symbols) (define-type Variable Symbol) (define-type (VariableMapping A) (Immutable-HashTable Variable A)) @@ -100,17 +101,35 @@ (let ([x-str (symbol->string x)]) (if (eq? #\: (string-ref x-str 0)) (string->symbol (substring x-str 1)) - x))))) + x)))) + + (: extract-symbols (-> Any (Listof Symbol))) + (define (extract-symbols form) + (: extract-rec (-> Any (Listof Any))) + (define (extract-rec form) + (match form + [(? symbol?) (list form)] + [(? list?) + (flatten (for/list : (Listof Any) + ([x form]) + (extract-symbols x)))] + [else '()])) + (cast (extract-rec form) (Listof Symbol))) + + (module+ test + (test-case "extract-symbols" + (check-equal? (extract-symbols '(1 (2 3) x (y z 3))) + '(x y z))))) (require 'typed) -(provide eval-with eval1-with auto-hash-ref/explicit auto-hash-ref/:) +(provide eval-with eval1-with auto-hash-ref/explicit auto-hash-ref/: + extract-symbols) ;;; Untyped section. (provide ;; Functions - (contract-out [extract-symbols (-> any/c list?)] - [any->string (-> any/c string?)] + (contract-out [any->string (-> any/c string?)] [stringify-variable-mapping (-> variable-mapping? string-variable-mapping?)] [string->any (-> string? any/c)] [read-org-sexp (-> string? (listof any/c))] @@ -160,26 +179,6 @@ (define (variable-mapping? dict) (hash/c symbol? any/c)) - -;;; ============================== -;;; Analysis of quoted expressions -;;; ============================== - -;;; Produces a list of symbols appearing in the quoted expression -;;; passed in the first argument. -(define (extract-symbols form) - (match form - [(? symbol?) (list form)] - [(? list?) (flatten (for/list ([x form]) - (extract-symbols x)))] - [else '()])) - -(module+ test - (test-case "extract-symbols" - (check-equal? (extract-symbols '(1 (2 3) x (y z 3))) - '(x y z)))) - - ;;; ========================= ;;; Interaction with Org-mode ;;; =========================