Em Sat, 14 Jul 2012 19:13:32 +0200, Anthony Ferrara <ircmax...@gmail.com>
escreveu:
Why is the last key special? Why not a function to get the first or the
penultimate key?
How would such a function look?
I think you've misread my statement. I was not suggesting a function that
would get an arbitrary key. I was asking what was special about the last
key that would merit a dedicated function. The underlying position was
that there was nothing special about them (but see below), and a dedicated
function function was not warranted.
Of course, there is one aspect where the first and last keys are special
-- if you have some algorithm where you pop or shift the array
successively. But in that case the argument about end() + key() having
side effects is irrelevant because you are changing the array anyway.
How are you changing the array by getting the last (or first) key in this
method? Could you elaborate on that?
I'm afraid I also wasn't clear here. One certainly does not have to change
the array to get the first or the last key. I was only stating that the
circumstances where it would be important to get specifically the first or
the last key are also those where you would want to have side effects. My
argument was that getting specifically the first or last keys is only
important if you are somehow traversing the array by removing the first or
last elements specifically and that requires side effects*.
*This statement requires some qualification. Of course, you don't need
side effects to remove the last element; you could use array_slice() to
implement a sort of array_rest() or array_most() that would remove the
first or the last element. But 1) it is not efficient to use these
patterns in PHP and 2) since you are anyway copying parts you the array,
it is not a problem to change those copies.
--
Gustavo Lopes
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php