utils: Type map-sexp.

This commit is contained in:
Sergiu Ivanov 2022-01-23 15:50:06 +01:00
parent b6417d2d07
commit ec84924608
2 changed files with 28 additions and 19 deletions

View file

@ -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 Listof Symbol String))) Any AnyValues Listof Symbol String Number Sexp cast)))
@title[#:tag "utils"]{dds/utils: Various Utilities} @title[#:tag "utils"]{dds/utils: Various Utilities}
@ -164,6 +164,19 @@ Reads any value from string.
(string->any "(or b (not a))") (string->any "(or b (not a))")
]} ]}
@defproc[(map-sexp [func (-> Any Any)] [sexp Any]) Any]{
Given a @racket[Sexp], applies the @racket[func] to any object which is not
a list.
@racket[map-sexp] will not check whether @racket[func] is indeed applicable to
every non-list element of @racket[sexp]. If this is not the case, a contract
violation for func will be generated.
@examples[#:eval utils-evaluator
(map-sexp (λ (x) (add1 (cast x Number))) '(1 2 (4 10) 3))
]}
@section{Additional graph utilities} @section{Additional graph utilities}
@section{Pretty printing} @section{Pretty printing}

View file

@ -16,7 +16,7 @@
(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 any->string stringify-variable-mapping string->any extract-symbols any->string stringify-variable-mapping string->any
handle-org-booleans) handle-org-booleans map-sexp)
(define-type Variable Symbol) (define-type Variable Symbol)
(define-type (VariableMapping A) (Immutable-HashTable Variable A)) (define-type (VariableMapping A) (Immutable-HashTable Variable A))
@ -172,18 +172,29 @@
(check-equal? (handle-org-booleans "#f") #f) (check-equal? (handle-org-booleans "#f") #f)
(check-equal? (handle-org-booleans '("#t" "#f")) '(#t #f)) (check-equal? (handle-org-booleans '("#t" "#f")) '(#t #f))
(check-equal? (handle-org-booleans "t") "t"))) (check-equal? (handle-org-booleans "t") "t")))
(: map-sexp (-> (-> Any Any) Any Any))
(define (map-sexp func sexp)
(match sexp
[(? list?) (map ((curry map-sexp) func) sexp)]
[datum (func datum)]))
(module+ test
(test-case "map-sexp"
(check-equal? (map-sexp (λ (x) (add1 (cast x Number))) '(1 2 (4 10) 3))
'(2 3 (5 11) 4))))
) )
(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 any->string stringify-variable-mapping string->any) extract-symbols any->string stringify-variable-mapping string->any
map-sexp)
;;; Untyped section. ;;; Untyped section.
(provide (provide
;; Functions ;; Functions
(contract-out [read-org-sexp (-> string? (listof any/c))] (contract-out [read-org-sexp (-> string? (listof any/c))]
[map-sexp (-> procedure? any/c any/c)]
[unorg (-> string? (listof any/c))] [unorg (-> string? (listof any/c))]
[unstringify-pairs (-> (listof (general-pair/c string? any/c)) [unstringify-pairs (-> (listof (general-pair/c string? any/c))
(listof (general-pair/c symbol? any/c)))] (listof (general-pair/c symbol? any/c)))]
@ -232,21 +243,6 @@
;;; A string variable mapping is a mapping from variables to strings. ;;; A string variable mapping is a mapping from variables to strings.
(define (string-variable-mapping? dict) (hash/c symbol? string?)) (define (string-variable-mapping? dict) (hash/c symbol? string?))
;;; 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)]))
(module+ test
(test-case "map-sexp"
(check-equal? (map-sexp add1 '(1 2 (4 10) 3)) '(2 3 (5 11) 4))))
;;; Reads a sexp from a string produced by Org-mode for a named table. ;;; Reads a sexp from a string produced by Org-mode for a named table.
;;; See example.org for examples. ;;; See example.org for examples.
(define read-org-sexp (define read-org-sexp