guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Write a macro which defines a procedure


From: Zelphir Kaltstahl
Subject: Re: Write a macro which defines a procedure
Date: Sun, 21 Jul 2019 03:48:50 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2

Hi,

At first I had the macro use `define`, but then I thought: "What if I
want to conditionally define a route procedure?". My guess is, that then
the define form would be inside some `cond` or `if` form and then it
would not work (I did not check this assumption though, because my macro
did not work yet.). That is why I looked for something that does what
`module-define!` does.

On 7/21/19 3:26 AM, Mark H Weaver wrote:
> Matt Wette <address@hidden> writes:
>
>> On 7/19/19 3:51 PM, zelphirkaltstahl wrote:
>>> (module-define! (current-module)
>>>                      ;; `route` should be `/container/json` for example.
>>>                      route
>> (quote route)
> Yes, or equivalently:  'route
>
> As an aside, is there a reason to not define it more simply as follows?
>
>      Mark
>
> --8<---------------cut here---------------start------------->8---
> (define-syntax define-api-route
>   (syntax-rules ()
>     [(define-api-route route http-method my-content-type)
>      ;; `route` should be `/container/json` for example.
>      (define* (route docker-socket #:key (data #f))
>        (call-with-values
>            (lambda ()
>              ;; Here the route needs to be given as a string to `http-get`.
>              (http-get (variable-name->string route)
>                        #:port docker-socket
>                        #:version '(1 . 1)
>                        #:keep-alive? #f
>                        ;; Why is my quasiquote not working as expected?
>                        #:headers `((host . ("localhost" . #f))
>                                    (content-type . (,my-content-type (charset 
> . "utf-8"))))
>                        #:body (scm->json-string data)
>                        #:decode-body? #t
>                        #:streaming? #f))
>          (lambda (response response-text)
>            (let ([resp-text-as-string (bytevector->string response-text 
> "utf-8")])
>              (cons response resp-text-as-string)))))]))
> --8<---------------cut here---------------end--------------->8---



reply via email to

[Prev in Thread] Current Thread [Next in Thread]