unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Zelphir Kaltstahl <zelphirkaltstahl@posteo.de>
To: Hartmut Goebel <h.goebel@crazy-compilers.com>
Cc: Guile User <guile-user@gnu.org>
Subject: Re: Simple list of key-value pairs?
Date: Fri,  9 Jul 2021 11:31:01 +0000	[thread overview]
Message-ID: <52395f5c-a470-1b47-2c5a-7274bbd14940@posteo.de> (raw)
In-Reply-To: <410e7168-3709-c961-c894-d534782ef526@crazy-compilers.com>

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



  parent reply	other threads:[~2021-07-09 11:31 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-08 14:37 Simple list of key-value pairs? Hartmut Goebel
2021-07-08 15:22 ` Christopher Lam
2021-07-08 18:59 ` Luis Felipe
2021-07-09 11:31 ` Zelphir Kaltstahl [this message]
2021-07-09 22:37   ` Aleix Conchillo Flaqué
2021-07-09 11:55 ` Tim Van den Langenbergh

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=52395f5c-a470-1b47-2c5a-7274bbd14940@posteo.de \
    --to=zelphirkaltstahl@posteo.de \
    --cc=guile-user@gnu.org \
    --cc=h.goebel@crazy-compilers.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).