From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Michael Heerdegen Newsgroups: gmane.emacs.devel Subject: pcase-if-let? Date: Thu, 29 Mar 2018 01:20:50 +0200 Message-ID: <871sg3eqi5.fsf@web.de> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1522279143 9888 195.159.176.226 (28 Mar 2018 23:19:03 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 28 Mar 2018 23:19:03 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) To: Emacs Development Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Thu Mar 29 01:18:59 2018 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1f1KLL-0002PI-Ek for ged-emacs-devel@m.gmane.org; Thu, 29 Mar 2018 01:18:59 +0200 Original-Received: from localhost ([::1]:50834 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f1KNM-00048U-Po for ged-emacs-devel@m.gmane.org; Wed, 28 Mar 2018 19:21:04 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:41936) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f1KNF-00047p-U0 for emacs-devel@gnu.org; Wed, 28 Mar 2018 19:20:58 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1f1KNC-0002nl-QT for emacs-devel@gnu.org; Wed, 28 Mar 2018 19:20:57 -0400 Original-Received: from mout.web.de ([212.227.15.4]:60713) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1f1KNC-0002mn-GX for emacs-devel@gnu.org; Wed, 28 Mar 2018 19:20:54 -0400 Original-Received: from drachen.dragon ([88.75.99.135]) by smtp.web.de (mrweb004 [213.165.67.108]) with ESMTPSA (Nemesis) id 0M844f-1efpJ42F2f-00vggD; Thu, 29 Mar 2018 01:20:51 +0200 X-Provags-ID: V03:K0:ecMtzaYkzCqh+Buv6WxT7HBF/GdDlHFlNDbmzBmGm3ePcq+MD/0 fEc0njCQqMrtrz8kYtzn75axS5arsHCyhje8a3Eu47h7XfCc/fW+H+QleIoGXzUVaTw5Bkf rwINhQU6c2NuKW/cl7ozNpN+lOG4YBY43nFRjdEXQxQm2gu8LCXqOhQdsS/P1HU2psw8Jiu Fr38Nb3tjQ7hQrcpK+zkQ== X-UI-Out-Filterresults: notjunk:1;V01:K0:HLGWPLAwgA0=:3Noy3ME/UsiFV1k572flAB fQ/aq3csgLaYx8B6DqMAQR22eGOa2Ul/Y3w4IPjOVmsMubjgqPMJe5rsG/yKxkvmma+qjeOP4 3wFDVp6v3qa3CmfsrV/bcXoIuKjV91fONdoidxDxLeH2Cd6Df3LXaYogoqOCecP4ipvo6iIBM 4WA8N7Cu6MjmocNY4F6fAfNpurif50fWc22NKGd1eC0jPfGLD34HGhGYKSgxDdkqJ6Ha4/jit ihyX+msL71z89L9S7vFhZh/faT1fVGIb5+5fCyRwKB9BIo1zKYWkFXzLMtnSfR4ASH1qo/wLw VMiAVq13FWy50DRZnHFKR/WeMKDaeQxIPnzhyEXlGtC1tesTvtLLJGY0TyS4M71F3JOe/1hq8 mGnxWa7EbZYnSUzxMAsD3vyA6Njk0YMF/nmURRDP9X/0x3oVN316+l9aATA3cKz7BeIHP98Zg /zQKdV7fA0MEOPK4mZguMlCL48ExJKLLwOK65GhIPBSdIOIrPLaWz3IWopcyEbcudgGedVo6/ SV8uBYrllhVc1fQGoiWA5QgG1+wszJm0HZpN19GJYATSfJohfOXDLwqEAb+b2EQXrfemVqo7L nfOBLdSuVA1aVP2z8GbORqCaQTYAQoZ4561n7NJCrs/RUlmKQiAEDfVWfe6aFwiMR5CwicFMY vQM2Ppm6PQYlVJQyTPhQblXLfHiAxYwhssbCBUS70lsdHYL1G9I+/wRqE5PMzCRnk7SrPR68S GXdB4Ye5KslBWV5HSZbe+r7CXWVyK4LpXDU3k10Kjf4L56lzxW77zH3Q2fUiJq/fI9Y/cuDC X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 212.227.15.4 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 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" Xref: news.gmane.org gmane.emacs.devel:224129 Archived-At: Hello, should we add a `pcase-if-let' to pcase.el? While the name might sound a bit obscure, in my experience I quite often wanted something like this, instead of having to write #+begin_src emacs-lisp (pcase value (pattern code more code ...) (_ other code more other code ...)) #+end_src for example. Here is the result of a first naive implementation trial (which works for me): #+begin_src emacs-lisp (defmacro pcase-if-let (bindings then &rest elses) "Bind variables according to BINDINGS and eval THEN or ELSE. This is like `if-let' but BINDINGS is a list of elements of the form \(PATTERN VALUE-FORM) and it's tested whether all the PATTERNs match instead of whether VALUE-FORMS are all non-nil." (declare (indent 2) (debug ((&rest (pcase-PAT &optional form)) form body))) (if (null bindings) '(let* () then) (let ((sym (make-symbol "sym")) (sucess-syms '()) (last-sucess-sym nil)) (dotimes (i (length bindings)) (push (make-symbol (format "binding-%d-sucess" i)) sucess-syms)) (cl-callf nreverse sucess-syms) `(progn (mapc (lambda (,sym) (setq ,sym nil)) ',sucess-syms) (pcase nil ((and ,@(mapcar (pcase-lambda (`(,pattern ,value)) `(let (and ,@(if last-sucess-sym `((guard ,last-sucess-sym)) '()) ,pattern (let ,(setq last-sucess-sym (pop sucess-syms)) t)) ,value)) bindings)) (if ,last-sucess-sym ,then ,@elses))))))) #+end_src Note that there is no need for a `pcase-if' (just don't bind variables in the patterns), so we could also use this name I think. Also note that we can't unite this macro with `if-let' (because it is about non-nil-ness of values, while `pcase-if-let' is about pattern matching, so a binding like (SYMBOL VALUE) has different semantics). Thanks, Michael.