Re: [discussion] add array_usearch()

From: Date: Fri, 07 Feb 2014 06:54:52 +0000
Subject: Re: [discussion] add array_usearch()
References: 1 2 3 4 5 6  Groups: php.internals 
Request: Send a blank email to internals+get-72363@lists.php.net to get a copy of this message
Hi, On Fri, Feb 7, 2014 at 2:36 PM, Yasuo Ohgaki <yohgaki@ohgaki.net> wrote: > Hi all, > > On Fri, Feb 7, 2014 at 9:26 AM, Tjerk Meesters <tjerk.meesters@gmail.com>wrote: > >> I'm not wildly enthusiastic about another magic flags parameter, but it's >> an acceptable compromise for me. The slight advantage there is that >> in_array() would be bestowed upon the ability to take a >> callback >> function >> as well. Something like: >> >> if (in_array('is_numeric', $array, SEARCH_NEEDLE_IS_CALLBACK)) { ... } >> > > What we need is 'closure only callback'. > If we have it, then we don't have to worry about string callback functions. > > if (in_array(function($elem) {return is_numeric(strstr($elem));}, $array)) > { ... } > > Why don't we have 'closure' as signature? It can do more than calling > a function. > > bool in_array(closure $callback, array $ary); > > Cons would be we cannot search closures in $ary. > Is there any use of searching closure? > That was my first attempt actually; if the first argument is a closure, use that as the search callback. A few problems with that: 1) You can't use other types of callback, e.g. string or array. 2) [bc] You can't search through an array of closures. 3) [minor] the $strict argument becomes ambiguous. The second problem can be mitigated by checking if $strict is given and true, but it becomes rather messy. > Regards, > > -- > Yasuo Ohgaki > yohgaki@ohgaki.net > > -- -- Tjerk

« previous php.internals (#72363) next »