Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:93641 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 6404 invoked from network); 30 May 2016 20:30:38 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 30 May 2016 20:30:38 -0000 Authentication-Results: pb1.pair.com header.from=ocramius@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ocramius@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.52 as permitted sender) X-PHP-List-Original-Sender: ocramius@gmail.com X-Host-Fingerprint: 74.125.82.52 mail-wm0-f52.google.com Received: from [74.125.82.52] ([74.125.82.52:36606] helo=mail-wm0-f52.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id DE/52-26200-CE2AC475 for ; Mon, 30 May 2016 16:30:36 -0400 Received: by mail-wm0-f52.google.com with SMTP id n129so101196664wmn.1 for ; Mon, 30 May 2016 13:30:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=JzxurrJIk57kE70vI9FxLxamlR+4dwvBY64XzVFhBcc=; b=aihXAmo7GKWfxUrVSLNObioNfuw8k1AUY7pwgA6a/R4psll/iQ5nhaAsckEjNEr8V8 GzsxYm8CaLxGp5Q5poii2XrqIfg/Gygwpu9KlkZAQhofWkyvKlOHyJBZ1ZRrxnYh6jNG HKMBjCVKQYN+uGMDVEU2S8uvxTzzPgATJpFhYoXaYUZIoJZ5D+l9ep2JsJArpy8SzwKh q04kWAi1WdwRzqYZ1eo3tlfiK1s1lswZVFWgMxAh9SSLugbr/drn0neDTA64C46EpaZr pcUUF/2R/fEUTca+loTE+wAlDHXvIhmaFR1iIMhukMMKK8z9S8Gv/4739emTFLtO1Rrh rq4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=JzxurrJIk57kE70vI9FxLxamlR+4dwvBY64XzVFhBcc=; b=RtqNqdJDIN6G6xsW+mVD08Fah4aVrNC8y2LyH+PczFhUKLPKR/af01gd1NV3oMI6rC wyIZr4qsrbhkxivZ09OYMFMWa6OsYAaDg7BGnlA0PWbvBSdrUotlSdwWKxfXF4c216KV RrkzvCJDlGTaMRnfDIQ5ZU8KM9scAkaW3lmHqb9kCQ02Akac3Swk3xvhx40Zuskf+3Hp vrA+UeoMUb0MhkQgPo4caja/MSNm7h5OrrFbskv/qem1fyUfvCbFhWnKlLTAiVAdfdES +Zva5coFQ6RgGnGSAhuCf/XK8o6Vhc5tSYoyil2tw8tevdXYmzhZTYxYl+9bgwxSHFCb 72tQ== X-Gm-Message-State: ALyK8tJPfwLttye1nLkHyO63Lq3qLl1o3whSPcesGy1Z/FWQalXGKhbj9jpCd6jAQAl3JkGX7ZMXeQO/7ps0GA== X-Received: by 10.28.48.196 with SMTP id w187mr12688090wmw.71.1464640233728; Mon, 30 May 2016 13:30:33 -0700 (PDT) MIME-Version: 1.0 Received: by 10.194.163.106 with HTTP; Mon, 30 May 2016 13:30:14 -0700 (PDT) In-Reply-To: References: Date: Mon, 30 May 2016 22:30:14 +0200 Message-ID: To: "Colin O'Dell" Cc: PHP Internals , Jeremy Mikola Content-Type: multipart/alternative; boundary=001a11424572daf21c0534152032 Subject: Re: [PHP-DEV] [RFC Discussion] array_change_keys() From: ocramius@gmail.com (Marco Pivetta) --001a11424572daf21c0534152032 Content-Type: text/plain; charset=UTF-8 On 30 May 2016 at 03:40, Colin O'Dell wrote: > Marco, > > >> 1. could you also provide the code for the benchmarks? I'd gladly >> measure them with an accurate tool >> > > Yeah that would be great! Here's the benchmark I was using: > https://gist.github.com/colinodell/872c1f0c92351af687347c0c8be4f253 > Ported to https://github.com/Ocramius/array_change_keys-benchmark, thanks! > 2. do we really need another array function that is basically an >> `array_combine(array_map($someFunc, array_keys($arr)), $arr)`? >> > > While array_combine() technically works, the callback does not have access > to both the key AND value. Anyone needing access to both must currently > resort to using a foreach loop (unless there's some other clever > combination of functions that I'm missing). > > We already have array_change_key_case(). IMO that function is way too > specific and not applicable in 99% of situations where you need to re-key > an array. This is why I'm proposing a general purpose function custom > tailored to rekeying an array. > > I'll touch on some other advantages further down this message. > From what I can see from the benchmarks, a userland implementation of this is not really very different from a core implementation of it, as the only removed overhead has pretty much an `O(1)`, while overhead is introduced by forcing the key change via a callable. > > 3. and... do we really want another function that accepts arrays and not >> generic Traversable (and therefore also generators)? >> > > Do the other array functions support this? If not then I'd argue that > adding Traversable support to array functions should be part of a broader > RFC which adds this support to all applicable functions at once. > No, but I really don't care about the other functions. If something doesn't support a `Traversable` in today's jungle of Generators and asynchronous processing. > In the mean time, you could certainly use iterator_to_array with this > proposed function: > > $b = array_change_keys(iterator_to_array($a), $callback); > This is not usable: Iterators may be infinite, you can't know. Also, this operation may cause a lot of blocking I/O, while instead we want to operate only on the first N elements of an iterator. Doing this with array_combine() would require an intermediate variable to > prevent a "Fatal error: Uncaught Exception: Cannot traverse an already > closed generator": > > $b = iterator_to_array($a); > $a = array_combine(array_map($callback, array_keys($b)), $b); > Yes, this is not workable, as I mention above. Unpacking an iterator into an array defeats the advantages of having an iterator. > Even if all array functions did support generators you'd still need this > intermediate variable to prevent the double traversal - array_change_keys() > would not have this problem. > Yeah, I know what you mean here, but I don't think that the solution is to add more `array_*` stuff that is as legacy as the previous. > 4. what is the real-world benefit over just >> `array_combine(array_map($someFunc, array_keys($arr)), $arr)`, except for >> the two additional function calls here? >> > > There are several IMO: > > 1. Callback has access to both the original key AND the original value. > The array_combine() approach can only access one or the other. > A loop has that too, and a userland looping implementation is still faster > 2. Better performance (hoping you can confirm this). > Sadly not :-( > 3. Purpose of the array_*() function is immediately obvious. > Disagree, as mentioned in few edge cases in other responses (something about numerical keys, IIRC: can't find the actual reply) > 4. No need for intermediate variable when working with iterators means > function composition is possible. > Function composition can be applied also with a userland implementation. My main issue with this is that it doesn't need to exist in *core*. Doesn't mean that it can't exist in userland ;-) Cheers, Marco Pivetta http://twitter.com/Ocramius http://ocramius.github.com/ --001a11424572daf21c0534152032--