Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:102517 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 93680 invoked from network); 28 Jun 2018 14:52:41 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 28 Jun 2018 14:52:41 -0000 Authentication-Results: pb1.pair.com smtp.mail=morrison.levi@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=levim@php.net; sender-id=unknown Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.215.66 as permitted sender) X-PHP-List-Original-Sender: morrison.levi@gmail.com X-Host-Fingerprint: 209.85.215.66 mail-lf0-f66.google.com Received: from [209.85.215.66] ([209.85.215.66:42934] helo=mail-lf0-f66.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id FC/9B-01794-636F43B5 for ; Thu, 28 Jun 2018 10:52:39 -0400 Received: by mail-lf0-f66.google.com with SMTP id u202-v6so4430603lff.9 for ; Thu, 28 Jun 2018 07:52:38 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=mH4ODCilolfRcCJEILD7PeezVCxd1eklJb0tIzLi1JM=; b=AvH2+v86ttS/JJYE4iMCyfPpUAP70EPernQwmxY21p9bne76Wjk6XyGu0u1z9kzuLz dcWORjdDrs16KrQnqvBB/gKaGvYDhlnQvu28eWX9VHsMALVPwtfiSVC3ss/zWdUu0GdO s5fwtpcu4AOLlJMaTlXF6zwDg9aYMsEuTeAXQP2Gh6RtkxDcyiyMH65kOgTl2+V6qyLS 6CKDlYvkKq4tSMCkWcQHygp4YVZomJacnaHtAVLDsj1KHuTS2GLkpeGZJf8LHkM2mzbO lzk0XDRRjLbMlLFsDJAm01sv/I/z624oubkyGi0bZvb1gEGnYZRT1Sm294jC2/6vmOzA kuDQ== X-Gm-Message-State: APt69E0iORu/xl+/06fl53ldqFAD0GHxAtBWkRaSEYtYh5RDgFylvKmL 4igh7t15bFRNk9s3vH4q4PQb6eJPaBQLsMO/ul0= X-Google-Smtp-Source: AAOMgpcea+gMgj1fTaujMCjR9aaiRvT5QGtLKUcJoYGbCOmuZ9zrpZ87hYKjnbQAusDx39GaKpvfwc0lj0jSFp/41BE= X-Received: by 2002:a19:f22:: with SMTP id e34-v6mr7024532lfi.1.1530197555579; Thu, 28 Jun 2018 07:52:35 -0700 (PDT) MIME-Version: 1.0 References: <75db877c-ef0b-7d3b-48a8-9879d90352b0@web.de> <3242366.BL9MUqXe46@vulcan> <2241856.Iz1GuBRvza@vulcan> In-Reply-To: Date: Thu, 28 Jun 2018 08:52:22 -0600 Message-ID: To: enno.woortmann@web.de Cc: Rowan Collins , Marc Bennewitz , internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] [RFC][Under Discussion] Add functions array_key_first()and array_key_last() From: levim@php.net (Levi Morrison) On Thu, Jun 28, 2018 at 7:02 AM Woortmann, Enno wrote: > > Am 26.06.2018 um 16:42 schrieb Rowan Collins: > > On 26 June 2018 at 08:58, Marc Bennewitz wrote: > > > >> Hi all, > >> > >> I just want to add some information that I feel it's missing in that > >> discussion - I'm sorry if it was mentioned already. > >> > >> 1. It's already possible to extract the key and value of any index > >> position of an array using array_slice: https://3v4l.org/mib99 > >> > >> 2. "array_[key|value]_[first|last]($array)" is in my opinion not a good > >> option as it lacks the possibility to extract key/value from any position. > >> Something like "array_[key|value]_index($array, $index)" where $index > >> would be the index position or negative index position from right. > > > > The big advantage of separate functions is that they make the intent clear; > > we could combine all four functions into one: array_index(int $position, > > bool $key), but "array_key_last($something)" is a lot clearer to read than > > "array_index($something, -1, true)". Are there actually use cases for > > getting "the 5th key from the end of the array", common enough to make "the > > last key in the array" harder to write? > > > > Regards, > > As well as Rowan I don't see use cases for fetching keys/values at any > position inside the array for justifying to break up the current > readable function signatures. > Combining multiple use cases and tasks into a single function always > offends the single responsibility principle (compare my previous mail) > and thus should be avoided. > > Are there other opinions concerning this topic? > I'd like to finish the discussion soon and move on to the voting phase. I think there has been a lack of discussion about why this is desired. For me, it's always been implementing certain kinds of iterators that wrap arrays. This means that functions which return both the key and value are optimal for me. As I already outlined the functions which return the key-value pair are the most general and do result in clean code without drawbacks, at least for this use-case. Some people do not like this idea. One potential implication this that maybe their use-case is not the same as mine. What use-cases do other people have for this? Why are you wanting to grab the first or last value? What data structure or algorithm is this a bigger part of?