Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:102906 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 47427 invoked from network); 18 Jul 2018 01:14:11 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Jul 2018 01:14:11 -0000 Authentication-Results: pb1.pair.com header.from=levim@php.net; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=morrison.levi@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.215.54 as permitted sender) X-PHP-List-Original-Sender: morrison.levi@gmail.com X-Host-Fingerprint: 209.85.215.54 mail-lf0-f54.google.com Received: from [209.85.215.54] ([209.85.215.54:36976] helo=mail-lf0-f54.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id CE/EF-37178-B549E4B5 for ; Tue, 17 Jul 2018 21:14:05 -0400 Received: by mail-lf0-f54.google.com with SMTP id j8-v6so2200873lfb.4 for ; Tue, 17 Jul 2018 18:14:03 -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=0PFV9nJccolgiJFqGgX0mQKhe+ayE3R3iaR5ji9HWjc=; b=UNC5+kC7sj5rS7rVXwZ25BmaKsIhCMbNw6iLI+WstSEI+GGur29817JJ7PG/9k3+D6 wrNBQfP+tBh0mAC1GLTIwckBWNltIJ4ouL0GaqLlMSQBvvkveGrG2ph1F1dRpy2lGp/0 iDoGv72izXYBAcues3ecUxRUpu2auEVfv02+v9jfR3pnFDXW+T843aueBIHkHq6LJAyP KQvL8QNbgO6rJ4GnYbqSi7RKMaG/w7tUa/OgGDP3jZquWZThM2G9e1awOjLyPwp3rmAL ow3xvadrOAdMWKydTzmhKDMTnuT9ICcitWuBKHTBYsfaNY5HdzVYB8+STrKz88onwKKY cD1w== X-Gm-Message-State: AOUpUlEbKwpyVXU1TeFr7zu8HJJXPtbPfxcIS4No6uSpylOh3kI5J+p9 NLrPA2JLUCVzkO+im88CX+xFmT+oPxafCkKJ+iA= X-Google-Smtp-Source: AAOMgpd0EQz9ECDHjOZtiIvX6w1moDgvNu/ecJwKHlfsdTBNXei7aHsL0iX8XcxTg+bWhfxSgM3oCLilf+sGlaJka3Q= X-Received: by 2002:a19:204f:: with SMTP id g76-v6mr2437067lfg.66.1531876440541; Tue, 17 Jul 2018 18:14:00 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 17 Jul 2018 19:13:39 -0600 Message-ID: To: Gabriel Caruso Cc: internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] Replacing array_slice From: levim@php.net (Levi Morrison) On Tue, Jul 17, 2018 at 6:24 AM Gabriel Caruso wrote: > > Hi Levi, thanks for bringing this discussion. > >> Recently I've been scrutinizing array_slice. It's been painful. >> >> For instance, `$preserve_keys` is totally ignored for string keys. >> This was undocumented until Christoph integrated it 2-3 days ago >> (thanks, cmb). However, this is really not a good design decision: why >> have a parameter that is called `preserve_keys` if the data gets the >> final say, especially since there isn't any technical requirement for >> this? >> >> Here's another "fun" behavior: negative offsets whose absolute values >> are larger than the size of the array (eg the array has 3 elements and >> you pass an offset of -5) will shift the window to begin at zero >> instead of truncating the out-of-range values (which is what happens >> on the positive side). >> >> And another: `$length` is a length when it's a positive number, but >> it's an offset from the end when it's negative. You could perhaps >> convince me that when length is positive it's an offset from the >> `$offset` parameter. However, if you look at the design of slicing >> functions in other languages (eg Python, Ruby, JavaScript) they take >> beginning and ending offsets, not a beginning offset and a length. >> >> And another: it always iterates from the beginning of the array even >> if you are only interested in the last half (somewhat common for >> various algorithms that split things in half). >> >> Fixing `array_slice` would probably do more harm than good at this >> stage. Instead I would like to provide an alternative function that >> does not have all this baggage, and will have decent performance much >> of the time. The best ideas I have for names are not that great: >> >> - `array_real_slice`? >> - `array_slice2`? >> >> We could also split it into 2 functions, one that preserves keys and >> one that doesn't, instead of using a parameter. Any ideas there? >> >> -- >> PHP Internals - PHP Runtime Development Mailing List >> To unsubscribe, visit: http://www.php.net/unsub.php > > > A while ago I come up with this discussion in my Twitter, not with a new function, but with a new "operator": https://twitter.com/carusogabriel/status/974610078807613443 > > What do you think? > > Thanks, I think slicing can potentially be a good feature, but there is a lot more design space for this than there is for just adding a new function. Given the other things I have on my plate right now I don't want to start another feature; someone else is welcome to try. "chop" in some other languages effectively means removing leading or trailing members - this would be misleading here. `array_extract` has no relation to `extract`, which might be confusing. It might be okay. What about `array_subslice` for a name? People might initially be curious about having both `array_slice` and `array_subslice`, which will hopefully lead them to reading documentation that `array_subslice` is preferred, but that there are no current plans to remove `array_slice`.