utils: Type extract-symbols.

This commit is contained in:
Sergiu Ivanov 2022-01-19 00:40:41 +01:00
parent 85d60c6a9b
commit 8ffd252fc2
2 changed files with 36 additions and 26 deletions

View file

@ -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

View file

@ -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
;;; =========================