Re: Variadic is_*() functions

From: Date: Mon, 11 Feb 2019 16:56:01 +0000
Subject: Re: Variadic is_*() functions
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-104347@lists.php.net to get a copy of this message
On Mon, Feb 11, 2019 at 11:35 AM Levi Morrison <levim@php.net> wrote: > >> I recognize that there is one downside, which is that lazy evaluation > >> is lost, but generally don't see it to be an issue in these specific > >> cases. > >> > > Lazy evaluation doesn't have to be lost if the all_of and any_of > functions are written correctly. all_of will return false as soon as one of > them fails, and any_of will return true as soon as one of them passes. > > > > Unless you are talking about cases like this: > > if(is_numeric(reallyFastFunc($foo)) || is_numeric(reallySlowFunc($bar))) > > In that case, you might be able to short circuit the evaluation of > reallySlowFunc($bar), which wouldn't be the case with > > if(any_of('is_numeric',reallyFastFunc($foo),reallySlowFunc($bar))){} > > Yes, this is what I was referring to. As previously stated, I don't > think it's likely to be an issue. If it is, then as you stated the > normal boolean logic can be used in such places. > > ----- > > In other words, nothing needs to be done in PHP itself. Just write or > use someone else's all_of, any_of¼2 > 2TË) }­SaýÌ, none_of, etc, functions. > > I'm sure there would be performance enhancement by having it handled with C vs PHP. I would guess you'd probably need to implement separate functions for each is_* variant though: any_numeric, all_numeric, none_numeric, any_boolean, all_boolean, none_boolean, etc. You could probably still have the any_of, all_of, none_of versions as a shortcut though. I'm speaking from a point of some ignorance though in terms of what is and isn't faster when done in C. Whether it needs to be in core/bundled extension is another debate though. Seems like it might be a good thing for someone wanting to learn to write extensions to get their feet wet with, though. I did something similar when I wanted to try out writing an extension. I implemented a left($str,$len) and right($str,$len) method... which are basically just shortcuts for substr*.. but gave me some experience with all of the boiler plate needed, as well as utilizing existing functions. *left($str,$len) => substr($str,0,$len) right($str,$len) => substr($str,-$len); > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > -- -- Chase chasepeeler@gmail.com

« previous php.internals (#104347) next »