From: Mark Oteiza <mvoteiza@udel.edu>
To: Michael Heerdegen <michael_heerdegen@web.de>
Cc: 28254@debbugs.gnu.org, npostavs@users.sourceforge.net
Subject: bug#28254: 26.0.50; SRFI-2 and-let*
Date: Sat, 2 Sep 2017 09:36:04 -0400 [thread overview]
Message-ID: <20170902133604.GA27251@holos.localdomain> (raw)
In-Reply-To: <87tw0lzn7w.fsf@drachen>
On 02/09/17 at 07:25am, Michael Heerdegen wrote:
>Mark Oteiza <mvoteiza@udel.edu> writes:
>
>> >> I didn't try writing it in the style of if-let*--perhaps if-let* could
>> >> be extended and all three macros would learn (EXPR).
>> >
>> >Yes, I think it's best if all foo-let* macros interpret the varlist in
>> >the same way.
>>
>> Alright, I'll look at it.
>
>Isn't there a problem with EXPR being a symbol S, which already has a
>different meaning (bind S to nil)? Though, this seems barely useful to
>me. Anyway, introducing (EXPR) would thus be backward incompatible.
Yeah, that is true. The following patch implements most of the
previous, except it doesn't specially handle EXPR being just a symbol.
All the incumbent subr-x-tests pass.
This single tuple special case is troublesome IMO:
(if-let* (x) "dogs" "cats") => "cats"
(if-let* (x (y 2)) "dogs" "cats") => (void-function y)
(if-let* (x (y 1) (z 2)) "dogs" "cats") => "cats"
I'm curious if this was brought up in the old discussion when this was
implemented.
diff --git a/lisp/emacs-lisp/subr-x.el b/lisp/emacs-lisp/subr-x.el
index 849ac19d6a..eeacdbcfcd 100644
--- a/lisp/emacs-lisp/subr-x.el
+++ b/lisp/emacs-lisp/subr-x.el
@@ -83,10 +83,16 @@ thread-last
`(internal--thread-argument nil ,@forms))
(defsubst internal--listify (elt)
- "Wrap ELT in a list if it is not one."
- (if (not (listp elt))
- (list elt)
- elt))
+ "Wrap ELT in a list if it is not one.
+If ELT is of the form ((EXPR)), listify (EXPR) with a dummy symbol."
+ (cond
+ ((nlistp elt) (list elt))
+ ((atom (car elt)) elt)
+ ((and (null (cdr elt))
+ (let ((form (car elt)))
+ (or (listp form) (atom form))))
+ (list (cl-gensym) (car elt)))
+ (t elt)))
(defsubst internal--check-binding (binding)
"Check BINDING is properly formed."
@@ -122,8 +128,11 @@ if-let*
Each binding is evaluated in turn with `let*', and evaluation
stops if a binding value is nil. If all are non-nil, the value
of THEN is returned, or the last form in ELSE is returned.
+
Each element of VARLIST is a symbol (which is bound to nil)
or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
+An element can additionally be of the form (EXPR), which is
+evaluated and checked for nil.
In the special case you only want to bind a single value,
VARLIST can just be a plain tuple.
\n(fn VARLIST THEN ELSE...)"
@@ -134,18 +143,23 @@ if-let*
(not (listp (car bindings))))
;; Adjust the single binding case
(setq bindings (list bindings)))
- `(let* ,(internal--build-bindings bindings)
- (if ,(car (internal--listify (car (last bindings))))
- ,then
- ,@else)))
+ (if bindings
+ `(let* ,(setq bindings (internal--build-bindings bindings))
+ (if ,(caar (last bindings))
+ ,then
+ ,@else))
+ `(let* () ,then)))
(defmacro when-let* (bindings &rest body)
"Bind variables according to VARLIST and conditionally eval BODY.
Each binding is evaluated in turn with `let*', and evaluation
stops if a binding value is nil. If all are non-nil, the value
of the last form in BODY is returned.
+
Each element of VARLIST is a symbol (which is bound to nil)
or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
+An element can additionally be of the form (EXPR), which is
+evaluated and checked for nil.
In the special case you only want to bind a single value,
VARLIST can just be a plain tuple.
\n(fn VARLIST BODY...)"
@@ -154,7 +168,12 @@ when-let*
(defalias 'if-let 'if-let*)
(defalias 'when-let 'when-let*)
-(defalias 'and-let* 'when-let*)
+
+(defmacro and-let* (varlist &rest body)
+ "Bind variables according to VARLIST and conditionally eval BODY.
+Like `when-let*', except if BODY is empty and all the bindings
+are non-nil, then the result is t."
+ `(when-let* ,varlist ,@(or body '(t))))
(defsubst hash-table-empty-p (hash-table)
"Check whether HASH-TABLE is empty (has 0 elements)."
next prev parent reply other threads:[~2017-09-02 13:36 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-08-27 20:11 bug#28254: 26.0.50; SRFI-2 and-let* Mark Oteiza
2017-09-01 2:55 ` npostavs
2017-09-02 2:10 ` Mark Oteiza
2017-09-02 3:05 ` npostavs
2017-09-02 4:14 ` Mark Oteiza
2017-09-02 5:25 ` Michael Heerdegen
2017-09-02 13:36 ` Mark Oteiza [this message]
2017-09-02 18:41 ` Noam Postavsky
2017-09-03 17:48 ` Michael Heerdegen
2017-09-03 22:39 ` Mark Oteiza
2017-09-04 0:48 ` Mark Oteiza
2017-09-04 14:12 ` Michael Heerdegen
2017-09-05 3:47 ` Mark Oteiza
2017-09-05 15:04 ` Michael Heerdegen
2017-09-06 12:12 ` Michael Heerdegen
2017-09-06 13:06 ` Mark Oteiza
2017-09-06 19:04 ` Michael Heerdegen
2017-09-04 1:13 ` Mark Oteiza
2017-09-05 3:55 ` Mark Oteiza
2017-09-09 0:33 ` Mark Oteiza
2017-09-12 12:39 ` Michael Heerdegen
2017-09-12 13:09 ` Mark Oteiza
2017-09-12 18:44 ` Michael Heerdegen
2017-09-12 20:21 ` Mark Oteiza
2017-09-13 10:16 ` Michael Heerdegen
2017-09-13 11:48 ` Mark Oteiza
2017-09-13 16:46 ` Michael Heerdegen
2017-09-13 16:49 ` Mark Oteiza
2017-09-13 17:05 ` Michael Heerdegen
2017-09-13 17:28 ` Mark Oteiza
2017-09-13 17:49 ` Michael Heerdegen
2017-09-12 12:13 ` Michael Heerdegen
2017-09-12 14:29 ` Mark Oteiza
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170902133604.GA27251@holos.localdomain \
--to=mvoteiza@udel.edu \
--cc=28254@debbugs.gnu.org \
--cc=michael_heerdegen@web.de \
--cc=npostavs@users.sourceforge.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).