From: Erik R.
Subject: (funcall #'or my-list)
Date: 
Message-ID: <1188903750.090497.280550@g4g2000hsf.googlegroups.com>
Greetings, all.  I've got a list of values which are either nil or t
and I want to know "are there any that are non-nil?"  Being a noob, my
first instinct was to write something like the subject line.  But that
obviously won't work because OR is a macro, not a function.

I could, of course, write a function like...

(defun any-non-null (values)
  (cond
    ((null values) nil)
    ((null (first values)) (any-non-null (rest values)))
    (t t)))

But I was wondering if this functionality was built into the language
somehow without calling something as disgusting as (eval (cons 'or my-
list)).  Is there a way to use OR or something like it when I've got
the list in data form?

Just curious.  I don't want to use my own function if it's already
been done in the base common-lisp.

Cheers,
Erik

P.S. If my any-non-null function sucks in any way, let me know.

From: Erik R.
Subject: Re: (funcall #'or my-list)
Date: 
Message-ID: <1188903894.845947.52190@d55g2000hsg.googlegroups.com>
On Sep 4, 1:02 pm, "Erik R." <·············@gmail.com> wrote:
> Greetings, all.  I've got a list of values which are either nil or t
> and I want to know "are there any that are non-nil?"  Being a noob, my
> first instinct was to write something like the subject line.  But that
> obviously won't work because OR is a macro, not a function.
>
> I could, of course, write a function like...
>
> (defun any-non-null (values)
>   (cond
>     ((null values) nil)
>     ((null (first values)) (any-non-null (rest values)))
>     (t t)))
>
> But I was wondering if this functionality was built into the language
> somehow without calling something as disgusting as (eval (cons 'or my-
> list)).  Is there a way to use OR or something like it when I've got
> the list in data form?
>
> Just curious.  I don't want to use my own function if it's already
> been done in the base common-lisp.
>
> Cheers,
> Erik
>
> P.S. If my any-non-null function sucks in any way, let me know.

What about (find-if #'identity my-list)?  That seems to work.
Anything better?
From: Rainer Joswig
Subject: Re: (funcall #'or my-list)
Date: 
Message-ID: <joswig-89F1D8.13183004092007@news-europe.giganews.com>
In article <·······················@d55g2000hsg.googlegroups.com>,
 "Erik R." <·············@gmail.com> wrote:

> On Sep 4, 1:02 pm, "Erik R." <·············@gmail.com> wrote:
> > Greetings, all.  I've got a list of values which are either nil or t
> > and I want to know "are there any that are non-nil?"  Being a noob, my
> > first instinct was to write something like the subject line.  But that
> > obviously won't work because OR is a macro, not a function.
> >
> > I could, of course, write a function like...
> >
> > (defun any-non-null (values)
> >   (cond
> >     ((null values) nil)
> >     ((null (first values)) (any-non-null (rest values)))
> >     (t t)))
> >
> > But I was wondering if this functionality was built into the language
> > somehow without calling something as disgusting as (eval (cons 'or my-
> > list)).  Is there a way to use OR or something like it when I've got
> > the list in data form?
> >
> > Just curious.  I don't want to use my own function if it's already
> > been done in the base common-lisp.
> >
> > Cheers,
> > Erik
> >
> > P.S. If my any-non-null function sucks in any way, let me know.
> 
> What about (find-if #'identity my-list)?  That seems to work.
> Anything better?

(notevery #'null sequence)

-- 
http://lispm.dyndns.org
From: Frode Vatvedt Fjeld
Subject: Re: (funcall #'or my-list)
Date: 
Message-ID: <2hlkbmwuc2.fsf@vserver.cs.uit.no>
"Erik R." <·············@gmail.com> writes:

> What about (find-if #'identity my-list)?  That seems to work.
> Anything better?

For an obfuscation contest, I think  (find-if #'null my-list :key
#'not) would do well :) Otherwise, the function "some" is often
used here. I like to define "true" as an alias for the "identity"
function for such uses.

Also, loop is often useful:

  (loop for x in my-list thereis x)

-- 
Frode Vatvedt Fjeld
From: Steven M. Haflich
Subject: Re: (funcall #'or my-list)
Date: 
Message-ID: <46DE7241.50106@alum.mit.edu>
> What about (find-if #'identity my-list)?  That seems to work.

There are so many ways to do this within the standard language that they 
are hard to count.  The standard function some can be used similarly to 
find-if in the above.  loop can be used in various ways, such as

  (loop for x in my-list thereis x)

or for those who dislike loop there is a map equivalent

  (block nil (map nil (lambda (x) (when x (return t))) my-list))

and for traditionalists there is the ancient dolist

  (dolist (x my-list) (when x (return x)))

The loop and dolist solutions are probably most portably efficient since 
they don't create breakoff lambdas or require lambda elimination.  A 
return that doesn't cross a lambda boundary is generally implemented 
very efficiently.  Many compilers will be able to optimize away even the 
  original find-if example, but the dolist version depends less upon 
compiler cleverness.
From: ·············@planet.nl
Subject: Re: (funcall #'or my-list)
Date: 
Message-ID: <1188904826.492541.324940@50g2000hsm.googlegroups.com>
On Sep 4, 1:02 pm, "Erik R." <·············@gmail.com> wrote:
> Greetings, all.  I've got a list of values which are either nil or t
> and I want to know "are there any that are non-nil?"  Being a noob, my
> first instinct was to write something like the subject line.  But that
> obviously won't work because OR is a macro, not a function.
>
> I could, of course, write a function like...
>
> (defun any-non-null (values)
>   (cond
>     ((null values) nil)
>     ((null (first values)) (any-non-null (rest values)))
>     (t t)))
>
> But I was wondering if this functionality was built into the language
> somehow without calling something as disgusting as (eval (cons 'or my-
> list)).  Is there a way to use OR or something like it when I've got
> the list in data form?
>
> Just curious.  I don't want to use my own function if it's already
> been done in the base common-lisp.
>
> Cheers,
> Erik
>
> P.S. If my any-non-null function sucks in any way, let me know.

The functions EVERY, SOME, NOTEVERY, and NOTANY are helpful in such
cases, for example:

(notevery #'null sequence)

Michiel
From: Erik R.
Subject: Re: (funcall #'or my-list)
Date: 
Message-ID: <1188905607.081166.108120@22g2000hsm.googlegroups.com>
On Sep 4, 1:20 pm, ·············@planet.nl wrote:
> On Sep 4, 1:02 pm, "Erik R." <·············@gmail.com> wrote:
>
>
>
> > Greetings, all.  I've got a list of values which are either nil or t
> > and I want to know "are there any that are non-nil?"  Being a noob, my
> > first instinct was to write something like the subject line.  But that
> > obviously won't work because OR is a macro, not a function.
>
> > I could, of course, write a function like...
>
> > (defun any-non-null (values)
> >   (cond
> >     ((null values) nil)
> >     ((null (first values)) (any-non-null (rest values)))
> >     (t t)))
>
> > But I was wondering if this functionality was built into the language
> > somehow without calling something as disgusting as (eval (cons 'or my-
> > list)).  Is there a way to use OR or something like it when I've got
> > the list in data form?
>
> > Just curious.  I don't want to use my own function if it's already
> > been done in the base common-lisp.
>
> > Cheers,
> > Erik
>
> > P.S. If my any-non-null function sucks in any way, let me know.
>
> The functions EVERY, SOME, NOTEVERY, and NOTANY are helpful in such
> cases, for example:
>
> (notevery #'null sequence)
>
> Michiel

Excellent!  Thanks guys.  I knew such a basic operation had a ready-
made solution.

-Erik
From: Klaus Schilling
Subject: Re: (funcall #'or my-list)
Date: 
Message-ID: <873axuzidg.fsf@web.de>
"Erik R." <·············@gmail.com> writes:

> On Sep 4, 1:20 pm, ·············@planet.nl wrote:
>> On Sep 4, 1:02 pm, "Erik R." <·············@gmail.com> wrote:
>>
>>
>>
>> > Greetings, all.  I've got a list of values which are either nil or t
>> > and I want to know "are there any that are non-nil?"  Being a noob, my
>> > first instinct was to write something like the subject line.  But that
>> > obviously won't work because OR is a macro, not a function.
>>
>> > I could, of course, write a function like...
>>
>> > (defun any-non-null (values)
>> >   (cond
>> >     ((null values) nil)
>> >     ((null (first values)) (any-non-null (rest values)))
>> >     (t t)))
>>
>> > But I was wondering if this functionality was built into the language
>> > somehow without calling something as disgusting as (eval (cons 'or my-
>> > list)).  Is there a way to use OR or something like it when I've got
>> > the list in data form?
>>
>> > Just curious.  I don't want to use my own function if it's already
>> > been done in the base common-lisp.
>>
>> > Cheers,
>> > Erik
>>
>> > P.S. If my any-non-null function sucks in any way, let me know.
>>
>> The functions EVERY, SOME, NOTEVERY, and NOTANY are helpful in such
>> cases, for example:
>>
>> (notevery #'null sequence)

but they don't shortcircuit evaluation, as the special forms 
"and" and "or" do

Klaus Schilling
From: Vebjorn Ljosa
Subject: Re: (funcall #'or my-list)
Date: 
Message-ID: <1188919259.676304.280440@o80g2000hse.googlegroups.com>
On Sep 4, 9:10 am, Klaus Schilling <···············@web.de> wrote:
>
> >> The functions EVERY, SOME, NOTEVERY, and NOTANY are helpful in such
> >> cases, for example:
>
> >> (notevery #'null sequence)
>
> but they don't shortcircuit evaluation, as the special forms
> "and" and "or" do

Yes, they do.

    "Predicate is first applied to the elements with index 0 in each
of the sequences, and possibly then to the elements with index 1, and
so on, until a termination criterion is met or the end of the shortest
of the sequences is reached."

http://www.lisp.org/HyperSpec/Body/fun_everycm_s_erycm_notany.html

Vebjorn
From: Klaus Schilling
Subject: Re: (funcall #'or my-list)
Date: 
Message-ID: <878x7mxpoy.fsf@web.de>
Vebjorn Ljosa <·······@ljosa.com> writes:

> On Sep 4, 9:10 am, Klaus Schilling <···············@web.de> wrote:
>>
>> >> The functions EVERY, SOME, NOTEVERY, and NOTANY are helpful in such
>> >> cases, for example:
>>
>> >> (notevery #'null sequence)
>>
>> but they don't shortcircuit evaluation, as the special forms
>> "and" and "or" do
>
> Yes, they do.
>
>     "Predicate is first applied to the elements with index 0 in each
> of the sequences, and possibly then to the elements with index 1, and
> so on, until a termination criterion is met or the end of the shortest
> of the sequences is reached."
>
> http://www.lisp.org/HyperSpec/Body/fun_everycm_s_erycm_notany.html
>

Then the following is a bug:?


>(catch 'oops
	(some #'string (list "qwer" (throw 'oops "This shouldnt be seen"))))

"This shouldnt be seen"


      Klaus Schilling
From: Vebjorn Ljosa
Subject: Re: (funcall #'or my-list)
Date: 
Message-ID: <1188930790.536970.257730@r34g2000hsd.googlegroups.com>
On Sep 4, 2:15 pm, Klaus Schilling <···············@web.de> wrote:
>
> Then the following is a bug:?
>
> >(catch 'oops
>
>         (some #'string (list "qwer" (throw 'oops "This shouldnt be seen"))))
>
> "This shouldnt be seen"

No, it's not a bug.  SOME and friends are functions, so the arguments
are evaluated before SOME is applied to them.  The consecutive
application of the predicate to the lists is short-circuited, however:

    (some #'funcall (list (lambda () t) (lambda () (error "Foo"))))
    => T

I see now that I was a little too quick to repond to your comment.
Being a function, SOME indeed cannot control whether its arguments are
_evaluated_, so you were correct.

But why would you want a (macro) version of SOME that short circuits
the evaluation of its arguments?  Isn't OR all that is needed?

Vebjorn
From: Klaus Schilling
Subject: Re: (funcall #'or my-list)
Date: 
Message-ID: <87wsv6tf1h.fsf@web.de>
Vebjorn Ljosa <·······@ljosa.com> writes:

> On Sep 4, 2:15 pm, Klaus Schilling <···············@web.de> wrote:
>>
>> Then the following is a bug:?
>>
>> >(catch 'oops
>>
>>         (some #'string (list "qwer" (throw 'oops "This shouldnt be seen"))))
>>
>> "This shouldnt be seen"
>
> No, it's not a bug.  SOME and friends are functions, so the arguments
> are evaluated before SOME is applied to them.  The consecutive
> application of the predicate to the lists is short-circuited, however:
>
>     (some #'funcall (list (lambda () t) (lambda () (error "Foo"))))
>     => T
>
> I see now that I was a little too quick to repond to your comment.

or I wasn't clear enough what I meant with 'short-circuiting' :>

> Being a function, SOME indeed cannot control whether its arguments are
> _evaluated_, so you were correct.
>
> But why would you want a (macro) version of SOME that short circuits
> the evaluation of its arguments?  Isn't OR all that is needed?
>
true in the batch case where (as above) the list is known in advance.
Alas, if the list is only determined at runtime, and not even its 
length is known at compile time, this doesn't work out the straight way.

Klaus Schilling
From: Matthias Benkard
Subject: Re: (funcall #'or my-list)
Date: 
Message-ID: <1188941806.857239.93620@r29g2000hsg.googlegroups.com>
Klaus Schilling schrieb:
> true in the batch case where (as above) the list is known in advance.
> Alas, if the list is only determined at runtime, and not even its
> length is known at compile time, this doesn't work out the straight way.

If you generate code at run-time, you need to use EVAL (or COMPILE or
something, but that doesn't matter) in order to evaluate it anyway.
In that case, you can use OR again.  No problem there.

~ Matthias
From: Matthias Benkard
Subject: Re: (funcall #'or my-list)
Date: 
Message-ID: <1188930837.640452.262920@r34g2000hsd.googlegroups.com>
Klaus Schilling schrieb:
> Then the following is a bug:?
>
> >(catch 'oops
> 	(some #'string (list "qwer" (throw 'oops "This shouldnt be seen"))))
> "This shouldnt be seen"

Not a bug in SOME in any case, because it isn't even called.

The OP asked for a function that was equivalent to applying OR to a
list of boolean values -- which only makes sense with the list already
existing.

~ Matthias
From: Ari Johnson
Subject: Re: (funcall #'or my-list)
Date: 
Message-ID: <m24pianuhn.fsf@nibbler.theari.com>
Klaus Schilling <···············@web.de> writes:

> Vebjorn Ljosa <·······@ljosa.com> writes:
>
>> On Sep 4, 9:10 am, Klaus Schilling <···············@web.de> wrote:
>>>
>>> >> The functions EVERY, SOME, NOTEVERY, and NOTANY are helpful in such
>>> >> cases, for example:
>>>
>>> >> (notevery #'null sequence)
>>>
>>> but they don't shortcircuit evaluation, as the special forms
>>> "and" and "or" do
>>
>> Yes, they do.
>>
>>     "Predicate is first applied to the elements with index 0 in each
>> of the sequences, and possibly then to the elements with index 1, and
>> so on, until a termination criterion is met or the end of the shortest
>> of the sequences is reached."
>>
>> http://www.lisp.org/HyperSpec/Body/fun_everycm_s_erycm_notany.html
>>
>
> Then the following is a bug:?
>
>
>>(catch 'oops
> 	(some #'string (list "qwer" (throw 'oops "This shouldnt be seen"))))
>
> "This shouldnt be seen"

Let's go through the evaluation in the order it happens:

CATCH is a special operator, with its own evaluation semantics.  THROW
is also a special operator.  SOME and LIST are functions.  Presumably,
you mean to use STRINGP, which is also a function.

CATCH will evaluate the 'OOPS as OOPS and set it up as a tag for the
ensuing THROW.  Next, it evaluates the form
  (SOME #'STRINGP (LIST "qwer" (THROW 'OOPS "This shouldnt be seen")))

Because SOME is a function, its arguments are evaluated before it is
run.  Those arguments are:
  1. #'STRINGP
  2. (LIST "qwer" (THROW 'OOPS "This shouldnt be seen"))

#'STRINGP evaluates by way of (FUNCTION STRINGP) to
(symbol-function 'stringp).

LIST is a function, so its arguments are also evaluated before it is
run.  Those arguments are:
  1. "qwer"
  2. (THROW 'OOPS "This shouldnt be seen")

"qwer" evaluates to itself.

(THROW 'OOPS "This shouldnt be seen"), when evaluated, throws control
back to the matching CATCH.

Because of the THROW, neither LIST, SOME, or STRINGP ever gets run.
You can also see this by trying this version:

(catch 'oops
  (some #'(lambda (x) (format t "Checking ~A~%" x) (stringp x))
        (list "qwer" (throw 'oops "This shouldnt be seen"))))
From: Kent M Pitman
Subject: Re: (funcall #'or my-list)
Date: 
Message-ID: <usl5u9j0u.fsf@nhplace.com>
Klaus Schilling <···············@web.de> writes:

> but they don't shortcircuit evaluation, as the special forms 
> "and" and "or" do

The problem is that in the functional form, the very definition of
APPLY is that the arguments are received evaluated.  So there is no
_opportunity_ to do that shortcutting.  The moment you've said it's
a function, you open yourself to:

 (defun or* (&rest things*)
   (some #'identity (apply #'list* things*)))

 (defvar *args* '(nil 'nil t 't))

 (or* *args*) => (QUOTE NIL) ; a true value, of course

What would it even mean to shortcut evaluation there?  There's no
evaluation planned.  Sure, *args* is going to evaluate because 
arguments to all functions evaluate when the function call happens
normally, but you can bypass even that.

 (apply #'or* *args*) => (QUOTE NIL) ; same true value. heh.

Now you're not even evaluating the arguments to OR*, you're evaluating
the arguments to APPLY.  The arguments to OR* are as constant as you
can make them.  There is simply nothing to shortcut.

So yes, it's true that it doesn't do this, but that's just a forced
implication of the question (at most, a constraint on the answer), not
a consequence of the answer except in the sense that the answer is a
consequence of the question and one might allege some bizarre form of
transitivity to no particularly good end.
From: Peder O. Klingenberg
Subject: Re: (funcall #'or my-list)
Date: 
Message-ID: <kshcmad6kl.fsf@beto.netfonds.no>
"Erik R." <·············@gmail.com> writes:

> Is there a way to use OR or something like it when I've got
> the list in data form?

#'SOME

(and EVERY as the equivalent of AND)

...Peder...
-- 
I wish a new life awaited _me_ in some off-world colony.
From: Ken Tilton
Subject: Re: (funcall #'or my-list)
Date: 
Message-ID: <WR4Ei.1243$Tu5.121@newsfe12.lga>
Peder O. Klingenberg wrote:
> "Erik R." <·············@gmail.com> writes:
> 
> 
>>Is there a way to use OR or something like it when I've got
>>the list in data form?
> 
> 
> #'SOME

The OP does not need SOME, they need what they found, find-if 'identity. 
Some is for when the function will chomp on the list values and produce 
SOMEthing else.

kt

-- 
http://www.theoryyalgebra.com/

"We are what we pretend to be." -Kurt Vonnegut
From: Rob St. Amant
Subject: Re: (funcall #'or my-list)
Date: 
Message-ID: <fbrp3s$ic6$2@blackhelicopter.databasix.com>
Ken Tilton <···········@optonline.net> writes:

> Peder O. Klingenberg wrote:
>> "Erik R." <·············@gmail.com> writes:
>>
>>
>>>Is there a way to use OR or something like it when I've got
>>>the list in data form?
>>
>>
>> #'SOME
>
> The OP does not need SOME, they need what they found, find-if
> identity. Some is for when the function will chomp on the list values
> and produce SOMEthing else.

I've lately found a use for a function that calls a predicate on each
element of a list and returns all non-null results.  (I bring this up
here because it has a SOME-like flavor).

(defun all-such-that (predicate list &key key)
  "Return non-nil PREDICATE results for elements in LIST."
  (loop for elt in list
     as value = (if key
                    (funcall predicate (funcall key elt))
                    (funcall predicate elt))
     when value
     collect value))

Does anyone else use a function like this?  If so, what do you call
it?  And is there a better implementation than the code above?
From: Ken Tilton
Subject: Re: (funcall #'or my-list)
Date: 
Message-ID: <b4eEi.17$mM1.12@newsfe12.lga>
Rob St. Amant wrote:
> Ken Tilton <···········@optonline.net> writes:
> 
> 
>>Peder O. Klingenberg wrote:
>>
>>>"Erik R." <·············@gmail.com> writes:
>>>
>>>
>>>
>>>>Is there a way to use OR or something like it when I've got
>>>>the list in data form?
>>>
>>>
>>>#'SOME
>>
>>The OP does not need SOME, they need what they found, find-if
>>identity. Some is for when the function will chomp on the list values
>>and produce SOMEthing else.
> 
> 
> I've lately found a use for a function that calls a predicate on each
> element of a list and returns all non-null results.  (I bring this up
> here because it has a SOME-like flavor).
> 
> (defun all-such-that (predicate list &key key)
>   "Return non-nil PREDICATE results for elements in LIST."
>   (loop for elt in list
>      as value = (if key
>                     (funcall predicate (funcall key elt))
>                     (funcall predicate elt))
>      when value
>      collect value))
> 
> Does anyone else use a function like this?  If so, what do you call
> it?  

(defun collect (x list &key (key 'identity) (test 'eql))
   (loop for i in list
         when (funcall test x (funcall key i))
         collect i))

(defun collect-if (test list)
   (loop for i in list
         when (funcall test i)
         collect i))

kt

-- 
http://www.theoryyalgebra.com/

"We are what we pretend to be." -Kurt Vonnegut
From: Rob St. Amant
Subject: Re: (funcall #'or my-list)
Date: 
Message-ID: <fbs9rc$sfn$1@blackhelicopter.databasix.com>
·······@ncsu.edu (Rob St. Amant) writes:

> Ken Tilton <···········@optonline.net> writes:
>
>> Rob St. Amant wrote:
>>> Ken Tilton <···········@optonline.net> writes:
>>>
>>>
>>>>Peder O. Klingenberg wrote:
>>>>
>>>>>"Erik R." <·············@gmail.com> writes:
>>>>>
>>>>>
>>>>>
>>>>>>Is there a way to use OR or something like it when I've got
>>>>>>the list in data form?
>>>>>
>>>>>
>>>>>#'SOME
>>>>
>>>>The OP does not need SOME, they need what they found, find-if
>>>>identity. Some is for when the function will chomp on the list values
>>>>and produce SOMEthing else.
>>>
>>>
>>> I've lately found a use for a function that calls a predicate on each
>>> element of a list and returns all non-null results.  (I bring this up
>>> here because it has a SOME-like flavor).
>>>
>>> (defun all-such-that (predicate list &key key)
>>>   "Return non-nil PREDICATE results for elements in LIST."
>>>   (loop for elt in list
>>>      as value = (if key
>>>                     (funcall predicate (funcall key elt))
>>>                     (funcall predicate elt))
>>>      when value
>>>      collect value))
>>>
>>> Does anyone else use a function like this?  If so, what do you call
>>> it?  
>>
>> (defun collect (x list &key (key 'identity) (test 'eql))
>>   (loop for i in list
>>         when (funcall test x (funcall key i))
>>         collect i))
>>
>> (defun collect-if (test list)
>>   (loop for i in list
>>         when (funcall test i)
>>         collect i))
>
> Thanks; that's more idiomatic than what I have.

Oops, I replied too soon.  The point of the functions is to collect
results of the predicate rather than the elements themselves, for
which remove-if and friends are appropriate.  Perhaps this revision:

(defun collect (x list &key (key 'identity) (test 'eql))
  (loop for i in list
        when (funcall test x (funcall key i))
        collect it)) ; it rather than i

(defun collect-if (test list)
  (loop for i in list
        when (funcall test i)
        collect it)) ; it rather than i
From: Rob Warnock
Subject: Re: (funcall #'or my-list)
Date: 
Message-ID: <RYCdnVJAHqpUbXzbnZ2dnUVZ_orinZ2d@speakeasy.net>
Rob St. Amant <·······@ncsu.edu> wrote:
+---------------
| Oops, I replied too soon.  The point of the functions is to collect
| results of the predicate rather than the elements themselves, for
| which remove-if and friends are appropriate.  Perhaps this revision:
| 
| (defun collect (x list &key (key 'identity) (test 'eql))
|   (loop for i in list
|         when (funcall test x (funcall key i))
|         collect it)) ; it rather than i
| 
| (defun collect-if (test list)
|   (loop for i in list
|         when (funcall test i)
|         collect it)) ; it rather than i
+---------------

What's wrong with just (remove-if #'null (mapcar PREDICATE SEQUENCE))?


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Rob St. Amant
Subject: Re: (funcall #'or my-list)
Date: 
Message-ID: <fbsvee$l7r$1@blackhelicopter.databasix.com>
····@rpw3.org (Rob Warnock) writes:

> Rob St. Amant <·······@ncsu.edu> wrote:
> +---------------
> | Oops, I replied too soon.  The point of the functions is to collect
> | results of the predicate rather than the elements themselves, for
> | which remove-if and friends are appropriate.  Perhaps this revision:
> | 
> | (defun collect (x list &key (key 'identity) (test 'eql))
> |   (loop for i in list
> |         when (funcall test x (funcall key i))
> |         collect it)) ; it rather than i
> | 
> | (defun collect-if (test list)
> |   (loop for i in list
> |         when (funcall test i)
> |         collect it)) ; it rather than i
> +---------------
>
> What's wrong with just (remove-if #'null (mapcar PREDICATE SEQUENCE))?

That would be nice and concise.  I think I've been caught up in
premature optimization; I don't actually know whether the lists will
be very long at all.  Thanks.
From: Drew Crampsie
Subject: Re: (funcall #'or my-list)
Date: 
Message-ID: <46e198fb$0$16334$88260bb3@free.teranews.com>
On Fri, 07 Sep 2007 11:23:52 -0400, Ken Tilton wrote:

> Rob St. Amant wrote:
>> Ken Tilton <···········@optonline.net> writes:
>> 
>> 
>>>Peder O. Klingenberg wrote:
>>>
>>>>"Erik R." <·············@gmail.com> writes:
>>>>
>>>>
>>>>
>>>>>Is there a way to use OR or something like it when I've got
>>>>>the list in data form?
>>>>
>>>>
>>>>#'SOME
>>>
>>>The OP does not need SOME, they need what they found, find-if
>>>identity. Some is for when the function will chomp on the list values
>>>and produce SOMEthing else.
>> 
>> 
>> I've lately found a use for a function that calls a predicate on each
>> element of a list and returns all non-null results.  (I bring this up
>> here because it has a SOME-like flavor).
>> 
>> (defun all-such-that (predicate list &key key)
>>   "Return non-nil PREDICATE results for elements in LIST."
>>   (loop for elt in list
>>      as value = (if key
>>                     (funcall predicate (funcall key elt))
>>                     (funcall predicate elt))
>>      when value
>>      collect value))
>> 
>> Does anyone else use a function like this?  If so, what do you call
>> it?  
> 
> (defun collect (x list &key (key 'identity) (test 'eql))
>    (loop for i in list
>          when (funcall test x (funcall key i))
>          collect i))

 (remove-if-not (lambda (item)
                 (funcall test x item)) SEQUENCE)
                         
> 
> (defun collect-if (test list)
>    (loop for i in list
>          when (funcall test i)
>          collect i))
> 
> kt
>

(remove-if-not test SEQUENCE)

drewc

-- 
Posted via a free Usenet account from http://www.teranews.com
From: Matthias Buelow
Subject: Re: (funcall #'or my-list)
Date: 
Message-ID: <5k4t93F24s7cU1@mid.dfncis.de>
Erik R. wrote:
> Greetings, all.  I've got a list of values which are either nil or t
> and I want to know "are there any that are non-nil?" 

(member t list) ?

Or

(member-if-not #'null list)

if it contains "true" values other than t.
From: Ken Tilton
Subject: Re: (funcall #'or my-list)
Date: 
Message-ID: <yS4Ei.1244$Tu5.854@newsfe12.lga>
Matthias Buelow wrote:
> Erik R. wrote:
> 
>>Greetings, all.  I've got a list of values which are either nil or t
>>and I want to know "are there any that are non-nil?" 
> 
> 
> (member t list) ?

Only if you did not notice that the noob is misspecifying their requirement.

kt

-- 
http://www.theoryyalgebra.com/

"We are what we pretend to be." -Kurt Vonnegut
From: Matthias Buelow
Subject: Re: (funcall #'or my-list)
Date: 
Message-ID: <5ke4k3F3b5hpU1@mid.dfncis.de>
Ken Tilton wrote:

>> (member t list) ?
> Only if you did not notice that the noob is misspecifying their
> requirement.

I don't do DWIM.
From: Ken Tilton
Subject: Re: (funcall #'or my-list)
Date: 
Message-ID: <c_4Ei.1245$Tu5.248@newsfe12.lga>
Erik R. wrote:
> Greetings, all.  I've got a list of values which are either nil or t
> and I want to know "are there any that are non-nil?"  Being a noob, my
> first instinct was to write something like the subject line.  But that
> obviously won't work because OR is a macro, not a function.
> 
> I could, of course, write a function like...
> 
> (defun any-non-null (values)...

You just changed the spec from nil or t to nil or non-nil.

find-if 'identity was my thought. I am pretty religious about never 
using negatives, but find-if-not 'null almost seems clearer.

Some lisps have a 'true function, I guess just to be more readable than 
find-if 'identity.

kt

-- 
http://www.theoryyalgebra.com/

"We are what we pretend to be." -Kurt Vonnegut
From: Duane Rettig
Subject: Re: (funcall #'or my-list)
Date: 
Message-ID: <o0bqcf9dqu.fsf@gemini.franz.com>
[OT]

Ken Tilton <···········@optonline.net> writes:

> find-if 'identity was my thought. I am pretty religious about never
> using negatives, but find-if-not 'null almost seems clearer.

So it wasn't twice that you didn't not use (double) negatives in this
last statement?

-- 
Duane Rettig    ·····@franz.com    Franz Inc.  http://www.franz.com/
555 12th St., Suite 1450               http://www.555citycenter.com/
Oakland, Ca. 94607        Phone: (510) 452-2000; Fax: (510) 452-0182   
From: Ken Tilton
Subject: Re: (funcall #'or my-list)
Date: 
Message-ID: <fQ9Ei.1278$Tu5.922@newsfe12.lga>
Duane Rettig wrote:
> [OT]
> 
> Ken Tilton <···········@optonline.net> writes:
> 
> 
>>find-if 'identity was my thought. I am pretty religious about never
>>using negatives, but find-if-not 'null almost seems clearer.
> 
> 
> So it wasn't twice that you didn't not use (double) negatives in this
> last statement?
> 

I was hoping someone would enjoy that.

kt

-- 
http://www.theoryyalgebra.com/

"We are what we pretend to be." -Kurt Vonnegut