On Mon, Aug 31, 2020 at 9:52 AM Josh Bruce <j...@joshbruce.dev> wrote:

> Just to confirm
>
> array_filter(“!is_int”, $collection)
>
> Would result in a collection of only non-integers??
>
>
No, you'd have to put it in a closure

The original poster had a typo, I think, and meant array_reject not
array_reverse. He basically implemented the solution that Larry was
referring to, before Larry referred to it.

function array_reject(Callable $c, Array $a){
  return array_filter(fn($item) => !$c($i), $a);
}

$non_ints = array_reject('is_int',[1,2,'a',3.5]);

If you don't want to write your own array_reject method, and just handle it
case-by-case, then it's still trivial

$non_ints = array_filter(fn($i) => !is_int($i), [1,2,'a',3.5]);

I do think there’s something to be said for the communication of intent
> without syntax.
>
> array_without or array_reject reads easier to me than making sure to watch
> for the bang.
>
>
I agree with Larry that userland implementation is trivial enough that it
doesn't really need to be implemented in core. It's just syntactic sugar
that's probably more trouble than it's worth.  That being said, I'm by far
an expert when it comes to core, so I can't really say 1.) what performance
benefits it would provide or 2.) how hard (or easy) it would be to
implement.



> Cheers,
> Josh
>
> >> On Aug 30, 2020, at 6:55 PM, Larry Garfield <la...@garfieldtech.com>
> wrote:
> >>
> >> On Sun, Aug 30, 2020, at 9:38 AM, David Rodrigues wrote:
> >> Currently we have array_filter(), but sometimes we need an inverse
> function
> >> like array_reject().
> >> array_reject('is_null', [ 1, 2, null, 3 ]); // [ 1, 2, 3 ]
> >> It could be easily implemented with:
> >> function array_reverse($fn, $arr) { return array_filter(fn($item) =>
> >> !$fn($item), $arr); }
> >> Anyway, I think that It should be implemented by core, once that we have
> >> array_filter().
> >
> > Any boolean function can be inverted with a simple ! in a short
> closure.  I don't really see a need to do that in C.
> >
> > --Larry Garfield
> >
> > --
> > PHP Internals - PHP Runtime Development Mailing List
> > To unsubscribe, visit: https://www.php.net/unsub.php
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: https://www.php.net/unsub.php
>
>

-- 
Chase Peeler
chasepee...@gmail.com

Reply via email to