From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Tim Van den Langenbergh Newsgroups: gmane.lisp.guile.user Subject: Re: Simple list of key-value pairs? Date: Fri, 09 Jul 2021 13:55:30 +0200 Message-ID: <2008040.57TmEalV0p@terra> References: <410e7168-3709-c961-c894-d534782ef526@crazy-compilers.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="37196"; mail-complaints-to="usenet@ciao.gmane.io" Cc: guile-user@gnu.org To: Hartmut Goebel Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Fri Jul 09 13:55:57 2021 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m1p6r-0009Wo-DT for guile-user@m.gmane-mx.org; Fri, 09 Jul 2021 13:55:57 +0200 Original-Received: from localhost ([::1]:55694 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m1p6q-0004Ii-1G for guile-user@m.gmane-mx.org; Fri, 09 Jul 2021 07:55:56 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:45962) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m1p6Y-0004D0-Gl for guile-user@gnu.org; Fri, 09 Jul 2021 07:55:38 -0400 Original-Received: from mout.gmx.net ([212.227.17.20]:55395) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m1p6W-0001QN-96 for guile-user@gnu.org; Fri, 09 Jul 2021 07:55:38 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1625831732; bh=eH9nlKWzt/xklDsPrtWqSI6HTDc7r9O7i1HxCvFWqkk=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=cl0E/+pSjlW0V47ViGrOQcsIal2DdXeOuZOkx6GfYKDd/hlP1lGaViVzjwZzX9hq8 vMV3AZsx083KUWIKjIh16cCXpNeDr3eGaNQvLRRbmeivZ5HOnvPpH+pCAXNELHeaAd qM3Mt+YXugo7rEY2FWBVsOGK4Rk1EpVxkEmcqvzg= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Original-Received: from terra.localnet ([94.227.125.226]) by mail.gmx.net (mrgmx105 [212.227.17.174]) with ESMTPSA (Nemesis) id 1MGQj7-1lwXdO3ETN-00GoeF; Fri, 09 Jul 2021 13:55:31 +0200 In-Reply-To: <410e7168-3709-c961-c894-d534782ef526@crazy-compilers.com> X-Provags-ID: V03:K1:4Z1yokBle0byLFokMsLxPELGhDOrw9nmosixQqc/vLNLl7IycxJ etTIFaabtioZ2ihY61W0gjvn6qjHozhlf7MO6UEQZma0ge7ad6jjttn3l7jnO5mB46+wK9s 9Uiu1tLJcsIqumQp2NpjBfyI337P6llEK2rGYhB8l2+xbuhV7dDfklUDoJGmN+7V905TZPI +RxB8iAHvZQCmWjkvJ9Gw== X-UI-Out-Filterresults: notjunk:1;V03:K0:QOQmadDwDas=:FcvDJJ3vWrM07iG/TwyxwZ M4foHkMUfANVfkteHfaEpoC3x8qHM5LNan/JZetgfsK6JETZ4b66KuVzDV4JpivlKqGKnK2vU BwCtonrBVYsgDYrRjyCD9tPASTVs0WQ9meo4Div1n4m+oBpioYTtZWxHgZqFEcFxQbngY0r2m AhSTznAwweRnU4ETk+CgLxBCWA70xMldBraibWJziogMliARmacrt6S9TFZpmIEWz+9zV9Zfb fnYe9EM3UNO27p8b+31a70Us6yzXb7xcBhv7iYXb5tIrUeLTtE75EPBkGbA2II1e8agB4CpkJ 74olmxLf1bmDM/jpyXvf35mKeY+luLHdgHIaXBW9l9XprWuKFaqqPwqUYTRTI6RE3o0iHyPMI zj4YaVnz0ox9OK+nw9uN/ugwVq7C6dsMO30bwfmxYzVNuEyhhRisjvyU3CwmiYRRbGrHlsfXR PkDsx7Ne8FdpPJwY+Zo/GRzofdYEIwtjLZetCERv0XnX05zkizjvJYK5MA7lSkoH8lYsZiKIY KUD+YxVeWpGN+3QOGJi0BEUqwabE2Xvc+3+wKw01Alu2b2vmNnlHjeHxA3CQ3ChHPEGr+gpMm Y5cQtfFpBMQHfY8C5yVrWy7dyrEr7RbmVSLuSaMM6Ks7gBBbFzjCMqVMiLHdwB3ByJ0yhT67T YJRpAtYh3KrERye0waA+D63f/LNyiHGI/kmV0HZCyP9kMq1yN62RfHYWSXiLXEar16yHn7uNR kwPBgdCIrKWt6C+c5nnUbYcKTkY+fKDozUhnPbU7HjycYcdMb/uf+1vEptOJ9/04xGaCtaqm Received-SPF: pass client-ip=212.227.17.20; envelope-from=tmt_vdl@gmx.com; helo=mout.gmx.net X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.io gmane.lisp.guile.user:17646 Archived-At: 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 "=3D" value)) ; FIXME: encode > ((name (? number? value)) > (string-append name "=3D" (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 argume= nts 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) "=3D" (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