Having now released my own programming language, FELT, and learned a lot about this and that in the process I have of late, in the evenings, been struggling to reconcile my love of LISP and how simple FELT makes some PHP coding task leaner and meaner with the fact that I still have to use PHP for my day job.
In my language, FELT, I have used the square brackets to define a "normal" array and curly braces to define a "key-value" array, mainly because this is identical to JSON format and anybody familiar with Javascript coding just won't have any issues getting to grips with that now will they!
Let's take some simple examples of FELT code:
Um. Okay.... but what "use" is it I hear you ask? Well, I called it "a" as that's shorter to type than "array", there's the clue. It's for making array creation simpler and easier at the small expense of a function call... I go for neater code and optimise only when there is no choice! Rule #1 of optimisation as we all know is of course, "Don't do it!".
Being something of a "functional programmer" I try to use the PHP functions that equate to FP idioms as far as I can as it helps me think better, for example, "fold" is "array_reduce" in PHP terminology, "map" is "array_walk" and so it goes on.
As much as I like PHP, it is only the run-time that I like, for my money it is too verbose like many ECMA script related languages as well and the language does not really allow you to shorten the actual way you create code in the way that Lisp or Haskell let you do, so I am constantly looking for ways to reduce my keystroke output and still get work done that remains readable to others.
The simple "a()" function turned out to be a real little cracker as I will show you now... I do a lot of Drupal work and as anybody that has done their share of Drupal FAPI (forms API) coding, you soon get a little tired of those arrays!
This is maybe too short to appreciate but which would you say is easier to enter, less prone to typos and easier to read?
Going back to my initial FELT example, we could code that:
So, there you are, with a little lateral thinking, PHP can be made a little easier to work with. It's the small things that count and I don't think I will ever use the array() function again! Oh yes, because of the way that they work, you can of course nest them to your heart and codes content.
As an exercise, create a file with the above code and then stick this at the end:
Emacs the Viking.
In my language, FELT, I have used the square brackets to define a "normal" array and curly braces to define a "key-value" array, mainly because this is identical to JSON format and anybody familiar with Javascript coding just won't have any issues getting to grips with that now will they!
Let's take some simple examples of FELT code:
(defvar simple-array [1 2 3 4]) (defvar simple-map {:name "Eric" :age 42 :occupation "Viking Hacker"})When FELT has done its thing, we get the following PHP code,
$simple_array = array(1, 2, 3, 4); $simple_map = array('name' => "Eric", 'age' => 42, 'occupation' => "Viking Hacker");So... I got to thinking about how data is transformed in PHP, not by the code you write but by the actual PHP instructions themselves, what avenues are there for making the declaration of arrays much shorter and neater? The obvious thing of course is to write a little variadic wrapper function that would take all of its arguments and return an array and then I had a sudden Eureka! moment and this little gem popped out of my brain and onto the page:
/* The coolest shortest PHP function I will ever write, * by Sean Charles aged 47 and a bit! */ function a() { return func_get_args(); }That's it! So why is it cool? It makes use of the fact that it declares no formal parameters and that PHP doesn't care of you pass excess parameters to a function, whereas if you pass too few you will be told! Secondly, it uses the func_get_args() function as the return data and therefore we have created a simple little function that magically converts any number of parameters you give to it into an array.
Um. Okay.... but what "use" is it I hear you ask? Well, I called it "a" as that's shorter to type than "array", there's the clue. It's for making array creation simpler and easier at the small expense of a function call... I go for neater code and optimise only when there is no choice! Rule #1 of optimisation as we all know is of course, "Don't do it!".
Being something of a "functional programmer" I try to use the PHP functions that equate to FP idioms as far as I can as it helps me think better, for example, "fold" is "array_reduce" in PHP terminology, "map" is "array_walk" and so it goes on.
As much as I like PHP, it is only the run-time that I like, for my money it is too verbose like many ECMA script related languages as well and the language does not really allow you to shorten the actual way you create code in the way that Lisp or Haskell let you do, so I am constantly looking for ways to reduce my keystroke output and still get work done that remains readable to others.
The simple "a()" function turned out to be a real little cracker as I will show you now... I do a lot of Drupal work and as anybody that has done their share of Drupal FAPI (forms API) coding, you soon get a little tired of those arrays!
This is maybe too short to appreciate but which would you say is easier to enter, less prone to typos and easier to read?
array( 1, 20, array( "name", "Sean"), 3.14 )or this....
a( 1, 20, a( "name", "Sean"), 3.14)Well, my money is on the second one, it's quicker to read and write and of course "a" means "array". Getting back to Drupal, and PHP in general I guess, you also need to use key-value arrays (hash-maps et al.) and so I then write this little function to complement "a", I called it "kv" for "key-value"...
/* The second short coolest function I will ever write in PHP, * by Sean Charles, aged 47 and a bit. */ function kv() { $args = func_get_args(); $kva = array(); while(count($args)) { $kva[array_shift($args)] = array_shift($args); } return $kva; }What "kv" does is blindly chew its way through the parameters that are passed to it and create an array where the first pair of values forms the first KV pair until all the values are used up. I don't bother to check if there are an even number of items to begin with because PHP is pretty slack and loose at the best of times and if you check, array_shift() returns NULL when it runs out of stuff to if you do pass an odd "key" at the end then it will just be assigned the value of NULL.
Going back to my initial FELT example, we could code that:
$simple_array = array(1, 2, 3, 4); $simple_map = array('name' => "Eric", 'age' => 42, 'occupation' => "Viking Hacker");in PHP to begin with like this:
$simple_array = a(1, 2, 3, 4); $simple_map = kv('name', "Eric", 'age', 42, 'occupation', "Viking Hacker");Which I think is leaner and cleaner. And whilst I was on a roll I trotted out these as well just to make raw PHP feel a little easier on my mind:
function map() { $args = func_get_args(); return call_user_func_array("array_map", &$args); } function walk() { $args = func_get_args(); return call_user_func_array("array_walk", &$args); }They are in the vein of the PHP version of underscore.js, which I have used a lot and think very highly of indeed although I personally think the PHP version feels clunky but that's the fault of PHP not the author of that clever piece of coding. The JS version is a joy to use!
So, there you are, with a little lateral thinking, PHP can be made a little easier to work with. It's the small things that count and I don't think I will ever use the array() function again! Oh yes, because of the way that they work, you can of course nest them to your heart and codes content.
As an exercise, create a file with the above code and then stick this at the end:
var_dump( a( 1, kv("name","Sean","mood","thinking"), a( 10, a(20,30,40)))));All the best.
Emacs the Viking.
Comments
Post a Comment