Re: Simple list of key-value pairs?

2021-07-09 Thread Aleix Conchillo Flaqué
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 
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
>
>


Re: Simple list of key-value pairs?

2021-07-09 Thread Tim Van den Langenbergh
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





Re: Simple list of key-value pairs?

2021-07-09 Thread Zelphir Kaltstahl
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



Re: Simple list of key-value pairs?

2021-07-08 Thread Luis Felipe
Hi,

On Thursday, July 8th, 2021 at 2:37 PM, Hartmut Goebel 
 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)



Re: Simple list of key-value pairs?

2021-07-08 Thread Christopher Lam
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 
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.goe...@crazy-compilers.com   |
> | www.crazy-compilers.com | compilers which you thought are impossible |
>
>
>


Simple list of key-value pairs?

2021-07-08 Thread Hartmut Goebel

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.goe...@crazy-compilers.com   |
| www.crazy-compilers.com | compilers which you thought are impossible |