[Top][All Lists]

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

Simple list of key-value pairs?

From: Hartmut Goebel
Subject: Simple list of key-value pairs?
Date: Thu, 8 Jul 2021 16:37:09 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0


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
        (filter (lambda (x) x) (map build-query-string query))
    (format #t "~%Query-String: ~a~%~%" query-string)
    ;; todo: build uri incl. query-string

(api-uri ""; "/api/jobs")
(api-uri ""; "/api/jobs"
     `("nr" ,limit)
     `("evaluation" ,evaluation)
     `("system" ,#f))

Hartmut Goebel

| Hartmut Goebel          |               |
| | compilers which you thought are impossible |

reply via email to

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