Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:96668 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 71468 invoked from network); 30 Oct 2016 23:26:47 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 30 Oct 2016 23:26:47 -0000 Authentication-Results: pb1.pair.com header.from=yohgaki@ohgaki.net; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=yohgaki@ohgaki.net; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain ohgaki.net designates 180.42.98.130 as permitted sender) X-PHP-List-Original-Sender: yohgaki@ohgaki.net X-Host-Fingerprint: 180.42.98.130 ns1.es-i.jp Received: from [180.42.98.130] ([180.42.98.130:54946] helo=es-i.jp) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id AC/39-25911-6B186185 for ; Sun, 30 Oct 2016 18:26:47 -0500 Received: (qmail 24110 invoked by uid 89); 30 Oct 2016 23:26:43 -0000 Received: from unknown (HELO mail-wm0-f51.google.com) (yohgaki@ohgaki.net@74.125.82.51) by 0 with ESMTPA; 30 Oct 2016 23:26:43 -0000 Received: by mail-wm0-f51.google.com with SMTP id p190so117534465wmp.1 for ; Sun, 30 Oct 2016 16:26:42 -0700 (PDT) X-Gm-Message-State: ABUngvex1e8awahVJxdEgDgQE8a/Sbw/tbj3QxlnXonbIjETfhrgdBsMmOp6m2hddJKBIqIFiHAJAjN5mxxLHQ== X-Received: by 10.194.118.100 with SMTP id kl4mr19469912wjb.193.1477869996865; Sun, 30 Oct 2016 16:26:36 -0700 (PDT) MIME-Version: 1.0 Received: by 10.194.205.42 with HTTP; Sun, 30 Oct 2016 16:25:56 -0700 (PDT) In-Reply-To: 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> Date: Mon, 31 Oct 2016 08:25:56 +0900 X-Gmail-Original-Message-ID: Message-ID: To: "internals@lists.php.net" Content-Type: text/plain; charset=UTF-8 Subject: Re: [PHP-DEV] Allow Iterator to be used with current, next, reset, key functions From: yohgaki@ohgaki.net (Yasuo Ohgaki) Hi, Fleshgrinder On Mon, Oct 31, 2016 at 8:10 AM, Fleshgrinder wrote: > 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. >> > > 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. It depends. Defining specific functions for a task could be bad design also. We do design basic functions with flags usually, too. That said, dedicated functions for getting first/last key/value sounds good to me. > > ``` > /** 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. A agree. That's the reason why I state return value is problem. However, if (empty($arr)) { // Do something } else { $first = array_first($arr); } shouldn't be difficult. We should raise error for nonsense usage. Programmers have to check empty()/isset() all the time anyway. Regards, -- Yasuo Ohgaki yohgaki@ohgaki.net