Array#nitems and Object#nil?

E

Eliah Hecht

I was working on an assignment for my Algorithms & Data Structures
class when I determined that I had a need to figure out whether an
array was (a) empty, or contained nothing but nils and things that I
want to consider nil, or (b) contained non-nil-esque items. So I was
pleasantly surprised to discover Array#nitems; I thought to myself,
"I'll just make nil-like things respond true to nil?". However, this
didn't seem to work: it appears that Array#nitems checks whether the
array elements == nil, not whether they respond true to nil?. It seems
to me that a nil?-based approach would be more productive, allowing
greater flexibility in terms of what counts as an item.

In case I'm being unclear, if I do
class Thing
def nil?
true
end
end
t = Thing.new
I think it should be the case that [t, nil, 7].nitems returns 1,
instead of 2 as it currently does.

-Eliah.
 
G

George Ogata

Eliah Hecht said:
I was working on an assignment for my Algorithms & Data Structures
class when I determined that I had a need to figure out whether an
array was (a) empty, or contained nothing but nils and things that I
want to consider nil, or (b) contained non-nil-esque items. So I was
pleasantly surprised to discover Array#nitems; I thought to myself,
"I'll just make nil-like things respond true to nil?". However, this
didn't seem to work: it appears that Array#nitems checks whether the
array elements == nil, not whether they respond true to nil?. It seems
to me that a nil?-based approach would be more productive, allowing
greater flexibility in terms of what counts as an item.

In case I'm being unclear, if I do
class Thing
def nil?
true
end
end
t = Thing.new
I think it should be the case that [t, nil, 7].nitems returns 1,
instead of 2 as it currently does.

Well, the documentation says "non-nil items", and that means things
that aren't nil. :) Checking for the exact value nil is quicker than
using a user-defined criterion. #any? and #all? are good for what
you're trying to do.
 
G

George Ogata

Eliah Hecht said:
I was working on an assignment for my Algorithms & Data Structures
class when I determined that I had a need to figure out whether an
array was (a) empty, or contained nothing but nils and things that I
want to consider nil, or (b) contained non-nil-esque items. So I was
pleasantly surprised to discover Array#nitems; I thought to myself,
"I'll just make nil-like things respond true to nil?". However, this
didn't seem to work: it appears that Array#nitems checks whether the
array elements == nil, not whether they respond true to nil?. It seems
to me that a nil?-based approach would be more productive, allowing
greater flexibility in terms of what counts as an item.

In case I'm being unclear, if I do
class Thing
def nil?
true
end
end
t = Thing.new
I think it should be the case that [t, nil, 7].nitems returns 1,
instead of 2 as it currently does.

Well, the documentation says "non-nil items", and that means things
that aren't nil. :) Checking for the exact value nil is quicker than
using a user-defined criterion. #any? and #all? are good for what
you're trying to do.
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Array#nitems and Object#nil?"

|In case I'm being unclear, if I do
|class Thing
| def nil?
| true
| end
|end
|t = Thing.new
|I think it should be the case that [t, nil, 7].nitems returns 1,
|instead of 2 as it currently does.

Nil is a nil is a nil. Defining nil? to be true doesn't make
something nil. It's just a false predicate.

matz.
 
F

Florian Gross

Yukihiro said:
|In case I'm being unclear, if I do
|class Thing
| def nil?
| true
| end
|end
|t = Thing.new
|I think it should be the case that [t, nil, 7].nitems returns 1,
|instead of 2 as it currently does.

Nil is a nil is a nil. Defining nil? to be true doesn't make
something nil. It's just a false predicate.

On another note: Can we perhaps introduce Enumerable#count which returns
the number of elements for which the block is true? I know that we can
do .find_all { ... }.size, but constructing an Array of all elements
just to get its size seems wasteful.
 
E

Eliah Hecht

Nil is a nil is a nil. Defining nil? to be true doesn't make
something nil. It's just a false predicate.

In that case, what's the point of nil?? I'm not sure what you mean by
"It's just a false predicate"; an object that returns true for nil?
doesn't seem to be false-valued. Does nil? do anything in the
language, or is it just meant to be a convient way to check if
something == nil?
-Eliah.
 
D

David A. Black

Hi --

In that case, what's the point of nil?? I'm not sure what you mean by
"It's just a false predicate"; an object that returns true for nil?
doesn't seem to be false-valued. Does nil? do anything in the
language, or is it just meant to be a convient way to check if
something == nil?

nil is an object, so any other object that says "I am the object nil"
(which is what nil? tests for) is giving false information. You can
rig an object to do that, just as you can rig, say, the number 3 to
tell you that it's equal to the number 4, but it's still basically
untrue.


David
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Array#nitems and Object#nil?"

|In that case, what's the point of nil??

It's a convenient (or better looking for someones' eyes) way to check
if something is nil, as you've guessed.

matz.
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Array#nitems and Object#nil?"

|On another note: Can we perhaps introduce Enumerable#count which returns
|the number of elements for which the block is true? I know that we can
|do .find_all { ... }.size, but constructing an Array of all elements
|just to get its size seems wasteful.

enum.inject(0){|i,j| j ? i + 1 : i }

No array construction, bit harder to read than #count.

matz.
 
F

Florian Gross

Yukihiro said:
|On another note: Can we perhaps introduce Enumerable#count which returns
|the number of elements for which the block is true? I know that we can
|do .find_all { ... }.size, but constructing an Array of all elements
|just to get its size seems wasteful.

enum.inject(0){|i,j| j ? i + 1 : i }

No array construction, bit harder to read than #count.

Is it too rare to deserve a built-in short cut?
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Array#nitems and Object#nil?"

|> enum.inject(0){|i,j| j ? i + 1 : i }
|>
|> No array construction, bit harder to read than #count.
|
|Is it too rare to deserve a built-in short cut?

I'm not sure. I've never had need for this operation before in the
10+ years of Ruby programming though.

matz.
 
B

Bertram Scharpf

Hi,

Am Freitag, 18. Mär 2005, 02:12:23 +0900 schrieb Yukihiro Matsumoto:
In message "Re: Array#nitems and Object#nil?"

|> enum.inject(0){|i,j| j ? i + 1 : i }
|>
|> No array construction, bit harder to read than #count.
|
|Is it too rare to deserve a built-in short cut?

I'm not sure. I've never had need for this operation before in the
10+ years of Ruby programming though.

That's 3 years per effective code line, if you add the block
feature to Array#nitems.

I appended it to my demanded and regarded private patch list at
<http://projects.bertram-scharpf.de/tmp/bertram-scharpf-ruby.patch>.

Bertram
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Array#nitems and Object#nil?"

|That's 3 years per effective code line, if you add the block
|feature to Array#nitems.

Today, I happened to re-discover this mail, and found it reasonable.
Array#nitems will be able to take a block to count items specified by
the block (CVS HEAD).

matz.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,588
Members
45,100
Latest member
MelodeeFaj
Top