Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:91130 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 88859 invoked from network); 8 Feb 2016 18:06:46 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 8 Feb 2016 18:06:46 -0000 Authentication-Results: pb1.pair.com smtp.mail=kinncj@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=kinncj@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.213.42 as permitted sender) X-PHP-List-Original-Sender: kinncj@gmail.com X-Host-Fingerprint: 209.85.213.42 mail-vk0-f42.google.com Received: from [209.85.213.42] ([209.85.213.42:33388] helo=mail-vk0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id CD/78-36326-439D8B65 for ; Mon, 08 Feb 2016 13:06:44 -0500 Received: by mail-vk0-f42.google.com with SMTP id k196so34115579vka.0 for ; Mon, 08 Feb 2016 10:06:44 -0800 (PST) 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:content-type; bh=lugWZQ3NuGfW4GL4BbY7reMLZHqe5CmWoPGxUm9WcLE=; b=HLByCHyTgxpUh38JDbDZjQUiGFCT3r0aqfsQ6pIS+zrbRVD5Trdc22MBaiY486oY6F EMLGPLi1oMrJcy9V9KFtfve+1MSvLOyYEkpO09KnrWm67rjuPcwu7m0hsuIeG0zJBzWe u8zETWA0sqIEBVCumtatVdeaRXp2VFACGijrrIfshoGPovl4ZfskBqyA7pBOyE5hAtpM U7wj6fdBu7c8259XIWyH/mSrlKuizlb5Aax0cRtGAwXMFK/50jIxzBaAxA15GuJybW9Q KuGlcOT9fJrjc9o4hNa7z8LFa+SwPfhJQMalg5Ynd5//NeDwi5KRJw5FwFO+OuMcYlpo gv4w== 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:content-type; bh=lugWZQ3NuGfW4GL4BbY7reMLZHqe5CmWoPGxUm9WcLE=; b=ilGJ660xRDVeteGEaWQhvwfxt8Kt/Po2VsownG5wyqbM2Lu89NfJPzrpj2dkYT5ukA 4G2qKJ3Rp2GWFb+4b5tapIfcD/u61cXQescKgT92iX6tWUgGeag7dac7c2zAfcKFx9WB UMyT58bW5/rInUQ538pry5YU7A41HwmlHdC6+xCnUzLSJwvR3y6UV8qlOKS1sCRbDm9Q vSxB384xbBfKBwEU1E4dzZb/O5aaHe181Zu+ZDft4x27asLRrElydW4kRKQcQTjDn37P JSiWs+WjWQuQHcgVw9hFyXBa9/vSzPWbAJjhf0l9o3oArF3Uc80+9/yPvbfuwjDVT/yX 6nxw== X-Gm-Message-State: AG10YOQOOZg8082VU9us3p9ze6tMIl8e0wzcC9FJI8Ue9GvR4nVDhuPnOy8TGeUcNxzorvRRL/lVyGjvx5nXAw== X-Received: by 10.31.151.11 with SMTP id z11mr21127799vkd.131.1454954801955; Mon, 08 Feb 2016 10:06:41 -0800 (PST) MIME-Version: 1.0 Received: by 10.31.52.23 with HTTP; Mon, 8 Feb 2016 10:06:22 -0800 (PST) In-Reply-To: <56B8D629.3030305@garfieldtech.com> References: <56B8C492.2060800@gmail.com> <56B8D629.3030305@garfieldtech.com> Date: Mon, 8 Feb 2016 13:06:22 -0500 Message-ID: To: Larry Garfield Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary=001a1140f86a228aec052b4610d4 Subject: Re: [PHP-DEV] Proposal for a new array function From: kinncj@gmail.com (=?UTF-8?B?S2lubiBKdWxpw6Nv?=) --001a1140f86a228aec052b4610d4 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Mon, Feb 8, 2016 at 12:53 PM, Larry Garfield wrote: > On 2/8/16 11:46 AM, Kinn Juli=C3=A3o wrote: > >> On Mon, Feb 8, 2016 at 11:38 AM, Rowan Collins >> wrote: >> >> Kinn Juli=C3=A3o wrote on 08/02/2016 16:05: >>> >>> That's the nice thing about userland... you are giving them ability to >>>> implement what they want! >>>> `array_filter([...], 'is_int', ARRAY_FILTER_USE_KEY)` does the job as >>>> the >>>> others... don't think having a new function would prevent this. >>>> >>>> It does the job, but it is much less efficient in some cases than the >>> foreach and return false case, as it must examine every element; it als= o >>> has unnecessary memory overhead, creating a filtered list which exists >>> only >>> to be counted and thrown away; and an internal implementation could be >>> optimised even further by exploiting the way the engine stores the arra= y >>> internally. >>> >>> How does `array_filter([...], 'is_int', ARRAY_FILTER_USE_KEY)` add that >> *much* overhead? >> And if that's the case, instead of introducing a 99999 array function in= to >> the core, why not just create a new Object then? >> >> $list =3D list('foo','bar'); // I know it's a reserved token, don't have >> time >> to find a better synonym. it's just a sample >> $list[2] =3D 'baz'; >> >> var_dump($list); >> >> string(1) "foo" >> string(1) "bar" >> string(1) "baz" >> >> $list['a'] =3D 'foobar'; // Fatal error. >> > > I'm confused at the goal here. Is the intent "I need to do different > logic if this is a numeric array vs. something else" or "I need to make > sure this is a numeric array, because things break otherwise"? > It doesn't really matter, and the explanation is below[1] (you actually wrote it). > > If the latter, $foo =3D array_values($bar); is a reasonably efficient and > reasonably bulletproof way to ensure you have a strictly numeric array. > > Agreed. > PHP is at this stage expressive enough that a large number of core >>> functions could be rewritten in multiple ways using other core function= s, >>> but that doesn't automatically rule out adding more functions to core >>> where >>> it is useful to do so. >>> >> >> PHP is at a stage where we can add support to a better structure and >> objects support... we don't need a function to check if the keys are >> integer, we need an object that only supports integer as keys, or even a >> generic key... >> >> Would rather have `Map` than `has_numeric_keys` >> > > Or that. Which I like even better. Ahead-of-time typing is much safer a= t > scale than runtime typing. (Of course we then run smack into the > arrays-and-objects-are-just-different-enough-to-be-annoying problem that > has plagued us for a decade.) > > [1] - That's why it doesn't matter if it's to restrict or validate... As the top post mentions: ` function isArrNum($arr) { foreach($arr as $i =3D>$v){ if (!is_int($i)) { return false; } } return true; } ` If you want to check the keys type. ``` return $variable instanceof Map; ``` Don't want to bc? ``` function isArrNum($arr) if (class_exists('Map')) $variable instanceof Map; foreach($arr as $i =3D>$v){ if (!is_int($i)) { return false; } } return true; } ``` Ugly? sure... but at least now we have support to a better structure... remember: Working towards making PHP 7 even better... adding support to Map(or whatever people wanna call it) can make it better, adding a `has_numeric_keys`, IMHO, is just an `alias` to other userland options. > -- > --Larry Garfield > > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --=20 *--* *Kinn Coelho Juli=C3=A3o* *Toronto - ON/Canada* --001a1140f86a228aec052b4610d4--