Welcome Guest, Not a member yet? Register   Sign In
Functional PHP Extension
#1

[eluser]Jamongkad[/eluser]
Functional PHP Extension

I all it's been a while since my last contribution to this community. But I feel that CI has given us so much in terms of developer happiness. It actually made me appreciate PHP again. Anyways here again is my small contribution to the community.

Just a few months ago I started to appreciate the power and expressiveness of Functional Programming, my work in using languages such as Scheme has opened my mind to new paradigms. I wanted to know if languages such as PHP can atleast scratch the surface of the power of FP.

Unfortunately in order to do anything remotely related to FP the language must support higer order functions(functions as first class citizens) which PHP does not. Lexical Scoping(which PHP does not execute as elegantly as other languages). These and a lot more are required, thus we come to the this humble library. Please note this library is more of a supplement and a helper to your code. I know it helped me a whole and I hope it does the same for you.

I'm currently the maintainer and active contributor to this library and I would like to share with the community the possiblity of writing code in a more functional manner and practicing Paul Graham's theory on power

So without further ado I'm pretty amped to demonstrate the power of this library with a few examples.
In order to load this library one must simple place it in their helpers folder. Change the file name to functional_helper.php and simply autoload it.

Anonymous Functions
The main crux of this library and the reason why I use it. One would want to utilize the power of unnamed functions for tasks that do not generally require the use of named functions. Small throw away tasks that one can utilize.

In order to invoke the use of anonymous functions...now I know some of you might be thinking that PHP has create_function. And I'm here to tell you that in the true spirit of FP anonymous functions immediately return their expressions. Create_function in all it's awkwardness defines it's expressions procedurally.

Here I define two lamdba functions.
Code:
$plus5 = lam('$x','$x + 5');
$times2 = lam('$x','$x * 2');

$combine = $plus5($times2(7));
//the result will be 19
Awesome isn't it? The library is well commented and you can find more examples I cooked up.

Scheme like Begin
This function was borrowed from Scheme and it allows the one to invoke functions by sequence. The function was inspired by the work of Dikini.net and his contributions.

Code:
function say() {
   echo "My";
}

function my() {
   echo " name ";
}

function name() {
   echo " is Mathew Wong.";
}

echo begin(
        say(),
        my(),
        name()
      );

//which would yield "My name is Mathew Wong."

Concat is the missing array reduction operation in PHP. It's closely related to implode.
Code:
$arr = array(
         'My name is Mathew Wong',
         'Ian Kjos is the original author of the Functional PHP library',
         'This is his implementation',
         'I love Functional programming!'
        );
echo concat($arr,'<br/>');
//Which will result....
    My name is Mathew Wong
    Ian Kjos is the original author of the Functional PHP library
    This is his.....
    I love Functional....

These trivial examples obviously do not do the library and it's original author justice. But for the sake of brevity I wanted to show you guys a taste of the expressive power that is contained in this library. There are alot more functions and combinators in the library that if I were to list them all one page would not be enough for this post.

Thank you and if there any questions I'll be more than happy to entertain them.


Messages In This Thread
Functional PHP Extension - by El Forum - 07-06-2008, 08:30 PM
Functional PHP Extension - by El Forum - 07-06-2008, 11:59 PM
Functional PHP Extension - by El Forum - 07-07-2008, 12:15 AM
Functional PHP Extension - by El Forum - 07-07-2008, 12:48 AM
Functional PHP Extension - by El Forum - 07-07-2008, 12:58 AM
Functional PHP Extension - by El Forum - 07-07-2008, 01:20 AM
Functional PHP Extension - by El Forum - 07-07-2008, 01:44 AM
Functional PHP Extension - by El Forum - 07-07-2008, 07:51 AM
Functional PHP Extension - by El Forum - 07-07-2008, 07:55 AM
Functional PHP Extension - by El Forum - 07-07-2008, 08:32 AM
Functional PHP Extension - by El Forum - 07-07-2008, 09:53 AM
Functional PHP Extension - by El Forum - 07-07-2008, 10:15 AM
Functional PHP Extension - by El Forum - 07-07-2008, 02:42 PM
Functional PHP Extension - by El Forum - 07-07-2008, 07:27 PM
Functional PHP Extension - by El Forum - 07-07-2008, 10:19 PM
Functional PHP Extension - by El Forum - 07-07-2008, 10:53 PM
Functional PHP Extension - by El Forum - 07-08-2008, 09:10 AM
Functional PHP Extension - by El Forum - 07-08-2008, 11:21 AM
Functional PHP Extension - by El Forum - 07-08-2008, 06:33 PM
Functional PHP Extension - by El Forum - 07-08-2008, 07:02 PM
Functional PHP Extension - by El Forum - 07-10-2008, 11:13 PM



Theme © iAndrew 2016 - Forum software by © MyBB