From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: Re: [Emacs-diffs] map 988d721: Add a pcase pattern for maps and `map-let' based on it Date: Tue, 02 Jun 2015 21:06:44 -0400 Message-ID: References: <20150602201956.22733.21800@vcs.savannah.gnu.org> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1433293622 2675 80.91.229.3 (3 Jun 2015 01:07:02 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 3 Jun 2015 01:07:02 +0000 (UTC) Cc: Nicolas Petton To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Wed Jun 03 03:06:53 2015 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Yzx8u-0003Nf-Mf for ged-emacs-devel@m.gmane.org; Wed, 03 Jun 2015 03:06:52 +0200 Original-Received: from localhost ([::1]:33137 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yzx8t-000479-JG for ged-emacs-devel@m.gmane.org; Tue, 02 Jun 2015 21:06:51 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:60477) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yzx8r-000446-1C for emacs-devel@gnu.org; Tue, 02 Jun 2015 21:06:49 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Yzx8n-00055m-Rg for emacs-devel@gnu.org; Tue, 02 Jun 2015 21:06:48 -0400 Original-Received: from ironport2-out.teksavvy.com ([206.248.154.181]:8402) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yzx8n-00055f-Nj for emacs-devel@gnu.org; Tue, 02 Jun 2015 21:06:45 -0400 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0AwFgA731xV/3K9xEVcgxCEAoVVu0CHSwQCAoE8OxIBAQEBAQEBgQpBBYNdAQEEViMQCzQSFBgNiGPPIwEBAQEGAQEBAR6LOoQ2TwcWhBcFsz+BRSOBZiMdFYFZIoJ4AQEB X-IPAS-Result: A0AwFgA731xV/3K9xEVcgxCEAoVVu0CHSwQCAoE8OxIBAQEBAQEBgQpBBYNdAQEEViMQCzQSFBgNiGPPIwEBAQEGAQEBAR6LOoQ2TwcWhBcFsz+BRSOBZiMdFYFZIoJ4AQEB X-IronPort-AV: E=Sophos;i="5.13,465,1427774400"; d="scan'208";a="124025388" Original-Received: from 69-196-189-114.dsl.teksavvy.com (HELO ceviche.home) ([69.196.189.114]) by ironport2-out.teksavvy.com with ESMTP/TLS/DHE-RSA-AES256-SHA; 02 Jun 2015 21:06:44 -0400 Original-Received: by ceviche.home (Postfix, from userid 20848) id 6D6C7660ED; Tue, 2 Jun 2015 21:06:44 -0400 (EDT) In-Reply-To: (Nicolas Petton's message of "Tue, 02 Jun 2015 20:19:56 +0000") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 206.248.154.181 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:186980 Archived-At: > + "pcase pattern matching map elements. > +Matches if the object is a map (list, hash-table or array), and > +binds values from ARGS to the corresponding element of the map. > + > +ARGS can be an alist of key/binding pairs of a list of keys." I think this would benefit from a bit more detail. E.g. you could say "ARGS is a list of elements of the form (KEY PAT)" or something like that. > + (seq-map (lambda (elt) > + (if (consp elt) > + `(app (pcase--flip map-elt ',(car elt)) ,(cdr elt)) > + `(app (pcase--flip map-elt ',elt) ,elt))) > + args)) Hmm... It looks like it's actually elements of the form (KEY . PAT) or elements of the form SYMBOL which stands for (SYMBOL . SYMBOL). Some alternatives to consider: - Use (KEY PAT) instead of (KEY . PAT). This makes the source code a bit more concise, so I'm in favor of the (KEY PAT) form. - Use a plist rather than an alist. I like alists better in general, so I'm fine with this choice. - let KEY be evaluated rather than having it be quoted. I.e. require the programmer to write 'a when looking up the key `a'. It costs an extra quote in some/many cases, but does give you extra power. Of course, the downside is that it begs the question of what to do with the SYMBOL case. FWIW, in dash.el, the `-let' includes a similar destructuring facility, with patterns of the form: (&plist key0 a0 ... keyN aN) - bind value mapped by keyK in the `source` plist to aK. If the value is not found, aK is nil. (&alist key0 a0 ... keyN aN) - bind value mapped by keyK in the `source` alist to aK. If the value is not found, aK is nil. (&hash key0 a0 ... keyN aN) - bind value mapped by keyK in the `source` hash table to aK. If the value is not found, aK is nil. where, AFAICT the `key's are expressions which are evaluated before being passed to the respective lookup function. IOW when you want a constant key you need to quote it, unless that constant is self-quoting (e.g. a keyword). Stefan