![]() |
Higher Order PHP: Map and Functional Extension Helper - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22) +--- Thread: Higher Order PHP: Map and Functional Extension Helper (/showthread.php?tid=11461) Pages:
1
2
|
Higher Order PHP: Map and Functional Extension Helper - El Forum - 09-09-2008 [eluser]Jamongkad[/eluser] Hey guys, It's been quite sometime since I last logged into the CI forums. It's quite nice to see that there are loads of libraries and plugins for CI. I'm very excited with it's development. Anyways here's my small contribution to the CI community. The inspiration of the Map library are from my readings of Higher Order Javascript and Higher Order Perl. Languages that have Functions as first class citizens. With characteristics such as the aforementioned one can write beautiful expressive programs. My goal is to atleast apply that characteristic to PHP code(take note if one works with PHP for quite some time I use the word "atleast" with utmost intention.) Enough talk let's get some examples running! Let's take a basic iteration case. You have a table filled with user names and you want to output it on the web page. Code: //Controller Code: //Views In our line of work we might do iterations like these if not more than once but several times throughout the application. Personally some of you might disagree with the following method I'm about to use but please bear with me that I find "foreach" construct, although good for many iteration purposes, can be abstracted to support larger concepts.(although in truth the guts of the Map library uses array_map. For reasons being that in Lisp when you use the map function it returns a list with the new values applied by the function. I'm trying to keep the concept of Map pure as much as possible.) And this is where the Map library comes in. Here we have our whatever controller... Code: //Controller Code: //Views Now isn't that much cleaner? Please bear in mind in the coming weeks I'll be posting new code examples on how to take this library to the limit and how you can use it to write shorter more expressive programs in PHP. I'll be posting more examples as the weeks come by but let me show how we can use the Map function in tandem with the Functional PHP extension to practice the concept of Higher Order Functions Code: //Controller Code: //Views will output 100 Note: To those of you who already use the Functional PHP extension please download the newest version below. Map Functional Extension Helper Higher Order PHP: Map and Functional Extension Helper - El Forum - 09-10-2008 [eluser]xwero[/eluser] How much damage does it do, performance wise? Creating functions is very slow i am told. Higher Order PHP: Map and Functional Extension Helper - El Forum - 09-10-2008 [eluser]Colin Williams[/eluser] Quote:Now isn’t that much cleaner? No. Because when I need to change how $display_users looks, I have to change my controller, and not a view, where "how stuff displays" should go. Higher Order PHP: Map and Functional Extension Helper - El Forum - 09-10-2008 [eluser]Jamongkad[/eluser] [quote author="xwero" date="1221045214"]How much damage does it do, performance wise? Creating functions is very slow i am told.[/quote] The old library which I posted a few months ago had did alot of damage in terms of performance. The Lam construct was akin to create_function in all intents and purposes. A much smarter man than I am "MetaPundit" pointed out to me that the reason there was such a hit in performance is because PHP's garbage collector does not garbage collect the create lambda functions. Thus polluting the function namespace. So what was the solution? PHP's GC garbage collects objects! So it was simple convert lam into an object. Bam! No more memory issues. Higher Order PHP: Map and Functional Extension Helper - El Forum - 09-10-2008 [eluser]wiredesignz[/eluser] Can't say I like this method either, PHP already provides a fantastic set of OOP methods to abstract data between controller and view, and they are a lot easier to understand. Higher Order PHP: Map and Functional Extension Helper - El Forum - 09-10-2008 [eluser]Jamongkad[/eluser] [quote author="Colin Williams" date="1221045687"] Quote:Now isn’t that much cleaner? No. Because when I need to change how $display_users looks, I have to change my controller, and not a view, where "how stuff displays" should go.[/quote] Perhaps you and I have differing views on how we display data. Like I said in my above post I like to blur the line between data and procedures. Therefore my library encourages the practice of using functions as "displays" of data and functions to do stuff to them. Higher Order PHP: Map and Functional Extension Helper - El Forum - 09-10-2008 [eluser]Jamongkad[/eluser] [quote author="wiredesignz" date="1221046350"]Can't say I like this method either, PHP already provides a fantastic set of OOP methods to abstract data between controller and view, and they are a lot easier to understand.[/quote] Agreed but please bear in mind that this lib does not only apply to "views". Think of it as a means of combining functions to produce complex data forms. Higher Order PHP: Map and Functional Extension Helper - El Forum - 09-10-2008 [eluser]Colin Williams[/eluser] Think of it as a way to really, really screw with your coworkers and others maintaining your code ![]() Higher Order PHP: Map and Functional Extension Helper - El Forum - 09-10-2008 [eluser]xwero[/eluser] Jamongkad i think you have to come up with an examples where it's easier to use your library/helper than using 'plain' php. I think the display_users example is a bad one because you retrieve too much data from the db and you are putting display logic in the controller. For the numbers example, why would an array with a value 7 return 100?? It has no other use than to show the adding of a conditional argument at runtime. Higher Order PHP: Map and Functional Extension Helper - El Forum - 09-10-2008 [eluser]Jamongkad[/eluser] [quote author="xwero" date="1221050206"]Jamongkad i think you have to come up with an examples where it's easier to use your library/helper than using 'plain' php. I think the display_users example is a bad one because you retrieve too much data from the db and you are putting display logic in the controller. For the numbers example, why would an array with a value 7 return 100?? It has no other use than to show the adding of a conditional argument at runtime.[/quote] Perhaps you're right. I usually don't place display logic in the controller. Truth be known I like to place it in the models where I believe it belongs. Now about the 7 returning the 100. I just wanted to demonstrate the use of Higher Order functions. Combining the first procedure with another procedure at it's argument. One can demonstrate power and expressive of functional programming concepts using PHP. If you take a look at the Wikipedia link you can see the Python example returns the exact value. Granted yes there is nothing wrong using plain PHP. But my background in MzScheme has prompted me to write a library that could more or less emulate behavior that is present in the language. Is the the correct approach? To some it may be a bit confusing especially if you're used to doing things procedurally as opposed to using the FP approach. |