(resending because of broken formatting)
Hi there,
I apologise for my previous email. It was a disorganised mess that didn't really
make the case for that function very well. So here's a proper proposal (perhaps
too proper?).
Introduction ------------
I am proposing a function, named array_pick(), which takes a single (array)
argument, and returns a random value from it.
Implementation --------------
https://github.com/php/php-src/pull/142
Specification -------------
mixed array_pick(array $array);
The function returns the value of one random array element from $array. Should
said array be empty, then it should return NULL.
Rationale ---------
- array_rand exists, however it only gets a random key, not a random value. This
is the logical counterpart for API completeness - array_pick is more convenient than $a[array_rand($a)], especially for these
cases:- short array syntax + array_pick, e.g. some_func(array_pick(['foo.png',
'bar.jpg', 'foobar.gif'])) vs $a = ['foo.png, 'bar.jpg', 'foobar.gif'];
some_func($a[array_rand($a)]); - where using long names or nested arrays, e.g.
some_func(array_pick($foo['bar']['foobar']));
- short array syntax + array_pick, e.g. some_func(array_pick(['foo.png',
- array_pick is less wasteful than shuffling an array and taking the first
element - $a[array_rand($a)] issuse a NOTICE from an undefined index if the array is
empty, but array_pick() just returns null, which is more convenient - I need this function myself quite often. It exists in two other languages I
use that I can think of off the top of my head (Python, Game Maker Language)
Objections ----------
- PHP has too many functions
- Yes, but that doesn't mean we can't add more. Otherwise we can't improve
things and move forward.
- Yes, but that doesn't mean we can't add more. Otherwise we can't improve
- This can be easily implemented in userland code/this is too frivolous to
warrant addition- This is true, however similarly to a lot of standard library functionality,
it is used frequently enough that it is silly to have to rewrite it every time
it is needed - It is also the logical counterpart to
array_rand()
. One for a key, one for a
value.
- This is true, however similarly to a lot of standard library functionality,
- The name isn't logical
- Whilst you could argue that array_rand_key is a better name, I have chosen
array_pick because it is shorter, and has a similar name to similar functions in
Python (random.choice) and GML (choose) - However, this could be changed.
- Whilst you could argue that array_rand_key is a better name, I have chosen
Thoughts?
--
Andrew Faulds
http://ajf.me/
Andrew,
(resending because of broken formatting)
Hi there,
I apologise for my previous email. It was a disorganised mess that didn't
really
make the case for that function very well. So here's a proper proposal
(perhaps
too proper?).Introduction ------------
I am proposing a function, named array_pick(), which takes a single (array)
argument, and returns a random value from it.Implementation --------------
https://github.com/php/php-src/pull/142
Specification -------------
mixed array_pick(array $array);
The function returns the value of one random array element from $array.
Should
said array be empty, then it should return NULL.Rationale ---------
- array_rand exists, however it only gets a random key, not a random
value. This
is the logical counterpart for API completeness- array_pick is more convenient than $a[array_rand($a)], especially for
these
cases:
- short array syntax + array_pick, e.g. some_func(array_pick(['foo.png',
'bar.jpg', 'foobar.gif'])) vs $a = ['foo.png, 'bar.jpg', 'foobar.gif'];
some_func($a[array_rand($a)]);- where using long names or nested arrays, e.g.
some_func(array_pick($foo['bar']['foobar']));- array_pick is less wasteful than shuffling an array and taking the first
element- $a[array_rand($a)] issuse a NOTICE from an undefined index if the array
is
empty, but array_pick() just returns null, which is more convenient- I need this function myself quite often. It exists in two other
languages I
use that I can think of off the top of my head (Python, Game Maker
Language)Objections ----------
- PHP has too many functions
- Yes, but that doesn't mean we can't add more. Otherwise we can't
improve
things and move forward.
Broken Window Theory. http://en.wikipedia.org/wiki/Broken_windows_theory
Saying things are already broken is never a valid justification for
breaking things further.
- This can be easily implemented in userland code/this is too frivolous to
warrant addition
- This is true, however similarly to a lot of standard library
functionality,
it is used frequently enough that it is silly to have to rewrite it every
time
it is needed
It's a one-liner. It's not like it's non-trivial, or there are weird
edge-cases, or security implications. It's a one line implementation:
function array_pick(array $array) {
return empty($array) ? null : $array[array_rand($array)];
}
- It is also the logical counterpart to
array_rand()
. One for a key, one
for a
value.
Again, since it's so trivial to implement, why is it needed? Symmetry is a
decent goal, but justified symmetry. Not just symmetry for the sake of it...
- The name isn't logical
- Whilst you could argue that array_rand_key is a better name, I have
chosen
array_pick because it is shorter, and has a similar name to similar
functions in
Python (random.choice) and GML (choose)
Actually, I think that array_pick() is a poor name. Python's name makes
sense, because it's bound to the random namespace. But being bound to the
array namespace indicates a different thing. I would argue that
array_random_value() would be a better choice (or any one of a number of
names)...
Thoughts?
Shared.
Thanks,
Anthony
Andrew,
(resending because of broken formatting)
Hi there,
I apologise for my previous email. It was a disorganised mess that didn't
really
make the case for that function very well. So here's a proper proposal
(perhaps
too proper?).Introduction ------------
I am proposing a function, named array_pick(), which takes a single (array)
argument, and returns a random value from it.Implementation --------------
https://github.com/php/php-src/pull/142
Specification -------------
mixed array_pick(array $array);
The function returns the value of one random array element from $array.
Should
said array be empty, then it should return NULL.Rationale ---------
- array_rand exists, however it only gets a random key, not a random
value. This
is the logical counterpart for API completeness- array_pick is more convenient than $a[array_rand($a)], especially for
these
cases:- short array syntax + array_pick, e.g. some_func(array_pick(['foo.png',
'bar.jpg', 'foobar.gif'])) vs $a = ['foo.png, 'bar.jpg', 'foobar.gif'];
some_func($a[array_rand($a)]);- where using long names or nested arrays, e.g.
some_func(array_pick($foo['bar']['foobar']));- array_pick is less wasteful than shuffling an array and taking the first
element- $a[array_rand($a)] issuse a NOTICE from an undefined index if the array
is
empty, but array_pick() just returns null, which is more convenient- I need this function myself quite often. It exists in two other
languages I
use that I can think of off the top of my head (Python, Game Maker
Language)Objections ----------
- PHP has too many functions
- Yes, but that doesn't mean we can't add more. Otherwise we can't
improve
things and move forward.Broken Window Theory. http://en.wikipedia.org/wiki/Broken_windows_theory
Saying things are already broken is never a valid justification for
breaking things further.
- This can be easily implemented in userland code/this is too frivolous to
warrant addition- This is true, however similarly to a lot of standard library
functionality,
it is used frequently enough that it is silly to have to rewrite it every
time
it is neededIt's a one-liner. It's not like it's non-trivial, or there are weird
edge-cases, or security implications. It's a one line implementation:function array_pick(array $array) {
return empty($array) ? null : $array[array_rand($array)];
}
- It is also the logical counterpart to
array_rand()
. One for a key, one
for a
value.Again, since it's so trivial to implement, why is it needed? Symmetry is a
decent goal, but justified symmetry. Not just symmetry for the sake of it...
- The name isn't logical
- Whilst you could argue that array_rand_key is a better name, I have
chosen
array_pick because it is shorter, and has a similar name to similar
functions in
Python (random.choice) and GML (choose)Actually, I think that array_pick() is a poor name. Python's name makes
sense, because it's bound to the random namespace. But being bound to the
array namespace indicates a different thing. I would argue that
array_random_value() would be a better choice (or any one of a number of
names)...
Thoughts?
Shared.
Thanks,
Anthony
It's a one-liner. It's not like it's non-trivial, or there are weird
edge-cases, or security implications. It's a one line implementation:function array_pick(array $array) {
return empty($array) ? null : $array[array_rand($array)];
}
array_rand()
is also trivial and one length long function, but it exists in the language and it is really useful.
I don't think that one should think about "how much line it takes to write it", you should think about "does many people use this function quite often in order to make it part of the language".
Personally, I think that this function is great, and when I read for the first time about array_rand()
I thought it'll returns a value, after I realized it returns a key, I looked for a value pair and surprised to see that there's no value pair.
Why bloat the core library of functions when you can perform: $randomValue
= $array[array_rand($array)]; Am I missing the point?
I don't think there is a real need for this personally it's just language
bloat. Look at Java (I know it's very different) but it provides a huge
amount of "built in" functions and is not any better off for it.
Sam
On Mon, Jul 23, 2012 at 5:12 PM, Yahav Gindi Bar g.b.yahav@gmail.comwrote:
Andrew,
(resending because of broken formatting)
Hi there,
I apologise for my previous email. It was a disorganised mess that
didn't
really
make the case for that function very well. So here's a proper proposal
(perhaps
too proper?).Introduction ------------
I am proposing a function, named array_pick(), which takes a single
(array)
argument, and returns a random value from it.Implementation --------------
https://github.com/php/php-src/pull/142
Specification -------------
mixed array_pick(array $array);
The function returns the value of one random array element from $array.
Should
said array be empty, then it should return NULL.Rationale ---------
- array_rand exists, however it only gets a random key, not a random
value. This
is the logical counterpart for API completeness- array_pick is more convenient than $a[array_rand($a)], especially for
these
cases:- short array syntax + array_pick, e.g.
some_func(array_pick(['foo.png',
'bar.jpg', 'foobar.gif'])) vs $a = ['foo.png, 'bar.jpg', 'foobar.gif'];
some_func($a[array_rand($a)]);- where using long names or nested arrays, e.g.
some_func(array_pick($foo['bar']['foobar']));- array_pick is less wasteful than shuffling an array and taking the
first
element- $a[array_rand($a)] issuse a NOTICE from an undefined index if the
array
is
empty, but array_pick() just returns null, which is more convenient- I need this function myself quite often. It exists in two other
languages I
use that I can think of off the top of my head (Python, Game Maker
Language)Objections ----------
- PHP has too many functions
- Yes, but that doesn't mean we can't add more. Otherwise we can't
improve
things and move forward.Broken Window Theory. http://en.wikipedia.org/wiki/Broken_windows_theory
Saying things are already broken is never a valid justification for
breaking things further.
- This can be easily implemented in userland code/this is too frivolous
to
warrant addition- This is true, however similarly to a lot of standard library
functionality,
it is used frequently enough that it is silly to have to rewrite it
every
time
it is neededIt's a one-liner. It's not like it's non-trivial, or there are weird
edge-cases, or security implications. It's a one line implementation:function array_pick(array $array) {
return empty($array) ? null : $array[array_rand($array)];
}
- It is also the logical counterpart to
array_rand()
. One for a key,
one
for a
value.Again, since it's so trivial to implement, why is it needed? Symmetry is
a
decent goal, but justified symmetry. Not just symmetry for the sake of
it...
- The name isn't logical
- Whilst you could argue that array_rand_key is a better name, I have
chosen
array_pick because it is shorter, and has a similar name to similar
functions in
Python (random.choice) and GML (choose)Actually, I think that array_pick() is a poor name. Python's name makes
sense, because it's bound to the random namespace. But being bound to the
array namespace indicates a different thing. I would argue that
array_random_value() would be a better choice (or any one of a number of
names)...
Thoughts?
Shared.
Thanks,
Anthony
It's a one-liner. It's not like it's non-trivial, or there are weird
edge-cases, or security implications. It's a one line implementation:function array_pick(array $array) {
return empty($array) ? null : $array[array_rand($array)];
}
array_rand()
is also trivial and one length long function, but it exists
in the language and it is really useful.
I don't think that one should think about "how much line it takes to write
it", you should think about "does many people use this function quite often
in order to make it part of the language".
Personally, I think that this function is great, and when I read for the
first time aboutarray_rand()
I thought it'll returns a value, after I
realized it returns a key, I looked for a value pair and surprised to see
that there's no value pair.