utils: Type extract-symbols.
This commit is contained in:
parent
85d60c6a9b
commit
8ffd252fc2
2 changed files with 36 additions and 26 deletions
|
@ -2,7 +2,7 @@
|
||||||
@(require scribble/example racket/sandbox
|
@(require scribble/example racket/sandbox
|
||||||
(for-label racket graph (submod "../utils.rkt" typed)
|
(for-label racket graph (submod "../utils.rkt" typed)
|
||||||
(only-in typed/racket/base
|
(only-in typed/racket/base
|
||||||
Any AnyValues)))
|
Any AnyValues Listof Symbol)))
|
||||||
|
|
||||||
@title[#:tag "utils"]{dds/utils: Various Utilities}
|
@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}
|
@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}
|
@section{Org-mode interoperability}
|
||||||
|
|
||||||
Org-mode supports laying out the output of code blocks as tables, which is very
|
Org-mode supports laying out the output of code blocks as tables, which is very
|
||||||
|
|
49
utils.rkt
49
utils.rkt
|
@ -14,7 +14,8 @@
|
||||||
(require typed/graph typed/rackunit
|
(require typed/graph typed/rackunit
|
||||||
(for-syntax syntax/parse racket/list))
|
(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 Variable Symbol)
|
||||||
(define-type (VariableMapping A) (Immutable-HashTable Variable A))
|
(define-type (VariableMapping A) (Immutable-HashTable Variable A))
|
||||||
|
@ -100,17 +101,35 @@
|
||||||
(let ([x-str (symbol->string x)])
|
(let ([x-str (symbol->string x)])
|
||||||
(if (eq? #\: (string-ref x-str 0))
|
(if (eq? #\: (string-ref x-str 0))
|
||||||
(string->symbol (substring x-str 1))
|
(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)
|
(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.
|
;;; Untyped section.
|
||||||
|
|
||||||
(provide
|
(provide
|
||||||
;; Functions
|
;; Functions
|
||||||
(contract-out [extract-symbols (-> any/c list?)]
|
(contract-out [any->string (-> any/c string?)]
|
||||||
[any->string (-> any/c string?)]
|
|
||||||
[stringify-variable-mapping (-> variable-mapping? string-variable-mapping?)]
|
[stringify-variable-mapping (-> variable-mapping? string-variable-mapping?)]
|
||||||
[string->any (-> string? any/c)]
|
[string->any (-> string? any/c)]
|
||||||
[read-org-sexp (-> string? (listof any/c))]
|
[read-org-sexp (-> string? (listof any/c))]
|
||||||
|
@ -160,26 +179,6 @@
|
||||||
|
|
||||||
(define (variable-mapping? dict) (hash/c symbol? any/c))
|
(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
|
;;; Interaction with Org-mode
|
||||||
;;; =========================
|
;;; =========================
|
||||||
|
|
Loading…
Reference in a new issue