Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:96684 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 54844 invoked from network); 31 Oct 2016 21:49:06 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 31 Oct 2016 21:49:06 -0000 Authentication-Results: pb1.pair.com header.from=larry@garfieldtech.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=larry@garfieldtech.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain garfieldtech.com from 66.111.4.26 cause and error) X-PHP-List-Original-Sender: larry@garfieldtech.com X-Host-Fingerprint: 66.111.4.26 out2-smtp.messagingengine.com Received: from [66.111.4.26] ([66.111.4.26:47966] helo=out2-smtp.messagingengine.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 9D/A3-25911-15CB7185 for ; Mon, 31 Oct 2016 16:49:05 -0500 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id A626A20A62 for ; Mon, 31 Oct 2016 17:49:02 -0400 (EDT) Received: from frontend2 ([10.202.2.161]) by compute3.internal (MEProxy); Mon, 31 Oct 2016 17:49:02 -0400 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc:x-sasl-enc; s= smtpout; bh=rsbUIAVODlOAd7nKeUa+51Dq8tc=; b=uyyWE9NpBbag2CgNX5pp LlJbBzGyS6iyQr3chifB5/OhBcwL7qagckGcCUQ2dv2mcW3n2+tbJktqeRCSqPTm lCdnMfRGGJE+pjTYwYWtTbpRPuLi3qix8f7EnCggv3iIwI8ZOygfn+Qft31RKuNJ VNJWNzn00J5+kiE5y0n3ECg= X-ME-Sender: X-Sasl-enc: M07H+Topw6D+yONouoaJrfA3QP5kXfdhnoVkUk1ykTGF 1477950541 Received: from [192.168.42.5] (c-50-178-40-84.hsd1.il.comcast.net [50.178.40.84]) by mail.messagingengine.com (Postfix) with ESMTPA id B11BCCCF29 for ; Mon, 31 Oct 2016 17:49:01 -0400 (EDT) To: internals@lists.php.net References: <44244cfd-c8ad-e0a9-4e70-380a8a8c940c@syberisle.net> <01f2f3da-592b-fd23-709d-ad620422f56d@fleshgrinder.com> <7fb9a5d5-318d-7ed3-59eb-d9845ed5dec1@gmail.com> <5219c048-66f1-be9a-ae3c-15633a65e263@garfieldtech.com> Message-ID: Date: Mon, 31 Oct 2016 16:49:01 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.3.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Allow Iterator to be used with current, next, reset, key functions From: larry@garfieldtech.com (Larry Garfield) On 10/31/2016 01:07 PM, Levi Morrison wrote: > On Mon, Oct 31, 2016 at 11:57 AM, Levi Morrison wrote: >> On Mon, Oct 31, 2016 at 11:27 AM, Larry Garfield wrote: >>> Question: Now that we have "iterable" as a type, should we be adding more >>> array-specific functions or should such operations be designed, and named, >>> to operate on any iterable? (first(), first_key(), etc.) >> At least for last and last-key I do not think we should make them work >> with any iterable. My reasoning is that we do not want to consume the >> iterator for them because many iterators are not rewindable (such as >> generators). The first and first-key routines should generally work >> because `rewind(); valid(); current(); rewind();` doesn't contain a >> `next()` call. >> >> Rewinding without iterating works for generators: https://3v4l.org/O7ZKO >> Rewinding after a next() call: https://3v4l.org/QI434 > Additionally consuming the iterator would be an algorithmic complexity > of O(n) whereas for an array would just be O(1). So not only is it > annoying that the iterator is consumed and can't be guaranteed to be > rewound it is also costly. > > The first and first-key routines do not have these issues. Might it make sense then to have first(iterable) first_key(iterable) array_last(array) array_last_key(array) It's not quite as parallel, but it does provide more broadly compatible functionality. If we're concerned about pw0ning the "first" function name, perhaps it's time to start a new prefix: iter_first(iterable) iter_first_key(iterable) array_last(array) array_last_key(array) Which gives a natural place to start putting iterable-friendly alternates of the various array_* functions, for those that make sense. (think iter_fold(), iter_map(), iter_filter(), etc.) (Insert debate about parameter order and method-based alternatives here.) --Larry Garfield