guile-user
[Top][All Lists]
Advanced

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

Re: Simple list of key-value pairs?


From: Aleix Conchillo Flaqué
Subject: Re: Simple list of key-value pairs?
Date: Fri, 9 Jul 2021 15:37:49 -0700

Hello,

I would also use Zelphir's approach and pass query parameters as an alist,
the reason is because then you can dynamically generate that alist more
easily if needed and even pass it around, and it looks nicer to me but
that's a personal choice. I would also actually make that an optional
argument instead and explicitly pass it with #:query-params.

(api-uri "https://ci.guix.gnu.org";
                 "/api/jobs"
                 #:query-params `(("nr" . ,limit)
                                              ("evaluation" . ,evaluation)
                                              ("system" . #f)))

Aleix

On Fri, Jul 9, 2021 at 4:37 AM Zelphir Kaltstahl <zelphirkaltstahl@posteo.de>
wrote:

> Hi!
>
> I think I would give the pairs together in an association list and use
> alist
> procedures to work with that. Then you could also only have one backtick,
> at the
> start of the alist:
>
> ~~~~
> `((attr . ,val)
>   (attr . ,val)
>   (attr . ,val)
>   (attr . ,val))
> ~~~~
>
> Looks simpler to me and fewer special characters.
>
> Regards,
> Zelphir
>
> On 7/8/21 4:37 PM, Hartmut Goebel wrote:
> > Hi,
> >
> > I'm seeking advice for passing simple key-value pairs to a function and
> > generate a string out of these values. Neither the names not the number
> of
> > keys is known in advance.
> >
> > Background: This shall become a generic function for generating URI
> > query-strings.
> >
> > My current approach please see below. Anyhow, I'm wondering whether the
> quite
> > and quasiquote can be replaced by something simpler.
> >
> > (use-modules (ice-9 match))
> >
> > (define limit 100)
> > (define evaluation "linux")
> >
> > (define* (api-uri base path #:rest query)
> >
> >   (define (build-query-string kv)
> >     (match kv
> >        ((name #f) #f)
> >        ((name (? string? value))
> >         (string-append name "=" value))  ; FIXME: encode
> >        ((name (? number? value))
> >         (string-append name "=" (number->string value)))))
> >
> >
> >   (format #t "~%Query: ~a~%~%" query)
> >   (let ((query-string
> >      (when query
> >        (string-join
> >         (filter (lambda (x) x) (map build-query-string query))
> >         "&"))))
> >     (format #t "~%Query-String: ~a~%~%" query-string)
> >     ;; todo: build uri incl. query-string
> >   ))
> >
> >
> > (api-uri "https://ci.guix.gnu.org"; "/api/jobs")
> > (api-uri "https://ci.guix.gnu.org"; "/api/jobs"
> >      `("nr" ,limit)
> >      `("evaluation" ,evaluation)
> >      `("system" ,#f))
> >
> --
> repositories: https://notabug.org/ZelphirKaltstahl
>
>


reply via email to

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