Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:53613 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 66436 invoked from network); 28 Jun 2011 13:51:25 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 28 Jun 2011 13:51:25 -0000 Authentication-Results: pb1.pair.com header.from=jarrod@squarecrow.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=jarrod@squarecrow.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain squarecrow.com designates 207.179.226.134 as permitted sender) X-PHP-List-Original-Sender: jarrod@squarecrow.com X-Host-Fingerprint: 207.179.226.134 smtp54.mtco.com Linux 2.6 Received: from [207.179.226.134] ([207.179.226.134:48176] helo=smtp53.mtco.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id FC/A0-61099-A5CD90E4 for ; Tue, 28 Jun 2011 09:51:24 -0400 Received: from smtp1.mtco.com ([207.179.255.101]) by smtp53.mtco.com ({cf6f0531-5817-46fb-9acd-8660d0c54100}) via TCP (outbound) with ESMTP id 20110628135119675 for ; Tue, 28 Jun 2011 13:51:19 +0000 X-RC-FROM: X-RC-RCPT: Received: from localhost (web1.mtco.com [207.179.212.68]) by smtp1.mtco.com (Postfix) with ESMTP id B9BCB1DC456 for ; Tue, 28 Jun 2011 09:01:42 -0500 (CDT) MIME-Version: 1.0 X-Priority: Normal X-Mailer: AtMail PHP 5.5 Message-ID: <7614.1309269079@mtco.com> To: Reply-To: jarrod@squarecrow.com Content-Type: multipart/alternative; boundary="=_f18c4e7c5f496ec625fd0e1bcfdb0efd" X-Origin: 65.89.229.229 X-Atmail-Account: jarrod@mtco.com Date: Tue, 28 Jun 2011 08:51:19 -0500 Subject: Inline Lambda Functions From: jarrod@squarecrow.com (Jarrod Nettles) --=_f18c4e7c5f496ec625fd0e1bcfdb0efd Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" There are two projects that I've been following for awhile now: PLINQ and PHPLinq. http://plinq.codeplex.com/ http://phplinq.codeplex.com/ Both of them have made very solid attempts at providing LINQ-like functionality to PHP but with both, I've been a little frustrated with the implementations, due to the wordy syntax that PHP lambda functions require. LINQ operates by the use of predicates: a function that will be run against each item in whatever datasource you happen to be querying and will resolve to true or false - literally, whether the current item should be included in the final result set. Both PLINQ and PHPLinq have achieved roughly the same level of technology. However, PHPLinq makes use of runtime evaluated strings to mimic the C# implementation and PLINQ makes use of PHP's existing lambda style, which can be very wordy.=20 Here is an example of both. PHPLinq // Create data source $names =3D array("John", "Peter", "Joe", "Patrick", "Donald", "Eric"); $result =3D from('$name')->in($names) =20 ->where('$name =3D> strlen($name) < 5') =20 ->select('$name'); Notice the quoted strings around the "where" clause in that statement. The reason for this was to provide as close as syntax to C# as possible (PHPLinq was created by a Microsoft MVP, if I remember correctly). The syntax is definitely shorter than anything PHP could currently provide, but it requires string evaluation and comes with all of the many downsides of being string evaluated that I'm sure are already rifling through your head right now. Now look at PLINQ. $plinq =3D new Plinq($users); //gets the users that are older than 60 $olderUsers =3D $plinq->Where(function($k, $v){ return $v->Age > 60; });PLINQ definitely follows the "correct" way handling predicated statements. I'm curious though, what people thought about providing a simpler, shorthand way of providing lambda functions (this isn't a request for LINQ in PHP! I'm merely using it as an example showing the extremes in PHP right now). Rewriting the statement above could look like this: $olderUsers =3D $plinq->Where($v =3D> $v->Age > 60); //this is just suggestive syntax This eliminates a swath of unnecessary text - specifically, the "function" and "return" keywords and the associated brackets and parentheses - the result is less typing and a more readable block of code. I love closures in PHP but often I find myself in a situation like the one above - I need a quick function that I will never use again and it would be great if there was a shorthand for creating such simple statements. What does everyone else think? =20 --=_f18c4e7c5f496ec625fd0e1bcfdb0efd--