Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:72412 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 2811 invoked from network); 8 Feb 2014 18:23:03 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 8 Feb 2014 18:23:03 -0000 Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.178 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 209.85.212.178 mail-wi0-f178.google.com Received: from [209.85.212.178] ([209.85.212.178:65312] helo=mail-wi0-f178.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 8A/91-28087-50676F25 for ; Sat, 08 Feb 2014 13:23:02 -0500 Received: by mail-wi0-f178.google.com with SMTP id cc10so1723825wib.11 for ; Sat, 08 Feb 2014 10:22:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=VEuuuqJJc70jgKXrRYN1HKT0Memvqc+ws4tHNI0jXr4=; b=MOEG5j2uZMPYTL6DthjOvTIhfCAqHrcHjoCmPqOGNBRcvNhr101JgQfCpzPY8tyfaq s84zG8MvcMuIBYdBDcoq8mS3cZY69yQ4rsKEss4z6LDvAU+CbxaD55HzLREpb0P54KsG yvQbC4qjn43zPUXXWiM4a/8RhHsFzGsWxEvtIkZV/DQ5rv8xwd3CWzaCulqsSHjSSMzg Ib7yAD03X/d/X1L8SEMie9QghDTeAJPbHXunM/tb6/jF+0le8Gk5qImFxVmfCIXhjEKr dma5igxt0EpvuJJ7MA/9UUeM36KMG/IVa7y9t1qzNIG9wG4BEGoZEMlu8g/4UZK4sAmw A0gw== X-Received: by 10.194.216.68 with SMTP id oo4mr85756wjc.79.1391883778695; Sat, 08 Feb 2014 10:22:58 -0800 (PST) Received: from [192.168.0.2] (cpc19-brig17-2-0-cust25.3-3.cable.virginm.net. [81.101.201.26]) by mx.google.com with ESMTPSA id fb6sm15365541wib.2.2014.02.08.10.22.57 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sat, 08 Feb 2014 10:22:57 -0800 (PST) Message-ID: <52F675F6.6060800@gmail.com> Date: Sat, 08 Feb 2014 18:22:46 +0000 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.3.0 MIME-Version: 1.0 To: internals@lists.php.net References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] [discussion] add array_usearch() From: rowan.collins@gmail.com (Rowan Collins) On 06/02/2014 19:55, Tjerk Meesters wrote: > Consider the following examples for finding a value in an array: > > array_search($value, $array, true); > > current(array_keys($array, $value, true)); > > key(array_filter($array, function($item) use ($value) { > return $item === $value; > })); > > Perhaps I've overlooked one way, but that's already three ways of doing > something similar; the difference lies in the memory behaviour, the first > being O(1) whereas the other two are O(n). I was pondering this, and it occurred to me it could actually be cast as an application of array_reduce(), except that that function doesn't currently provide array keys to the callback function. Assuming a third parameter was passed to the callback containing the current key, you could define it thus: function array_usearch($array, $fn) { return array_reduce($array, function(&$result, $value, $key) use ( $fn ) { if ( is_null($result) && $fn($value, $key) ) { $result = $key; } }); } (The invert flag would make it a little more verbose, but this would be enough for parity with array_filter() anyway.) This would presumably have the same memory profile as a dedicated implementation (no array needs to be initialised, only the single return value), and the same worst-case time performance, but without the ability to short-cut once a match has been found. Incidentally, in your proposal, you mentioned this: > [2] the array key is passed as the 2nd argument by using > `ARRAY_USEARCH_USE_KEY` Is there a particular reason *not* to pass an extra parameter to callbacks, even if it's unused? In the case of an existing function like array_filter(), I can see that there is a small risk of a BC break if a function was reused as the callback that could optionally take an additional parameter, and relied on that parameter not being passed in, but that seems like a bit of an edge case anyway, and doesn't apply to a brand new function. Of couse, the proposed function can trivially be implemented with a foreach loop as well, even including a short-cut once a match has been found: function array_usearch( $array, $fn ) { foreach ( $array as $k => $v ) { if ( $fn($k, $v) ) { return $k; } } return false; } http://codepad.viper-7.com/YM8LSs Again, I've omitted the ability to invert the matching, but there are plenty of efficient ways of adding that. Regards, -- Rowan Collins [IMSoP]