From patchwork Mon Sep 26 04:08:23 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Eric Bavier X-Patchwork-Id: 16008 Received: (qmail 46226 invoked by uid 89); 26 Sep 2016 04:09:01 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Checked: by ClamAV 0.99.2 on sourceware.org X-Virus-Found: No X-Spam-SWARE-Status: No, score=-4.7 required=5.0 tests=AWL, BAYES_00, RP_MATCHES_RCVD, SPF_PASS autolearn=ham version=3.3.2 spammy=bonus X-Spam-Status: No, score=-4.7 required=5.0 tests=AWL, BAYES_00, RP_MATCHES_RCVD, SPF_PASS autolearn=ham version=3.3.2 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on sourceware.org X-Spam-Level: X-HELO: lists.gnu.org Received: from lists.gnu.org (HELO lists.gnu.org) (208.118.235.17) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 26 Sep 2016 04:08:50 +0000 Received: from localhost ([::1]:41452 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1boNDk-0001aY-Sy for patchwork@sourceware.org; Mon, 26 Sep 2016 00:08:48 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55488) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1boNDf-0001aE-4n for guix-devel@gnu.org; Mon, 26 Sep 2016 00:08:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1boNDb-0007ls-RX for guix-devel@gnu.org; Mon, 26 Sep 2016 00:08:42 -0400 Received: from smtp13.openmailbox.org ([62.4.1.47]:48856) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1boNDb-0007lS-EO for guix-devel@gnu.org; Mon, 26 Sep 2016 00:08:39 -0400 Received: by mail2.openmailbox.org (Postfix, from userid 1001) id 65B89103173; Mon, 26 Sep 2016 06:08:37 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=openmailbox.org; s=openmailbox; t=1474862917; bh=Y3XMm3ZPVYoz+5gwErRD5R0zpicDFBRSNbwirAtG72A=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=u+k4+cjRWxpmNAeDuCWL0xDzEWhb+g9fimA3eJsyLDV237QXWKJ1XY4x2mAVSeXii moeOzwSWr87j69pouc779nZUzqXeDOGrzP6Z4FkjwJ7zEFbniBHkQPPeeHDsXeHDDM 9J3AaUWQjJ4kUdI/YpkAk1cA6InDh0dMrcHkkKQE= Date: Sun, 25 Sep 2016 23:08:23 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=openmailbox.org; s=openmailbox; t=1474862915; bh=Y3XMm3ZPVYoz+5gwErRD5R0zpicDFBRSNbwirAtG72A=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=UMnRUYr2ZWUHv6MyA3iQ9PtCTFTwWYvIq5DLZnu33Om6aLNGGpkFKW9khzphiHtRr jfG72kGE5xyxlqMVv0rJ5TyZq8VkTGllF5mc7s52Kuc9IrYngLVLUqvMcOc08FI/ks vULbAV1HKZSc4MMxozRjhoQ8PB1QomlpoSGvFfPs= From: Eric Bavier To: ludo@gnu.org (Ludovic =?UTF-8?Q?Court=C3=A8s?=) Subject: Re: [PATCH 2/2] utils: Support defaults in substitute-keyword-arguments. Message-ID: <20160925230823.3782ff7b@openmailbox.org> In-Reply-To: <87mviyf363.fsf@gnu.org> References: <20160920212916.18530-1-ericbavier@openmailbox.org> <20160920212916.18530-2-ericbavier@openmailbox.org> <87mviyf363.fsf@gnu.org> X-Mailer: Claws Mail 3.13.2 (GTK+ 2.24.30; x86_64-unknown-linux-gnu) MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 62.4.1.47 X-BeenThere: guix-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Development of GNU Guix and the GNU System distribution." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: guix-devel@gnu.org Errors-To: guix-devel-bounces+patchwork=sourceware.org@gnu.org Sender: "Guix-devel" On Sat, 24 Sep 2016 11:21:40 +0900 ludo@gnu.org (Ludovic Courtès) wrote: > > > +replaced by EXP. EXP is evaluated in a context where VAR is bound to the > > +previous value of the keyword argument, or DFLT if given." > > + (syntax-case x () > > + ((_ original-args ((kw var dflt ...) exp) ...) > > + #`(let loop ((args (default-keyword-arguments > > + original-args > > + (list #,@(append-map (match-lambda > > + ((k) '()) > > + (x x)) > > + #'((kw dflt ...) ...))))) > > + (before '())) > > I would prefer to stick to ‘syntax-rules’ when matching the clauses, > with a helper macro: Right, I was wanting to do that, but was having a difficult time working with mixed cases, where some keywords are declared with a default and others are not. > But this would be a bonus; How about this patch instead? `~Eric From 9481121fef60f0c3f4ea0f742d77336906771167 Mon Sep 17 00:00:00 2001 From: Eric Bavier Date: Tue, 20 Sep 2016 15:41:31 -0500 Subject: [PATCH] utils: Support defaults in substitute-keyword-arguments. * guix/utils.scm (collect-default-args, expand-default-args): New syntax. (substitute-keyword-arguments): Allow default value declarations. * tests/utils.scm (substitute-keyword-arguments): New test. --- guix/utils.scm | 29 +++++++++++++++++++++++++---- tests/utils.scm | 20 ++++++++++++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/guix/utils.scm b/guix/utils.scm index ded3114..cbad4ea 100644 --- a/guix/utils.scm +++ b/guix/utils.scm @@ -375,13 +375,34 @@ keywords not already present in ARGS." (() args)))) +(define-syntax collect-default-args + (syntax-rules () + ((_ (_ _)) + (list)) + ((_ (kw _ dflt)) + (list kw dflt)) + ((_ (_ _) rest ...) + (collect-default-args rest ...)) + ((_ (kw _ dflt) rest ...) + (cons* kw dflt (collect-default-args rest ...))))) + +(define-syntax expand-default-args + (syntax-rules () + ((_ original-args (kw var) ...) + original-args) + ((_ original-args clause ...) + (default-keyword-arguments + original-args + (collect-default-args clause ...))))) + (define-syntax substitute-keyword-arguments (syntax-rules () "Return a new list of arguments where the value for keyword arg KW is -replaced by EXP. EXP is evaluated in a context where VAR is boud to the -previous value of the keyword argument." - ((_ original-args ((kw var) exp) ...) - (let loop ((args original-args) +replaced by EXP. EXP is evaluated in a context where VAR is bound to the +previous value of the keyword argument, or DFLT if given." + ((_ original-args ((kw var dflt ...) exp) ...) + (let loop ((args (expand-default-args original-args + (kw var dflt ...) ...)) (before '())) (match args ((kw var rest (... ...)) diff --git a/tests/utils.scm b/tests/utils.scm index 960928c..bcfaa14 100644 --- a/tests/utils.scm +++ b/tests/utils.scm @@ -123,6 +123,26 @@ (default-keyword-arguments '(#:bar 3) '(#:foo 2)) (default-keyword-arguments '(#:foo 2 #:bar 3) '(#:bar 6)))) +(test-equal "substitute-keyword-arguments" + '((#:foo 3) + (#:foo 3) + (#:foo 3 #:bar (1 2)) + (#:bar (1 2) #:foo 3) + (#:foo 3)) + (list (substitute-keyword-arguments '(#:foo 2) + ((#:foo f) (1+ f))) + (substitute-keyword-arguments '() + ((#:foo f 2) (1+ f))) + (substitute-keyword-arguments '(#:foo 2 #:bar (2)) + ((#:foo f) (1+ f)) + ((#:bar b) (cons 1 b))) + (substitute-keyword-arguments '(#:foo 2) + ((#:foo _) 3) + ((#:bar b '(2)) (cons 1 b))) + (substitute-keyword-arguments '(#:foo 2) + ((#:foo f 1) (1+ f)) + ((#:bar b) (cons 42 b))))) + (test-assert "filtered-port, file" (let* ((file (search-path %load-path "guix.scm")) (input (open-file file "r0b"))) -- 2.9.2