[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Simple list of key-value pairs?
From: |
Christopher Lam |
Subject: |
Re: Simple list of key-value pairs? |
Date: |
Thu, 8 Jul 2021 15:22:29 +0000 |
IMO quotes and quasiquotes are being used perfectly well. You can use
'(system #f). However I suspect building a list of intermediate strings is
not desirable. And we may want to eliminate map and filter. How about
building the query-string piecemeal then building the final string once?
(define (build-query-string)
(let lp ((query (or query '())) (acc '()))
(match query
(() (string-concatenate acc))
(((_ #f) . rest) (lp rest acc))
(((name val) . rest)
(lp rest (cons* name "="
(if (string? val) val (object->string val))
(if (null? acc) "" "&")
acc))))))
On Thu, 8 Jul 2021 at 14:37, Hartmut Goebel <h.goebel@crazy-compilers.com>
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))
>
> --
> Regards
> Hartmut Goebel
>
> | Hartmut Goebel | h.goebel@crazy-compilers.com |
> | www.crazy-compilers.com | compilers which you thought are impossible |
>
>
>