From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Pascal J. Bourguignon" Newsgroups: gmane.emacs.help Subject: Re: How to mapcar or across a list? Date: Wed, 15 Jul 2015 22:45:39 +0200 Organization: Informatimago Message-ID: <871tg9m9os.fsf@kuiper.lan.informatimago.com> References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1436993431 4225 80.91.229.3 (15 Jul 2015 20:50:31 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 15 Jul 2015 20:50:31 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Jul 15 22:50:24 2015 Return-path: Envelope-to: geh-help-gnu-emacs@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 1ZFTdI-0007ke-H7 for geh-help-gnu-emacs@m.gmane.org; Wed, 15 Jul 2015 22:50:24 +0200 Original-Received: from localhost ([::1]:37448 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZFTdH-0006WB-QB for geh-help-gnu-emacs@m.gmane.org; Wed, 15 Jul 2015 16:50:23 -0400 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 127 Original-X-Trace: individual.net 7ADyHFjepD9WgHGCxRmZNg7eFXLWHs3cppi3aj+ROmdFiZG4TS Cancel-Lock: sha1:OTE5NmFlMjA4MzMzZWRmOTJlMjNhZTBiNjZkMTM5MmEwNTE2ZmI0MA== sha1:mTDs83a1vPwCIFLc67Vq4b0ty/U= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Original-Xref: usenet.stanford.edu gnu.emacs.help:213450 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:105737 Archived-At: Marcin Borkowski writes: > Hi there, > > so here's my problem: I have a list of Boolean values, and I want to > `mapcar' an `or' across it (IOW, I want to test whether at least one of > them is true). Of course, (apply #'or my-list) does not work. Of > course, I can (cl-reduce (lambda (x y) (or x y)) my-list) -- but is > there a better method? > > BTW, my-list doesn't really exist: it is a result of `mapcar'ing > a function taking some value and yielding a Boolean value, so bonus > points if the method does not process the whole list. It's too late, lisp is not a lazy language. > (Note: I just noticed that my-list is in fact sorted so that all true > values (if any) will occur at the beginning anyway, so I can just test > the first one. This means that my problem is purely academic, though > still interesting, I guess.) First you want to reduce a list of booleans to a single value, so what makes you thing that mapcar is the right too for that? There's the word "map" in "mapcar", didn't you notice? map reduce x1 --> r1 x1 -\ x2 --> r2 x2 --+-> r x3 --> r3 x3 -/ Also, mapcar allocates a new list of same length as the original list for the result, so it is costly. At least, you could consider mapc. With mapc (or mapcar) you could perform your task with some cl magic: (require 'cl) (setf lexical-binding t) (defun* or-all (list) (mapc (lambda (element) (when element (return-from or-all t))) list) nil) (defun* and-all (list) (mapc (lambda (element) (unless element (return-from and-all nil))) list) t) (or-all '(nil nil nil)) --> nil (or-all '(nil t nil)) --> t (and-all '(t t t)) --> t (and-all '(t nil t)) --> nil But I fail to see how it's better than: (defun* or-all (list) (reduce (lambda (a b) (or a b)) list)) (defun* and-all (list) (reduce (lambda (a b) (and a b)) list)) or than just: (some 'identity '(nil nil nil)) --> nil (some 'identity '(nil t nil)) --> t (some 'identity '(t t t )) --> t (every 'identity '(nil nil nil)) --> nil (every 'identity '(t nil t )) --> nil (every 'identity '(t t t )) --> t There are also the negations: (notevery 'identity '(nil nil nil)) --> t (notevery 'identity '(nil t nil)) --> t (notevery 'identity '(t t t )) --> nil (notany 'identity '(nil nil nil)) --> t (notany 'identity '(t nil t )) --> nil (notany 'identity '(t t t )) --> nil And since your booleans are computed by a function with mapcar, you could avoid computing this function for all the elements of the original list, and you could avoid allocating the temporary list by having some call this function: So instead of: (reduce (lambda (a b) (or a b)) (mapcar 'complex-and-lengthy-predicate list)) use: (some 'complex-and-lengthy-predicate list) For example: (some (lambda (x) (print x) (evenp x)) '(1 2 3 5 7 9 11)) prints: 1 2 --> t Learn more about Common Lisp: http://www.lispworks.com/documentation/HyperSpec/Front/ http://cliki.net/ -- __Pascal Bourguignon__ http://www.informatimago.com/ “The factory of the future will have only two employees, a man and a dog. The man will be there to feed the dog. The dog will be there to keep the man from touching the equipment.” -- Carl Bass CEO Autodesk