I just did something like that”
;; [Listof String] -> Week
;; parse a list of strings into a week
(define (1-week los)
...
(define zipped (map list all-keywords all-vals))
(define sorted (sort zipped keyword<? #:key first))
(keyword-apply week (map first sorted) (map second sorted) '()))
The last line is a call to a constructor for keyword-based structs.
The . . . part parses a file to generate the call from specs (using
string->keyword).
[[ For completeness,
(define-syntax (struct/opt stx)
(syntax-case stx ()
[(_ name ([field default contract] ...))
(let* ([fields (syntax->list #'(field ...))]
[field-names (map syntax-e fields)]
[field+default* (syntax->list #'([field default] ...))]
[field+contract* (syntax->list #'([field contract] ...))]
[keywords
(for/list ((f field-names))
(string->keyword (format "~a" f)))]
;; ---
[parameters (append-map list keywords field+default*)]
[checks
(for/list ((f+c field+contract*))
(match-define `(,f ,c) (syntax-e f+c))
#`(unless (#,c #,f)
(error 'name "~a must be a ~a, given ~e" '#,f '#,c #,f)))])
#`(begin
(struct name (field ...)
#:transparent
#:extra-constructor-name make-name)
(define old-name name)
(set! make-name
(lambda #,parameters
#,@checks
(old-name #,@fields)))))]))
(define link/c (list/c symbol? string?))
(define link*/c (or/c link/c (listof link/c)))
(define (date-symbol? day)
(and (symbol? day)
(regexp-match #px"(\\d*)-(\\d*)" (symbol->string day))))
(struct/opt
week
([topic "" string?]
[day-topics '() (listof
(or/c (list/c date-symbol? string?)
(list/c date? string? link*/c)))]
[readings '() link*/c]
[optional '() link*/c]
[lab #false (or/c #false string?)]
[homework '(a b c) (or/c #false (and cons? (listof (or/c #false symbol?))))]
[exam #false (or/c false? (list/c date-symbol? symbol?))]
[rationale #false (or/c false? string?)]))
]]
> On Oct 26, 2016, at 2:11 PM, David Storrs <[email protected]> wrote:
>
> Is it possible to generate keywords for a call?
>
> For example, this works:
>
> (with-output-to-file
> (buildpath "." "foo")
> (lambda () (displayln "foooo!"))
> #:mode 'text
> #:exists 'replace)
>
> But this does not:
>
> (with-output-to-file
> (buildpath "." "foo")
> (lambda () (displayln "foooo!"))
> #:mode 'text
> (string->keyword "exists") 'replace)
>
> I ask because I'm interested in automating the production of structs
> from the murphy/protobuf library for purposes of testing. It provides
> reflection usage so I can determine the fields and their types, but
> the structs are created using keywords.
>
> I'm guessing that this is a phase issue and I could do it with a macro
> at compile time but not a function at runtime. Is that right?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.