📜 ⬆️ ⬇️

If PHP were to highlight lists and generators

A few weeks ago, the PHP mailing list had an interesting sentence with someone's hack, which describes generators, generators, expressions, and list highlighting . It is not surprising that some people are not sure about the prospects of this proposal, as well as core developers and PHP users. I would like to share a few ideas and try to convince these guys.

Declarative XML parsing


The quickest example I could come up with is a simple interface for parsing large XML files.

<?php function *readXML($file) { $r = new XMLReader; $r->open($file); while($r->read()) { yield $r; } } function *filterNodes(callable $predicate, $nodes) { foreach($nodes as $node) if($predicate($node)) yield $node; } function matchName($name) { return function($node) use($name) { return $node->name === $name; }; } 

And a possible option for parsing using these functions.

 <?php function *getArticlesFromXML($file) { foreach(filterNodes(matchName('article'), readXML($file)) as $node) yield nodeToArticle($node); } function nodeToArticle($node) { // transform node return $node->name; } $articles = [foreach(getArticlesFromXML('data.xml') as $article) yield $article]; 

I am sure that those who have already used XMLReader will understand how the control over parsing was taken from the while , and “adjusted” to the declarative style using higher order functions.
')

Simple iterator decoration


This is not a general pattern, but when I have to communicate with external services I like to wrap their answer in two parts:



As I said, I did not see a similar pattern in someone else’s code, but for me this is a frequent pattern that has become easier with the use of generators.

 <?php // inside some class public function getResult() { return SomeCustomIterator($this->results, new SomeCustomDecorator); } // with generators public function *getResults() { foreach($this->results as $result) yield new SomeCustomDecorator($result); } 


Again about more expressive API


Lazy computing, the fundamental principle of generators, an underused programming style in most PHP developers, and I would like to see how people start to delve into it if this feature falls into PHP.

It’s not hard to imagine people using mixed styles (OOP and procedural programming). They could start with something simple, for example:

 <?php function select() { return func_get_args(); } function from($source) { return $source; } function where() { return func_get_args(); } function *query($select, $from, $where) { foreach($from as $element) { foreach($where as $predicate) { if(false === $predicate($element)) continue 2; } $fields = array(); foreach($select as $selector) { $fields[] = $selector($element); } yield $fields; } } $articles = query( select(property('name')), from(readXML('data.xml')), where(matchName('article')) ); // where property could be something as simple as function property($name) { return function($node) use ($name) { return $node->{$name}; }; } 

Or maybe even build something like LINQ? Not that which already exists .

Looks like python


There were several comments in which people noticed this aspect and were against it. I did not understand then and now I do not understand why this is bad. I, for example, consider this good news: no language is perfect and each of them should try to include features that have proven useful in others (of course, given the possibility of implementation).

Test it yourself


If you have read the article before these lines, and have not lost interest, I recommend going to the terminal and executing the following commands before you close the tab.

 $ git clone -b addListComprehensions https://github.com/nikic/php-src.git $ cd php-src $ ./buildconf $ ./configure $ make cli $ ./sapi/cli/php some-example-file.php 

Source: https://habr.com/ru/post/147885/


All Articles