all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Pascal J. Bourguignon" <pjb@informatimago.com>
To: help-gnu-emacs@gnu.org
Subject: Re: How to mapcar or across a list?
Date: Wed, 15 Jul 2015 22:45:39 +0200	[thread overview]
Message-ID: <871tg9m9os.fsf@kuiper.lan.informatimago.com> (raw)
In-Reply-To: mailman.6968.1436991281.904.help-gnu-emacs@gnu.org

Marcin Borkowski <mbork@mbork.pl> 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


       reply	other threads:[~2015-07-15 20:45 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.6968.1436991281.904.help-gnu-emacs@gnu.org>
2015-07-15 20:45 ` Pascal J. Bourguignon [this message]
2015-07-15 21:02   ` How to mapcar or across a list? Marcin Borkowski
2015-07-15 23:56 ` Barry Margolin
2015-07-15 20:14 Marcin Borkowski
2015-07-15 20:42 ` Rasmus
2015-07-15 20:55   ` Marcin Borkowski
2015-07-15 22:21 ` Emanuel Berg
2015-07-15 22:21 ` John Mastro
     [not found] ` <mailman.6977.1436998965.904.help-gnu-emacs@gnu.org>
2015-07-15 22:28   ` Pascal J. Bourguignon
2015-07-15 22:36     ` Emanuel Berg

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=871tg9m9os.fsf@kuiper.lan.informatimago.com \
    --to=pjb@informatimago.com \
    --cc=help-gnu-emacs@gnu.org \
    /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 external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.