unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Simple list of key-value pairs?
@ 2021-07-08 14:37 Hartmut Goebel
  2021-07-08 15:22 ` Christopher Lam
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Hartmut Goebel @ 2021-07-08 14:37 UTC (permalink / raw)
  To: guile-user

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 |




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Simple list of key-value pairs?
  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
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Christopher Lam @ 2021-07-08 15:22 UTC (permalink / raw)
  To: Hartmut Goebel; +Cc: guile-user

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 |
>
>
>


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Simple list of key-value pairs?
  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
  2021-07-09 11:55 ` Tim Van den Langenbergh
  3 siblings, 0 replies; 6+ messages in thread
From: Luis Felipe @ 2021-07-08 18:59 UTC (permalink / raw)
  To: Hartmut Goebel; +Cc: guile-user

Hi,

On Thursday, July 8th, 2021 at 2:37 PM, Hartmut Goebel <h.goebel@crazy-compilers.com> wrote:

> Anyhow, I'm wondering whether the
>
> quite and quasiquote can be replaced by something simpler.

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

Would it be simpler to create the pairs like this?

(cons "nr" limit)
(cons "evaluation" evaluation)
(cons "system" #false)



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Simple list of key-value pairs?
  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
  2021-07-09 22:37   ` Aleix Conchillo Flaqué
  2021-07-09 11:55 ` Tim Van den Langenbergh
  3 siblings, 1 reply; 6+ messages in thread
From: Zelphir Kaltstahl @ 2021-07-09 11:31 UTC (permalink / raw)
  To: Hartmut Goebel; +Cc: Guile User

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



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Simple list of key-value pairs?
  2021-07-08 14:37 Simple list of key-value pairs? Hartmut Goebel
                   ` (2 preceding siblings ...)
  2021-07-09 11:31 ` Zelphir Kaltstahl
@ 2021-07-09 11:55 ` Tim Van den Langenbergh
  3 siblings, 0 replies; 6+ messages in thread
From: Tim Van den Langenbergh @ 2021-07-09 11:55 UTC (permalink / raw)
  To: Hartmut Goebel; +Cc: guile-user

On Thursday, 8 July 2021 16:37:09 CEST you 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))

Hello,

personally I'd use key-value pairs rather than lists for passing in arguments
and simplify it a bit by using fold-right from SRFI-1, like this:

#+BEGIN_SRC scheme
(use-modules (srfi srfi-1))

(define (api-uri base path . query)
  (string-join
   (fold-right
	(lambda (name-value-pair result)
	  (if (cdr name-value-pair)
		  (cons (string-append (car name-value-pair)
							   "="

(object->string (cdr name-value-pair)))
				 result)
		  result))
	'()
	query)
   "&"))

(api-uri "https://ci.guix.gnu.org"
		 "/api/jobs"
		 (cons "nr" limit)
		 (cons "evaluation" evaluation)
		 (cons "system" #f))
#+END_SRC

Vale,

-Tim





^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Simple list of key-value pairs?
  2021-07-09 11:31 ` Zelphir Kaltstahl
@ 2021-07-09 22:37   ` Aleix Conchillo Flaqué
  0 siblings, 0 replies; 6+ messages in thread
From: Aleix Conchillo Flaqué @ 2021-07-09 22:37 UTC (permalink / raw)
  To: Zelphir Kaltstahl; +Cc: Guile User

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
>
>


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-07-09 22:37 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2021-07-09 22:37   ` Aleix Conchillo Flaqué
2021-07-09 11:55 ` Tim Van den Langenbergh

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 NNTP newsgroup(s).