Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:96666 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 68290 invoked from network); 30 Oct 2016 23:10:55 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 30 Oct 2016 23:10:55 -0000 Authentication-Results: pb1.pair.com header.from=php@fleshgrinder.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=php@fleshgrinder.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain fleshgrinder.com from 77.244.243.89 cause and error) X-PHP-List-Original-Sender: php@fleshgrinder.com X-Host-Fingerprint: 77.244.243.89 mx108.easyname.com Received: from [77.244.243.89] ([77.244.243.89:34947] helo=mx204.easyname.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id B4/98-25911-DFD76185 for ; Sun, 30 Oct 2016 18:10:55 -0500 Received: from cable-81-173-132-21.netcologne.de ([81.173.132.21] helo=[192.168.178.20]) by mx.easyname.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1c0zFa-0002BM-SE; Sun, 30 Oct 2016 23:10:51 +0000 Reply-To: internals@lists.php.net References: <44244cfd-c8ad-e0a9-4e70-380a8a8c940c@syberisle.net> <01f2f3da-592b-fd23-709d-ad620422f56d@fleshgrinder.com> <402447f2-6007-95c4-6b1d-2bbb30dfdd1d@fleshgrinder.com> <35e0c893-435e-d5da-58be-4f63796775b4@fleshgrinder.com> To: Yasuo Ohgaki , "internals@lists.php.net" Message-ID: Date: Mon, 31 Oct 2016 00:10:31 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Allow Iterator to be used with current, next, reset, key functions From: php@fleshgrinder.com (Fleshgrinder) On 10/30/2016 11:59 PM, Yasuo Ohgaki wrote: > There should be delete option. It's more useful if there is this option. > > mixed array_first(array $arr [,bool $get_key = FALSE [, bool > $remove_entry=FALSE]]); > mixed array_last(array $arr [,bool $get_key = FALSE [, bool > $remove_entry=FALSE]]); > > Problem with this is error condition. When array is empty, what should > be returned from these with default option? Returning NULL for empty > array would be enough. IMO. > > On Mon, Oct 31, 2016 at 1:52 AM, Levi Morrison wrote: >> To clarify, do you use the return value of these functions or rely on >> the IAP moving to also get the key? > > Depends on code, but I usually use them just to get value of head/tail > element. It could be array_slice/splice, so I don't have much problem > with removing internal array position pointer. However, I still need > current() or each() to get value from returned array. This applies to > array_first/last($arr, TRUE); > > Regards, > > -- > Yasuo Ohgaki > yohgaki@ohgaki.net > Flags are usually a bad design decision, it's better to make it fully explicit and provide dedicated APIs for everything. This is much easier for the reader and reading is more important than anything else. ``` /** Get the first element in the array. */ function array_first(array $array); /** Get the key of the first element in the array. */ function array_first_key(array $array); /** Get the last element in the array. */ function array_last(array $array); /** Get the key of the last element in the array. */ function array_last_key(array $array); /** Get and remove the first element from the array. */ function array_take_first(array $array); /** Get the key of the first element in the array and remove it. */ function array_take_first_key(array $array); /** Get and remove the last element from the array. */ function array_take_last(array $array); /** Get the key of the last element in the array and remove it. */ function array_take_last_key(array $array); ``` Returning `null` is imho the most sensible thing we can do if the array is empty. -- Richard "Fleshgrinder" Fussenegger