Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:61651 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 80878 invoked from network); 23 Jul 2012 16:16:13 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 23 Jul 2012 16:16:13 -0000 Authentication-Results: pb1.pair.com header.from=sam.e.giles@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=sam.e.giles@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.176 as permitted sender) X-PHP-List-Original-Sender: sam.e.giles@gmail.com X-Host-Fingerprint: 209.85.212.176 mail-wi0-f176.google.com Received: from [209.85.212.176] ([209.85.212.176:34911] helo=mail-wi0-f176.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 46/43-63091-CC87D005 for ; Mon, 23 Jul 2012 12:16:13 -0400 Received: by wibhn17 with SMTP id hn17so2438138wib.11 for ; Mon, 23 Jul 2012 09:16:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=Zm6yEEEVSHr3OnrRbFzt2V9D8H1eCefCFXmukFns510=; b=RFN0bGw9ZWP7eRS+Q4H4KyZncBbevV3wY7MHHZU6gFbyHWyjyNzs1tH5+CBRCAUe6c iCHHY9igBGJ5CgT3jwqXoIMx7CMr4d9jOZubsospDlFpuU8NgMAK04oZ8CeJi719C77+ ZKDl8+hEtrxV7AuqBY9Z79vA5fKZET47+qgOwdYp3a2V7tPuqT8oX0WEmpngCg6qktih N+xSukG5ywKnpC3Lx7E/RYkwCv8TliIBdkgJb+DzhPFK/r5u3KlDsn0sehNuByLlTs4z jyiBmC/kl7Pat1O7LVx44cI/A43R7JyPcBEW3BCa4fFsjcxNGRn4p1Lq5RyHKWc9eoT7 hA3Q== MIME-Version: 1.0 Received: by 10.216.139.95 with SMTP id b73mr7782578wej.191.1343060170135; Mon, 23 Jul 2012 09:16:10 -0700 (PDT) Received: by 10.194.34.232 with HTTP; Mon, 23 Jul 2012 09:16:10 -0700 (PDT) In-Reply-To: <9A437120-DD2E-4A75-A6D0-64B3E100DA83@gmail.com> References: <2075060250.98513.1343049996761.JavaMail.open-xchange@oxwebmail.registrar-servers.com> <9A437120-DD2E-4A75-A6D0-64B3E100DA83@gmail.com> Date: Mon, 23 Jul 2012 17:16:10 +0100 Message-ID: To: Yahav Gindi Bar Cc: Anthony Ferrara , Andrew Faulds , internals@lists.php.net Content-Type: multipart/alternative; boundary=0016e6d4f5985a581f04c5819000 Subject: Re: [PHP-DEV] array_pick() - resent because email client broke formatting From: sam.e.giles@gmail.com (Samuel Giles) --0016e6d4f5985a581f04c5819000 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Why bloat the core library of functions when you can perform: $randomValue =3D $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 wrote= : > > On 23 =D7=91=D7=99=D7=95=D7=9C 2012, at 19:05, Anthony Ferrara wrote: > > > Andrew, > > > > On Mon, Jul 23, 2012 at 9:26 AM, Andrew Faulds wrote: > > > >> > >> (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 fo= r > >> these > >> cases: > >> - short array syntax + array_pick, e.g. > some_func(array_pick(['foo.png', > >> 'bar.jpg', 'foobar.gif'])) vs $a =3D ['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_theor= y > > > > 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 frivolou= s > 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 i= s > 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 t= he > > array namespace indicates a different thing. I would argue that > > array_random_value() would be a better choice (or any one of a number o= f > > 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 writ= e > it", you should think about "does many people use this function quite oft= en > 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. > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --0016e6d4f5985a581f04c5819000--