Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:85278 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 69369 invoked from network); 20 Mar 2015 12:24:46 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Mar 2015 12:24:46 -0000 Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.173 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.173 mail-we0-f173.google.com Received: from [74.125.82.173] ([74.125.82.173:35050] helo=mail-we0-f173.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id CE/9E-25408-C811C055 for ; Fri, 20 Mar 2015 07:24:45 -0500 Received: by webcq43 with SMTP id cq43so80883717web.2 for ; Fri, 20 Mar 2015 05:24:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type; bh=yYYJHZvzALmopn94MZyqKZZ1lGVSl/6Uy59guojU2NM=; b=siz+sGi56ZGJdUaWJaY3UNHbIp/X5gJZkt9qMvCJOKOLB3E9RDxoTFsI6zrvfQxZlS OYfg9ck4BapIqLON+kCctRUrvxDMM5j63IzJpj8HEBQOKLpzzAMzhRUii3344JCdkHr2 jqzsjRRZSPP557AvMDqW3GgPTk49d5Gel8sTE9ZWYctFjtfZRvzvSxLZw/SxNi3KqjuH z7k4HNXFjSc+NeNj6dQM1J0TvvMSNIzd2sHAoZ0atQsOVNGQoqmUbb14l6/pffiAa0FR 0Dw5O0YGSxc+wnxx8QCFM0VGgH8LsrqBRmvVB2Ea6dz6jMwcAX24pv2BGwzarjAUWmvl pzIw== X-Received: by 10.180.82.9 with SMTP id e9mr24347861wiy.88.1426854282009; Fri, 20 Mar 2015 05:24:42 -0700 (PDT) Received: from [192.168.0.159] ([62.189.198.114]) by mx.google.com with ESMTPSA id dx11sm6154433wjb.23.2015.03.20.05.24.39 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 20 Mar 2015 05:24:41 -0700 (PDT) Message-ID: <550C1176.9050307@gmail.com> Date: Fri, 20 Mar 2015 12:24:22 +0000 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 MIME-Version: 1.0 To: Alex Bowers CC: internals@lists.php.net References: <550B5A81.1090706@gmail.com> <550B7189.4040608@gmail.com> <7486C296-7535-4633-AFE2-02E7BBC67BC5@gmail.com> In-Reply-To: Content-Type: multipart/alternative; boundary="------------080409030502070706030508" Subject: Re: [PHP-DEV] RFC - Array slice syntactic sugar From: rowan.collins@gmail.com (Rowan Collins) --------------080409030502070706030508 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Alex Bowers wrote on 20/03/2015 11:38: > > The @ symbol in my examples is not a special marker that is > meaningful on its own; the parser wouldn't even see it as a > separate token. The syntax for key access is $array[$key], the > syntax for positional access would be $array[@$position]; chosen > to look similar, but one is not a special case of the other. > > > Ah, I understand you now. Yes I agree that this would be useful then. > It allows us the future possibility of a separate RFC looking into > slicing on keys as mentioned at the bottom of your post. > > The syntax needn't be just an extra character, we could invent > something else, like $array[[$position]], or $array@($position), > though I can't think of anything I like. > > > I think for simplicity, we should keep it as $array[], and have a > symbol within it that shows that it is an internal index rather than > the key. An Asterisk (*) is my preference at the moment, though caret > also looks promising. > > Here are some examples of how various characters could look > > $array[*1:3], $array[*:], $array[*:1], $array[*1:] > $array[@1:3], $array[@:], $array[@:1], $array[@1:] > $array[^1:3], $array[^:], $array[^:1], $array[^1:] > $array[&1:3], $array[&:], $array[&:1], $array[&1:] > > The symbol isn't something that can be missing, it's just part of > the syntax, so this is like saying "what should we do if the > closing ] is missing?" The answer is, nothing, it's undefined > syntax, so it's a parse error. > > > So a parse error currently, and a future RFC can define the terms of > using this normally? > > If we go down the route of adding a symbol for by index rather than by > key, then this RFC should also include the normal getting array by key > style, without the colon range operator. > > For example: > > $array[*1] to get the item that is the second in the list, rather than > at key 1. Yep, I think we're on the same page now. There's basically three ways of selecting items - by key, by position from start, and by position from end - and two types of selection - a single element, or an array of zero or more elements. That gives us 6 possible combinations, which (using the @ notation for consistency with previous examples) would be: Select by key: Single element: $array[$x] - existing syntax Slice: $array[$a:$b] - equivalent to a filter asserting $key >= $a && $key <= $b Select by position from start: Single element: $array[@$x] Slice: $array[@$a:$b],$array[@$a:], and $array[@:$b] Select by position from end - as above, but using negative indexes: Single element: $array[@-$x] - equivalent to $array[@length($array)-$x] Slice: $array[@-$a:-$b] - equivalent to $array[@length($array)-$a:length($array)-$b] Several of these are actually rather hard to do with current PHP features: - you can get the first element with reset() and the last with end(), but arbitrary position selection is harder - there is no version of array_filter for testing keys, and being able to write $dictionary['elephant':'snake'] would actually be pretty handy Actually, the only one that's easy to emulate (apart from the obvious single element by key) is the one you propose to add first - getting a positional slice of an array, which array_slice(..., true) already does. Regards, -- Rowan Collins [IMSoP] --------------080409030502070706030508--