help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to mapcar or across a list?


From: Pascal J. Bourguignon
Subject: Re: How to mapcar or across a list?
Date: Thu, 16 Jul 2015 00:28:56 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

Emanuel Berg <embe8573@student.uu.se> writes:

> Marcin Borkowski <mbork@mbork.pl> writes:
>
>> 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?
>
> "Better" I don't know, but how about using your
> new-found skills with the backquote? (`or' itself
> should be used, of course.)
>
>     (setq boolean-list-1 '(t   nil nil nil t   nil nil))
>     (setq boolean-list-2 '(nil nil nil nil nil nil nil))
>
>     (eval `(or ,@boolean-list-1)) ; t
>     (eval `(or ,@boolean-list-2)) ; nil

Nope.  

It's always a bad idea to use eval, because eval evalutes in the nil
environment, not in the local lexical envrionment.

Your example works because those lists are literal, but then you don't
need or to know the answer.

In pratice, translating:

   (flet ((some-complex-predicate (x) …))
     (let ((some-data (generate-some-data)))
        (some (function some-complex-predicate) some-data)))

to:

   (flet ((some-complex-predicate (x) …))
     (let ((some-data (generate-some-data)))
       (eval `(or ,@(mapcar (lambda (data)
                               `(some-complex-predicate ',data))
                            some-data)))))

fails lamentably.  

It is also very slow, since it makes you build a new list,
and it makes you interpret, or worse, compile, some new code!

-- 
__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 via email to

[Prev in Thread] Current Thread [Next in Thread]