[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Enumerable#all? and empty arrays

Andre Nathan

8/9/2006 4:42:00 PM

Hello

I found this to be somewhat amusing:

>> a = []
=> []
>> a.all?
=> true
>> a.all? { |i| i == 10 }
=> true

Is there any reasoning for Enumerable#all? to always return true for
empty arrays?

Best regards,
Andre


6 Answers

Ara.T.Howard

8/9/2006 5:33:00 PM

0

Carlos

8/9/2006 5:49:00 PM

0

Andre Nathan wrote:

> Hello
>
> I found this to be somewhat amusing:
>
>
>>>a = []
>
> => []
>
>>>a.all?
>
> => true
>
>>>a.all? { |i| i == 10 }
>
> => true
>
> Is there any reasoning for Enumerable#all? to always return true for
> empty arrays?

It's what modern logic says... or something like it.

http://en.wikipedia.org/wiki/Square_of_opposition#The_problems_of_existent...

--



Florian Frank

8/9/2006 5:56:00 PM

0

Andre Nathan wrote:
>>> a = []
>>>
> => []
>
>>> a.all?
>>>
> => true
>
>>> a.all? { |i| i == 10 }
>>>
> => true
>
> Is there any reasoning for Enumerable#all? to always return true for
> empty arrays?
>
I think they are modeled after universal and existential quantifiers in
predicate calculus:

a.all?(&b) is true iff for all x in a b[x].

This leads to a.all? being true.

a.any?(&b) is true iff there exists an x in a for which b[x].

This leads to a.any? being false.

--
Florian Frank


Mauricio Fernández

8/9/2006 6:03:00 PM

0

On Thu, Aug 10, 2006 at 01:41:48AM +0900, Andre Nathan wrote:
> Hello
>
> I found this to be somewhat amusing:
>
> >> a = []
> => []
> >> a.all?
> => true
> >> a.all? { |i| i == 10 }
> => true
>
> Is there any reasoning for Enumerable#all? to always return true for
> empty arrays?

a.all?{ ... } == true

can be read as "All elements in a satisfy { ... }", which we can interpret as
"there is no element in a such that { ... } is not satisfied", which is
obviously true if a is empty.

--
Mauricio Fernandez - http://eige... - singular Ruby

Andre Nathan

8/9/2006 6:27:00 PM

0

On Thu, 2006-08-10 at 03:03 +0900, Mauricio Fernandez wrote:
> can be read as "All elements in a satisfy { ... }", which we can interpret as
> "there is no element in a such that { ... } is not satisfied", which is
> obviously true if a is empty.

OK, thank you all for the explanations :)

Andre


Just Another Victim of the Ambient Morality

8/9/2006 11:54:00 PM

0


"Andre Nathan" <andre@digirati.com.br> wrote in message
news:1155141696.24603.43.camel@andre.mz.digirati.com.br...
> Hello
>
> I found this to be somewhat amusing:
>
>>> a = []
> => []
>>> a.all?
> => true
>>> a.all? { |i| i == 10 }
> => true
>
> Is there any reasoning for Enumerable#all? to always return true for
> empty arrays?

It's mathematically consistent.

You may be surprised to hear this but mathematicians are generally
optimists. Thus, if a condition is "vacuously satisfied," then we generally
say the condition is true...