Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:61029 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 67858 invoked from network); 28 Jun 2012 17:05:20 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 28 Jun 2012 17:05:20 -0000 Authentication-Results: pb1.pair.com smtp.mail=nikita.ppv@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=nikita.ppv@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.214.42 as permitted sender) X-PHP-List-Original-Sender: nikita.ppv@gmail.com X-Host-Fingerprint: 209.85.214.42 mail-bk0-f42.google.com Received: from [209.85.214.42] ([209.85.214.42:39257] helo=mail-bk0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 8F/B0-62543-ECE8CEF4 for ; Thu, 28 Jun 2012 13:05:19 -0400 Received: by bkcje2 with SMTP id je2so1731231bkc.29 for ; Thu, 28 Jun 2012 10:05:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=p9ElsPNYFCAvBG7LzpnxiSV8Zd4CJTsFN6T3ILuQIBs=; b=aNBQN7VCi4El6IN4DDLT+TzwVWqqeCA/VAjIdhv9ItjZmxRCOEntIPAjHWHbqiQmR6 xd3SunD0zZm/Rts//7/LdidT+asDwclqB/4OeqgBS96cMJJBd8tT1mSYLYmilWun48Aw zT12IuZwO3dwMH6Y1ftOmljUCWO/luUPwt+iy9wZxJ5wUcxzCofW4Bgz4ZYRbLm4eY2U 8enlcrMJGRpoEVz5//ZqAbcw9g7jWFYss0yVVB9p3k6gGv8n7Nc7H03+kCO/Q35+mp4k uFA8FjgzAJHocZNsUjWOv4JTMxxCtejjrQ9PORS3dkbMcAE9FFna0WVUD8AKSbTy2moq JR9Q== MIME-Version: 1.0 Received: by 10.152.105.173 with SMTP id gn13mr3005333lab.20.1340903115359; Thu, 28 Jun 2012 10:05:15 -0700 (PDT) Received: by 10.152.114.70 with HTTP; Thu, 28 Jun 2012 10:05:15 -0700 (PDT) In-Reply-To: References: Date: Thu, 28 Jun 2012 19:05:15 +0200 Message-ID: To: Sebastian Krebs Cc: PHP internals Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [PHP-DEV] List comprehensions and generator expressions for PHP From: nikita.ppv@gmail.com (Nikita Popov) On Thu, Jun 28, 2012 at 2:43 PM, Sebastian Krebs wrote: > Hi, > > Whats the difference to the (already existing) function array_map() (except > the syntax and one more new keyword)? > >> $firstNames = array_map(function($user){return $user->firstname;}, > $users); > > Don't want to rewrite every example you gave, but you probably see my point. They are roughly the same. List comprehensions are basically a way of mapping + filtering. Their main advantage is a dedicated, more concise syntax and also the ability to combine multiple maps and filters in one expression. Also PHP is not a particularly lambda-y language in general; using array_map for that purpose would feel "wrong" to me. I'd probably rather write out a full foreach loop than use map. Generator expressions additionally bring the concept of lazy evaluation into the mix. This is useful in several situations, e.g. when processing large amounts of data, like log files. The following Python example is taken from http://www.dabeaz.com/generators/Generators.pdf: wwwlog = open("access-log") bytecolumn = (line.rsplit(None,1)[1] for line in wwwlog) bytes = (int(x) for x in bytecolumn if x != '-') print "Total", sum(bytes) The code is written as a set of simple operations, which are all defined in terms of iterating a whole file/list. But the actual execution is lazy, thus basically applying all the operations in a "pipeline". So the whole potentially multi-gig log file is never loaded into memory. The same could be done in PHP, just a different syntax :) Nikita