Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:93630 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 42744 invoked from network); 30 May 2016 01:40:34 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 30 May 2016 01:40:34 -0000 Authentication-Results: pb1.pair.com header.from=colinodell@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=colinodell@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.218.54 as permitted sender) X-PHP-List-Original-Sender: colinodell@gmail.com X-Host-Fingerprint: 209.85.218.54 mail-oi0-f54.google.com Received: from [209.85.218.54] ([209.85.218.54:33313] helo=mail-oi0-f54.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C0/60-36420-11A9B475 for ; Sun, 29 May 2016 21:40:33 -0400 Received: by mail-oi0-f54.google.com with SMTP id k23so250010173oih.0 for ; Sun, 29 May 2016 18:40:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=kz4KSy/kG4679MSOuImwGuS2dz9Ih2BbEhSloEDke2o=; b=p0mOuvEjexq2arK3RUxNg0hXehjg5610OARah7DLvtcDUxg+A2CT5pi2WA/nYPDyPv nAJwNer1MaIfVc+qVzFZyvHLBkElu7isBOrGr0oioM3JwQbrWnEyf4DGhD0IwOpU+xac D98Cs8ekMkSxHr7TNTu3d2f4DqkoMq0uuwwaJbtIHIr1+u+sgIdwcpkVZ6zN/9zrnHMo 8DHLTl77bWJG3JSixrUjlJNveiTxMKkkv/v+M+XJLlgKs5TRobitE6sJb+Kjp8JDF4xP VhHU7Tf5kpiCaj08pVqnKJrgQ49+yvxJJCTc3uvaTk04hmUtQ41ukgYmR9gDjcOIa/wo qKqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=kz4KSy/kG4679MSOuImwGuS2dz9Ih2BbEhSloEDke2o=; b=a64Woo/PQKIgJBQdA1QvNr4ad6ziDxUb87LcUjdP5txcY7rPIMYZOvPFflrJIWJ9T9 99c7FsCw8ij1oaRxRki9qWE8U1Ck4apF+tFmUF9iuydANwwdlbppafQmCh/1KL/vr7jB +hrLWnZ9CSpYR1eOY/TjS8G3CSICL8rxIKULywDvL2XVbUMlIGteQtjWxybfSvfe327h 3NxsKybbpNofITaV47LGup1Q33Ey4ho6ng5bGrWTQbCO+73fQa/dI6LfVRQdKxn4EMwa 1Bami5Hc5RRf4Ngfng7rCC6T61fyiHztQddIx15K6uuL7Y2Oj507THb4Bu+cYP5OM0HU 4QJQ== X-Gm-Message-State: ALyK8tIADrvakMd6XHwipWYIKI0ucH8GvVB6L9dO/i3gU1SpsDEIdDUQnGYOXI/BPbuRzUg87Uq+IJT4cpZNuA== X-Received: by 10.157.15.163 with SMTP id d32mr17008237otd.160.1464572430699; Sun, 29 May 2016 18:40:30 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Mon, 30 May 2016 01:40:21 +0000 Message-ID: To: Marco Pivetta Cc: PHP Internals , Jeremy Mikola Content-Type: multipart/alternative; boundary=001a113d0f047ad418053405575e Subject: Re: [PHP-DEV] [RFC Discussion] array_change_keys() From: colinodell@gmail.com ("Colin O'Dell") --001a113d0f047ad418053405575e Content-Type: text/plain; charset=UTF-8 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 > 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. 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. In the mean time, you could certainly use iterator_to_array with this proposed function: $b = array_change_keys(iterator_to_array($a), $callback); 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); 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. > 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. 2. Better performance (hoping you can confirm this). 3. Purpose of the array_*() function is immediately obvious. 4. No need for intermediate variable when working with iterators means function composition is possible. Thanks for the feedback! Colin --001a113d0f047ad418053405575e--