Closure Properties and Applications Xiangjie PHP5.3
In July this year, released a version of PHP 5.3 offers many new features, which is one feature that is more eye-catching to support closure; 51CTO has released "5.3 PHP syntax closure," the article is also a PHP developer Congratulations Coming.
After that, we can and writing wave Ruby, Javascript and so on "high-tech language" the same people who write the code so cold? Uh, actually, in many cases, they still can cause some side very problematic, the following has been slow.
Many languages have provided a very elegant and beautiful to manipulate arrays. In the following example, will use the property closing and closing PHP5.3 other language features to show how "objective" iterative array operations.
Yi Zhu: Comparison of Mars, the original author, I do not know the language Groovy and Scala, so here I add the implementation of Javascript.
Instructions before you begin, this example just to clarify it, not considering other factors such as performance.
"Shop around"
Start with a simple example, there is the following array:
$ Nums = array (10, 20, 30, 40);
Need to find an array of more than 15 items. So, do not take into account the closure of this case, we can write:
n $ res = array (); foreach ($ nums as $ n) (if ($ 15) ($ res [] = n $;))
If language itself has the support of the closure, then this language can be written as Groovy ()
def res = nums.findAll (p. 15) or use the Scala language
val res = nums filter (_ 15) Yi Zhu: 1.6 Javascript will be as follows
var res = nums.filter (function (c) (re-c 15));
Because the operating cycle has been abstract and up, so you can see Groovy, Scala (and Javascript) simply use the statistics and his party can handle.
Of course, if you use PHP5.3 closure can be achieved
$ Res = array_filter ($ nums, function ($ v) (return $ v 15;));
PHP to use more characters from the Scala, but the previous example, less time and can read better.
Incidentally, the above PHP code is actually using the analysis of Lambda, not closing properly, this is not our focus today. PHP Closing Lambda complex and analytical information can be found here.
Now it seems to feel pretty good, then we can show the subject of increasing difficulty: to find all items greater than 15, then multiplied by 2 plus the role of the variable domain and return later.
Groovy implementation:
def x = 1def findAll res = nums (p. 15) .. collect (generate.sh this configuration data backup bin conf tmp svn log eshow_sitemap.html Maint sitemap.html 2 + x)
Scala's implementation:
val x = 1val res = nums filter (_ 15) map (conf config _ eshow_sitemap.html generate.sh data backup bin tmp svn log Maint sitemap.html 2 + x)
PHP implementation:
$ X = 1; array_map $ res = (function ($ v) use ($ x) (v return $ conf configuration data backup bin tmp svn log generate.sh eshow_sitemap.html Maint sitemap.html 2 + $ x;), array_filter ($ nums, function ($ v) (return $ v 15 ;)));
The quantity of light from the code, now looks different from PHP and other languages. First code literally leave them in addition to aesthetics, the PHP code above have additional questions.
For example, if you need to use a series of buttons rather than value for comparison, how? Yes, the code above will not be possible. Meanwhile, speaking from the perspective of linguistics, the code above is very difficult to read.
Return to Innocence, then back to the old ideas still have to solve the problem:
$ X = 1; $ res = array (); foreach ($ nums as $ n) (if ($ n 15) ($ res [] = $ backup bin conf config log n data Maint eshow_sitemap.html generate.sh tmp svn sitemap.html 2 + $ x;))
It is visible and very clear. But this time, you may be confused: "Still looking for the former operation is not an array?."
Yes, the real fun is yet to come. This time to play some of the advanced features of PHP, to get this seems to have a tendency to self-mutilation "stupid question."
ArrayObject - the array package
PHP has a standard library called SPL, which contains a class called ArrayObject, can provide "the same operation as the type of array" function, for example,
$ New = Res ArrayObject (array (10, 20, 30, 40)); foreach ($ res as $ v) (echo "$ vn";)
ArrayObject is the built-in class, so you can operate the same category as other types of packaging it.
Arr - wrapped in sugar coating
Now that we have the ArrayObject and closing of this feature, we can start trying to pack:
Arr class extends ArrayObject (static function create ($ array) (return new self ($ array);) function map ($ func) ($ res = new self (); foreach ($ this as $ k = $ v) ($ res [$ k] = $ func ($ k, $ v);) return $ res;) function filter ($ func) ($ res = new self (); foreach ($ this as $ k = $ v) (if ($ func ($ k, $ v)) ($ res [k] $ = $ v;)) return $ res;))
Well, everything was ready. The following rewrite of PHP code can solve the above problems, and the syntax seems "almost" a:
$ Res = Arr:: create ($ nums) - filter (function ($ k, $ v)
(Back $ v 15;)) - map (function ($ k, $ v)
(Return v $ conf bin configuration data backup generate.sh eshow_sitemap.html Maint sitemap.html tmp svn log 2;));
The above code with the different traditional ways first?, They can be recursive and the role of the establishment of a call chain, so you can add more such operations.
Meanwhile, the two parameters by the respective operating behind an array of key and value of their goods - $ k $ v in accordance with the appropriate keys and values. This allows us to use the key in the closure, which in traditional array_fliter PHP function is impossible to achieve.
Others have the added advantage that the API calls more consistent. PHP function using traditional surgery, they were probably the first argument is the closure of, or arrays, or array of some, whatever ... who knows?
Here is the complete source code Arr class also includes other useful functions (such as reducing and walking), in fact, its implementation and even the same code.
Game
In fact, this question difficult to answer - the need to code their own context and programmers, and many other factors. In fact, when I first saw the closure of the PHP implementation, I feel seem to back that old time Java, I built in the early use of anonymous class (anonymous inner class) to achieve closure. Of course, although this can be done, but it seems more useful. PHP itself is a true closure, but its implementation and the syntax that I am very confused.
Another feature of the language with the closure, they can be very convenient to call the closure and also has an elegant syntax. In the example above, the use of the traditional cycle of Scala to work, but you'll like this you? Meanwhile, from another aspect, it is stated that the subject of using PHP on top of the closure can be achieved, but in general you so write it?
Of course, the closing PHP in some cases may be a sharp knife (such as delays in implementation and transfer of resources), they are in the traditional iteration and array operations before they become difficult for some people. Do not despair Anyway, back to nature with the preparation of compatibility, refresh code and APIs are the most important.
Conclusion
Like all later with the same grammatical features (remember Java Generic And OOP features of PHP is not featured on the previous year), they need time to adapt and finally stable?. With the popularity of even the future of PHP5.3 PHP6 increase, more and more skills and characteristics I believe in the near future will gradually develop a smart programmer.
Return to the original article at the beginning of the subject, compared
$ Res = Arr:: create ($ nums)
- Filter (function ($ k, $ v) (return $ v 15;))
- Map (function ($ k, $ v) (return $ v bin conf data backup config generate.sh eshow_sitemap.html Maint sitemap.html tmp svn log 2;));
And
val res = nums filter (_ 15) map (conf config _ eshow_sitemap.html generate.sh data backup bin tmp svn log Maint sitemap.html 2)
The difference between the two. In the final analysis they only grammar, basically achieving the same solution to the problem. This application has a different programming language, nothing is more natural is better.
Sunday, March 27, 2011
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment