Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-18 Thread Jorg Sowa
> Sure in many use cases undefined and null can be equivalent, but there > are other use cases and considering the fact that php does not have a > user level "undefined" value. It should at least attempt to not be > ambiguous when possible. Instead of being ambiguous by default, > requiring the

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-18 Thread Tim Düsterhus
Hi On 10/15/23 05:48, Saki Takamachi wrote: I came up with the idea of ​​using a signature like array_filter(), and when a callback is passed, "return the first/last element that matches the condition" and "return null if there is no match." This would be 'array_find()' and I would be in

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-18 Thread Deleu
On Wed, Oct 18, 2023 at 2:49 PM Brandon Jackson wrote: > > The only portion in your email I disagree with is this ending. I believe > there are enough use-cases where if the first value is "valid null" or > "default null" it won't matter. The developer will treat them both the > same. Perhaps

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-18 Thread Brandon Jackson
> The only portion in your email I disagree with is this ending. I believe > there are enough use-cases where if the first value is "valid null" or > "default null" it won't matter. The developer will treat them both the same. > Perhaps you disagree with this and want to avoid ambiguity at all

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-18 Thread Larry Garfield
On Wed, Oct 18, 2023, at 3:37 PM, Deleu wrote: > I believe my reservations have been registered, I would not like to have > `array_first()` behave in a non-obvious way (i.e. involving array $key) > because it's useful for Fibers users. I believe that writing your own > wrapper that always returns

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-18 Thread Benjamin Morel
On Wed, 18 Oct 2023 at 17:47, Levi Morrison wrote: > > I don't see how `array_is_list` is relevant to `array_first` and > `array_last`. PHP arrays are ordered: > > $foo = [ > "first" => 1, > "third" => 3, > ]; > > It would be perfectly fine to use `array_first` or

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-18 Thread Levi Morrison via internals
> > This is simply not true, 0 is not always the first key in a list, > > especially after filtering it. Hence there is a need for this function > > in the first place. > > Just to clarify this point: If 0 is not the first key, then it's not a list. > After filtering a list, you get an array that

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-18 Thread Deleu
On Wed, Oct 18, 2023 at 12:04 PM Robert Landers wrote: > > I completely understand what you're saying and I don't disagree with the > thought process. What I disagree with is your statement that you will > always use array_first() together with array_key_first(). When talking > about standard

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-18 Thread Benjamin Morel
On Wed, 18 Oct 2023 at 17:05, Robert Landers wrote: > > This is simply not true, 0 is not always the first key in a list, > especially after filtering it. Hence there is a need for this function > in the first place. Just to clarify this point: If 0 is not the first key, then it's not a list.

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-18 Thread Robert Landers
> I completely understand what you're saying and I don't disagree with the > thought process. What I disagree with is your statement that you will always > use array_first() together with array_key_first(). When talking about > standard lists (indexed from 0 to upper-bound), array_first() is an

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-18 Thread Deleu
On Wed, Oct 18, 2023 at 10:29 AM Brandon Jackson wrote: > > You may be using them and be totally unaware that you are using them. > > Emphasis on that. Take promises for example. Anyone using libraries > that incorporate promises either are using fibers or will likely be > using them in the near

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-18 Thread Deleu
On Wed, Oct 18, 2023 at 10:11 AM Brandon Jackson wrote: > > I get your desire to keep things simple, but IMO returning a value > that does not conflict with possibly valid values or somehow indicates > the value was not present is important, and should come before > simplicity. Which likely

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-18 Thread Brandon Jackson
> You may be using them and be totally unaware that you are using them. Emphasis on that. Take promises for example. Anyone using libraries that incorporate promises either are using fibers or will likely be using them in the near future. And I'd say that one pattern is probably used enough to be

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-18 Thread Deleu
On Wed, Oct 18, 2023 at 9:29 AM Robert Landers wrote: > On Wed, Oct 18, 2023 at 1:43 PM Deleu wrote: > > > > > > > > On Wed, Oct 18, 2023 at 4:31 AM Robert Landers > wrote: > >> > >> On Wed, Oct 18, 2023 at 5:26 AM Deleu wrote: > >> > > >> > On Tue, Oct 17, 2023 at 3:43 PM Brandon Jackson < >

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-18 Thread Brandon Jackson
> This function signature can be accomplished by userland once we have > `array_key_first()` and `array_first()`. It's much better to keep > `array_first()` as simple as possible and let everyone build their own > approach to go about it since we have so many approaches. The goal wasn't

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-18 Thread Robert Landers
On Wed, Oct 18, 2023 at 1:43 PM Deleu wrote: > > > > On Wed, Oct 18, 2023 at 4:31 AM Robert Landers > wrote: >> >> On Wed, Oct 18, 2023 at 5:26 AM Deleu wrote: >> > >> > On Tue, Oct 17, 2023 at 3:43 PM Brandon Jackson >> > wrote: >> > >> > > > There is also a technique to make the return

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-18 Thread Deleu
On Wed, Oct 18, 2023 at 4:31 AM Robert Landers wrote: > On Wed, Oct 18, 2023 at 5:26 AM Deleu wrote: > > > > On Tue, Oct 17, 2023 at 3:43 PM Brandon Jackson > > wrote: > > > > > > There is also a technique to make the return value `[$key => $value]` > > > instead of just a value, but this

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-18 Thread Robert Landers
On Wed, Oct 18, 2023 at 5:26 AM Deleu wrote: > > On Tue, Oct 17, 2023 at 3:43 PM Brandon Jackson > wrote: > > > > There is also a technique to make the return value `[$key => $value]` > > instead of just a value, but this loses simplicity. > > > > Hmm, since the naming array_first and array_last

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-17 Thread Deleu
On Tue, Oct 17, 2023 at 3:43 PM Brandon Jackson wrote: > > There is also a technique to make the return value `[$key => $value]` > instead of just a value, but this loses simplicity. > > Hmm, since the naming array_first and array_last doesn't clarify that > it's returning a key or a value. What

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-17 Thread Saki Takamachi
I thought of it after sending the email, so I'll post it again. By combining my two ideas, I think I can come up with a smarter proposal. ``` array_first(array $array, int|string &$key = null) ``` If you want to accurately determine whether an array is empty or not, you can satisfy your

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-17 Thread Saki Takamachi
Since reset() and end() return false when the array is empty, in a sense, there is an idea that there is no need to take such strict care of the return value. If you were to take proper care, you would probably specify a default value or throw an exception, as has already been suggested.

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-17 Thread Brandon Jackson
> There is also a technique to make the return value `[$key => $value]` instead > of just a value, but this loses simplicity. Hmm, since the naming array_first and array_last doesn't clarify that it's returning a key or a value. What if it returned both as ?[key, value]. That opens quite a few

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-17 Thread Saki Takamachi
I see now, that makes sense. There is also a technique to make the return value `[$key => $value]` instead of just a value, but this loses simplicity. Thanks. Saki -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-17 Thread Michael Cordover
On Tue, Oct 17, 2023, at 12:19, Saki Takamachi wrote: >> What about a signature like: >> >> ```php >> array_first(array $array, mixed $value_if_missing = null); >> ``` >> >> That would let you specify your own sentinel (or default) where appropriate, >> without losing the convenience of this

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-17 Thread Saki Takamachi
> What about a signature like: > > ```php > array_first(array $array, mixed $value_if_missing = null); > ``` > > That would let you specify your own sentinel (or default) where appropriate, > without losing the convenience of this function. This is a problem that should be addressed in your

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-17 Thread Michael Cordover
On Tue, Oct 17, 2023, at 05:10, Levi Morrison via internals wrote: > It's true that sentiment may have shifted in this time. However, a > common argument at that time still stands: `null` is not a good > sentintenal for failure because the value inside the array very well > could have been null.

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-17 Thread Aimeos | Norbert Sendetzky
On 17.10.23 17:16, Ken Guest wrote: Having array_value_first and array_value_last to match the existing array_key_first and array_key_last functions make sense, and would seem to me to be more intuitive than function names that would not match that scheme. Please don't make things more

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-17 Thread Ken Guest
Having array_value_first and array_value_last to match the existing array_key_first and array_key_last functions make sense, and would seem to me to be more intuitive than function names that would not match that scheme. On Tue, 17 Oct 2023 at 10:41, Robert Landers wrote: > On Tue, Oct 17, 2023

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-17 Thread Robert Landers
On Tue, Oct 17, 2023 at 11:37 AM Robert Landers wrote: > > On Tue, Oct 17, 2023 at 11:34 AM Aleksander Machniak wrote: > > > > On 17.10.2023 11:29, Robert Landers wrote: > > > $value = array_value_first($array, $key); > > > if($key === null) // handle error > > > else // do something with $value

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-17 Thread Robert Landers
On Tue, Oct 17, 2023 at 11:34 AM Aleksander Machniak wrote: > > On 17.10.2023 11:29, Robert Landers wrote: > > $value = array_value_first($array, $key); > > if($key === null) // handle error > > else // do something with $value > > > > You can also freely ignore the key, if you don't need it or

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-17 Thread Aleksander Machniak
On 17.10.2023 11:29, Robert Landers wrote: $value = array_value_first($array, $key); if($key === null) // handle error else // do something with $value You can also freely ignore the key, if you don't need it or care about error checking. That would save doing two function calls on the same

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-17 Thread Robert Landers
On Tue, Oct 17, 2023 at 11:22 AM Lynn wrote: > > > > On Tue, Oct 17, 2023 at 11:15 AM Robert Landers > wrote: >> >> On Tue, Oct 17, 2023 at 11:10 AM Levi Morrison via internals >> wrote: >> >> How is returning null any different than $array[0] on an empty array? >> https://3v4l.org/> > > > >

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-17 Thread Lynn
On Tue, Oct 17, 2023 at 11:15 AM Robert Landers wrote: > On Tue, Oct 17, 2023 at 11:10 AM Levi Morrison via internals > wrote: > > How is returning null any different than $array[0] on an empty array? > https://3v4l.org/ > > > c) Two such functions were proposed and

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-17 Thread Robert Landers
On Tue, Oct 17, 2023 at 11:10 AM Levi Morrison via internals wrote: > > > c) Two such functions were proposed and rejected during the > > array_key_first/last RFC > > (https://wiki.php.net/rfc/array_key_first_last) > > > > Yes, that was in 2018. At that time, functions like str_contains() or > >

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-17 Thread Levi Morrison via internals
> c) Two such functions were proposed and rejected during the > array_key_first/last RFC > (https://wiki.php.net/rfc/array_key_first_last) > > Yes, that was in 2018. At that time, functions like str_contains() or > str_starts_with() wouldn't have even come into existence, just because > there was

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-16 Thread Pierre
Le 15/10/2023 à 18:09, Larry Garfield a écrit : That has already been done:https://www.php.net/array_is_list --Larry Garfield Oh, I forgot it was accepted and merged, thanks for pointing at it. Cheers, Pierre

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-15 Thread Saki Takamachi
Hi Pierre > The first two probably only make sense for a numerically indexed array I do not think so. If these are something like "reset/end without side effects", then they should work fine even not numeric indexed array. Regards. Saki -- PHP Internals - PHP Runtime Development Mailing List

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-15 Thread Larry Garfield
On Sun, Oct 15, 2023, at 7:40 AM, Pierre wrote: > The first two probably only make sense for a numerically indexed array, > so I guess that array_is_list() (whatever the name is, I don't want to > bikeshed about naming) would be a good addition as well, that, in my > opinion, would be

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-15 Thread Pierre
Le 15/10/2023 à 01:11, Ben Ramsey a écrit : On Oct 14, 2023, at 16:30, Nikita Popov wrote: On Sat, Oct 14, 2023, at 20:00, David Grudl wrote: PHP lacks two very basic functions for working with arrays: - array_first() returning the first element of an array (or null) - array_last()

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-15 Thread Paul Dragoonis
On Sun, 15 Oct 2023, 04:49 Saki Takamachi, wrote: > I came up with the idea of ​​using a signature like array_filter(), and > when a callback is passed, "return the first/last element that matches the > condition" and "return null if there is no match." > > The downside to this idea is the loss

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-14 Thread Saki Takamachi
I came up with the idea of ​​using a signature like array_filter(), and when a callback is passed, "return the first/last element that matches the condition" and "return null if there is no match." The downside to this idea is the loss of simplicity. So I'll leave it up to you whether you want

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-14 Thread Ayesh Karunaratne
> > PHP lacks two very basic functions for working with arrays: > > - array_first() returning the first element of an array (or null) > - array_last() returning the last element of the array (or null) > > While PHP has functions that return the first and last keys, > array_key_first() and

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-14 Thread Saki Takamachi
Hi, David Many times I've seen reset() used to meet this requirement. I support this, not only from a side effect standpoint, but also from a readability standpoint. I would also like to add the opinion that it is better because it is simpler than the function name in the original RFC that is

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-14 Thread Ben Ramsey
> On Oct 14, 2023, at 16:30, Nikita Popov wrote: > > On Sat, Oct 14, 2023, at 20:00, David Grudl wrote: >> PHP lacks two very basic functions for working with arrays: >> >> - array_first() returning the first element of an array (or null) >> - array_last() returning the last element of the

Re: [PHP-DEV] Two new functions array_first() and array_last()

2023-10-14 Thread Nikita Popov
On Sat, Oct 14, 2023, at 20:00, David Grudl wrote: > PHP lacks two very basic functions for working with arrays: > > - array_first() returning the first element of an array (or null) > - array_last() returning the last element of the array (or null) > > While PHP has functions that return the

[PHP-DEV] Two new functions array_first() and array_last()

2023-10-14 Thread David Grudl
PHP lacks two very basic functions for working with arrays: - array_first() returning the first element of an array (or null) - array_last() returning the last element of the array (or null) While PHP has functions that return the first and last keys, array_key_first() and array_key_last(), it