Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:61649 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 77239 invoked from network); 23 Jul 2012 16:05:44 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 23 Jul 2012 16:05:44 -0000 Authentication-Results: pb1.pair.com smtp.mail=ircmaxell@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=ircmaxell@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.170 as permitted sender) X-PHP-List-Original-Sender: ircmaxell@gmail.com X-Host-Fingerprint: 209.85.216.170 mail-qc0-f170.google.com Received: from [209.85.216.170] ([209.85.216.170:62726] helo=mail-qc0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A2/72-63091-7567D005 for ; Mon, 23 Jul 2012 12:05:43 -0400 Received: by qcmt36 with SMTP id t36so3833093qcm.29 for ; Mon, 23 Jul 2012 09:05:40 -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=q5ssScM5avU1qeNDYOalbRLpw1MEi3hKfZyIqqI1mW8=; b=ERvC+6DxJan/f+whOQERYwtoawoUZHr1C+6Go4DFzwynEZM/uUAplDWul3lRq4rgaB ametf5RQ+B6jO+3klMbkbatYLXYOkr3+LXPYcRv++qJIrckTDqXc4+t3Q8hNfOUQLS8l TeNyvdOER2+IVMEiKoVDGsyuA1cUNnN0d/ayIhSWDM/J+Bo4Qet7Z0BVgIqoD9yLOECo gZDlHAwv4P3HceyDm51Sm58GXfHbMzrjY7CUgDywJISbodA4DQZuyttKgrtmmLzHNCQt nrqRQtygpFhhRB7V6oNPfOuxyq+fMftPhsZnUE/5PX75J1mccM07FluCcbuKa+n8+Qtg H50Q== MIME-Version: 1.0 Received: by 10.229.136.131 with SMTP id r3mr7311779qct.145.1343059540526; Mon, 23 Jul 2012 09:05:40 -0700 (PDT) Received: by 10.229.182.4 with HTTP; Mon, 23 Jul 2012 09:05:40 -0700 (PDT) In-Reply-To: <2075060250.98513.1343049996761.JavaMail.open-xchange@oxwebmail.registrar-servers.com> References: <2075060250.98513.1343049996761.JavaMail.open-xchange@oxwebmail.registrar-servers.com> Date: Mon, 23 Jul 2012 12:05:40 -0400 Message-ID: To: Andrew Faulds Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary=00248c768f9ed348c204c5816adc Subject: Re: [PHP-DEV] array_pick() - resent because email client broke formatting From: ircmaxell@gmail.com (Anthony Ferrara) --00248c768f9ed348c204c5816adc Content-Type: text/plain; charset=ISO-8859-1 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 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 --00248c768f9ed348c204c5816adc--