Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:61650 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 79399 invoked from network); 23 Jul 2012 16:12:24 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 23 Jul 2012 16:12:24 -0000 Authentication-Results: pb1.pair.com smtp.mail=g.b.yahav@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=g.b.yahav@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.54 as permitted sender) X-PHP-List-Original-Sender: g.b.yahav@gmail.com X-Host-Fingerprint: 74.125.82.54 mail-wg0-f54.google.com Received: from [74.125.82.54] ([74.125.82.54:63649] helo=mail-wg0-f54.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 3D/E2-63091-7E77D005 for ; Mon, 23 Jul 2012 12:12:24 -0400 Received: by wgx1 with SMTP id 1so4659674wgx.11 for ; Mon, 23 Jul 2012 09:12:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:mime-version:content-type:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to:x-mailer; bh=XUHo+DOk/304fwlFFK06RzRPN6LEVft9U/+Laz4CSFs=; b=WQNwRqMw0T0JwzfZBYbeTCE1gKTCZwtvJg2D5RBwPhzUrqPHgrV+cu6CJyg2IsWuFN u56M9fmhQr4yVqbfH6UW78dhprtf/SV53JVtqw6NlfUwgG0DgU+HPcn77uJrE5hdRxN8 zecylSksEjrsl0auZBX18COANaHGiVYsQ2nGIgB25/zKFe835GY5xuBNgLCL18BQvLQu xeIb3cBIrfxacaBQslKJYnlMN5gt4LseHiIuxC+Fm9XJCf3vFSOzl/0SlKCHdoZq4bEX 8ZfiTl7ITqmuf5YalEV/dT9jjRZtZlbdmlZzjK1xCwqcEBfjT2j2lX3W12iVGQuJ/NTd TrTA== Received: by 10.216.29.10 with SMTP id h10mr8492680wea.126.1343059941050; Mon, 23 Jul 2012 09:12:21 -0700 (PDT) Received: from [10.0.0.3] ([109.64.148.5]) by mx.google.com with ESMTPS id j6sm26111662wiy.4.2012.07.23.09.12.19 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 23 Jul 2012 09:12:20 -0700 (PDT) Mime-Version: 1.0 (Apple Message framework v1278) Content-Type: text/plain; charset=utf-8 In-Reply-To: Date: Mon, 23 Jul 2012 19:12:17 +0300 Cc: Andrew Faulds , internals@lists.php.net Content-Transfer-Encoding: quoted-printable Message-ID: <9A437120-DD2E-4A75-A6D0-64B3E100DA83@gmail.com> References: <2075060250.98513.1343049996761.JavaMail.open-xchange@oxwebmail.registrar-servers.com> To: Anthony Ferrara X-Mailer: Apple Mail (2.1278) Subject: Re: [PHP-DEV] array_pick() - resent because email client broke formatting From: g.b.yahav@gmail.com (Yahav Gindi Bar) On 23 =D7=91=D7=99=D7=95=D7=9C 2012, at 19:05, Anthony Ferrara wrote: > Andrew, >=20 > On Mon, Jul 23, 2012 at 9:26 AM, Andrew Faulds wrote: >=20 >>=20 >> (resending because of broken formatting) >>=20 >> Hi there, >>=20 >> 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?). >>=20 >> Introduction ------------ >>=20 >> I am proposing a function, named array_pick(), which takes a single = (array) >> argument, and returns a random value from it. >>=20 >> Implementation -------------- >>=20 >> https://github.com/php/php-src/pull/142 >>=20 >> Specification ------------- >>=20 >> 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. >>=20 >> Rationale --------- >>=20 >> - 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 =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) >>=20 >> 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. >>=20 >=20 > Broken Window Theory. = http://en.wikipedia.org/wiki/Broken_windows_theory >=20 > Saying things are already broken is never a valid justification for > breaking things further. >=20 >=20 >> - 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 >>=20 >=20 > 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: >=20 > function array_pick(array $array) { > return empty($array) ? null : $array[array_rand($array)]; > } >=20 >=20 >> - It is also the logical counterpart to array_rand(). One for a key, = one >> for a >> value. >>=20 >=20 > 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... >=20 >=20 >> - 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) >>=20 >=20 > 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)... >=20 >=20 >> --- >> Thoughts? >>=20 >=20 > Shared. >=20 > Thanks, >=20 > 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: >=20 > 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".=20 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.