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

[eluser]wiredesignz[/eluser]
@Sophistry, Did you actually download the library and read through.

This does have potential by allowing a user defined function to be asigned to a $variable. (not the result, but the function itself)
#12

[eluser]sophistry[/eluser]
hi wired, thanks for the feedback:

yes, i downloaded the code, read thoroughly, read the graham thing on power=succinctness, studied the wikipedia page on functional programming and read one of the tutorial pages linked from the wikipedia page.

i understand the theory. i know many of the concepts are available already in PHP (c.f., call_user_func(), create_function(), and the ability to assign functions to variables which PHP calls variable functions variable functions in php manual:

i see how the FP library wraps up those disparate elements into a package that approximates FP in toto.

i see in general how the FP coding approach is strikingly different from a procedural and the OO approach.

what i am missing is a concrete example of how adopting this approach will:

a) make me a better/faster/more efficient programmer
b) make my code more expressive and "succinct" without losing clarity
c) illuminate my current CI projects - especially this one because this is the "language" we all use here (in addition to PHP of course)
#13

[eluser]Randy Casburn[/eluser]
I completely agree with sophistry's points here. (Wired - hang in there - I acknowledge you point below).

Each time this stuff comes up it reminds me of regular expressions:
Code:
// return unique lines
(?<=,|^)([^,]*)(,\1)+(?=,|$)
vs. "expressive", "succinct", "without losing clarity", PHP
Code:
// return unique elements (lines)
array_unique($array);
Both of these have their place and balance of performance trade offs, etc. RegEx are so very powerful, efficient, and flexible. Right? Are they realistically "expressive" or "clear"?
=====
From my point of view I would like to see this...

One of the compelling nuances of this abstract approach (FP) is the possibility of providing the devs this awesome ability that JS has of dealing with abstract function arguments. By this I mean function arguments that aren't set into concrete at declaration. The dev can use function arguments in any order regardless of the function declaration, and use the arguments they chose and ignore the one's they choose not to use.

Could you imagine a function like:
Code:
function label_new_if_today($date, $date_format="Y-m-d", $label="(New Project)")
{
    return ($date === date($date_format)) ? $label : null;
}

Being called thus: label_new_if_today($label="(Old Project)"); ?

Or how about this...
Code:
function constructQuery($statement,$from,$where,$limit,$orderBy){
   ...
}

constructQuery($statement="select",$orderBy="firstname",$from="customers");

...

Then the beauty in the flexibility (that you so aptly see WiredByDesignz) would so grand it would undeniable.

Unfortunately, no one has been able to achieve this with PHP. (AFAIK)

=======
The rest of the theory is just academic to until a hard core WIIFM demonstration is provided.

Randy
#14

[eluser]wiredesignz[/eluser]
Anything that allows more abstraction catches my attention.

Consider the possibilites if APPPATH and BASEPATH were changed to use variable functions,
$apppath('libraries/Session'.EXT) as an example would fall nicely inline for cross loading libraries from multiple applications or modules.

Just a thought.
#15

[eluser]Jamongkad[/eluser]
[quote author="sophistry" date="1215459146"]
it seems to me that there must be a more compelling example for adopting FP than string output and concatenation...
maybe something that dramatically shifts the coding architecture?
some better way of working in teams, splitting up large problems?
making unit testing easier?
maybe there is a particular problem domain where FP is better suited? maybe an event-driven system?

sorry to be a pest, but you seem like a smart coder and i'd like to know what you are thinking.[/quote]

Oh of course not I don't mind answering questions, and the points you brought up are legitimate and I will try to answer them to the best of my ability. FP as I see is another programming paradigm that has been around for many decades now. Admittedly the library has been abandoned since 2001 and I dusted it off just recently. So many of the concepts present in the library have been already been implemented with the present version of PHP or are about to be implemented.

Although I think your line of questioning goes beyond the limitations of the library and tries to scratch the surface of what problem set FP is best suited for. I would like to direct you and the rest of the CI community to this link. In it's heart FP is a paradigm that tries to evaluate a programming problem set mathematically.

[quote author="sophistry" date="1215459146"]
maybe something that dramatically shifts the coding architecture?
[/quote]
Alas I'm not smart enough to predict that haha, the library at the moment is very limited and touches lightly on the list building and manipulation abilities of Lisp and Scheme. But in terms of code structure in FP style I find it more elegant than traditional OOP. Please consider watching Douglas Crockford's lectures and notes on how you can write FP style code in Javascript. Something that I wish we could do in PHP. Oh wait there is a patch for that.

[quote author="sophistry" date="1215459146"]
some better way of working in teams, splitting up large problems?
[/quote]
Hmmm ever heard of the smug Lisp weenie? but seriously I would like to think that and this is my personal opinion that FP was created in it's heart for the programmer who does not want to work with teams.

[quote author="sophistry" date="1215459146"]
making unit testing easier?
[/quote]
Off the top of my head and I could be wrong, unit testing is already good as it is.

[quote author="sophistry" date="1215459146"]
maybe there is a particular problem domain where FP is better suited? maybe an event-driven system?
[/quote]
AFAIK event-driven systems generally rely on callbacks. But you could use FP and apply the concept of CPS or Continuation Passing Style. Although this style works very well with languages that have first class functions, proper lexical scoping, and closures.
#16

[eluser]Jamongkad[/eluser]
[quote author="Randy Casburn" date="1215481359"]
From my point of view I would like to see this...

One of the compelling nuances of this abstract approach (FP) is the possibility of providing the devs this awesome ability that JS has of dealing with abstract function arguments. By this I mean function arguments that aren't set into concrete at declaration. The dev can use function arguments in any order regardless of the function declaration, and use the arguments they chose and ignore the one's they choose not to use.

Could you imagine a function like:
Code:
function label_new_if_today($date, $date_format="Y-m-d", $label="(New Project)")
{
    return ($date === date($date_format)) ? $label : null;
}

Being called thus: label_new_if_today($label="(Old Project)"); ?

Or how about this...
Code:
function constructQuery($statement,$from,$where,$limit,$orderBy){
   ...
}

constructQuery($statement="select",$orderBy="firstname",$from="customers");

...

Then the beauty in the flexibility (that you so aptly see WiredByDesignz) would so grand it would undeniable.

Unfortunately, no one has been able to achieve this with PHP. (AFAIK)

=======
The rest of the theory is just academic to until a hard core WIIFM demonstration is provided.

Randy[/quote]

I have to agree to a certain extent that it is the limitations of the language itself. I guess this is the reason why this patch tries to solve it check it out here oh you need php5.3.0 and you need to recompile it. Much of a hassle I know but it allows you much of the same power that is afforded to you via JS.

To answer your question about variable arguments to a function. I wrote a function that tries to at least capture the spirit of what you're looking for. Consider the example below
Code:
//Major functions in the library use the func_get_args() method as it allows us captures a number
//of different arguments.

//function takes a variety of parameters for our validation library.
function validation_parameters($params) {
  $params = func_get_args();
  foreach($params as $value)
   $rules[] = $value;
   //our hypothetical validation lib.
   this->sample_validation_lib->set_rules($rules);
   return $this;
}

//This approach is very similar on how we apply a function to a list of arguments in Scheme.
//Qoute construct that takes a function as the first argument and the rest are
//the parameters for our function. Unqoute executes the stuffs the parameters into the function.
$x = qoute('validation_parameters','user_name','last_name','age','sex','email','location');
return unqoute($x);
#17

[eluser]sophistry[/eluser]
Thanks all for the responses to my questions. I found this well-written paper about FP from the wikipedia page. Here is the link. I've found it helpful to sit back for a few minutes and think about the big picture while reading this. FP really is a different way of thinking about structuring programs, so you can not just drop this library in and have some neat new features in CI.

Here is a quote from the paper linked above that describes modularity and glue as key aspects of a change to an FP mindset...
Quote:It is now generally accepted that modular design is the key to successful pro-
gramming, and languages such as Modula-II [Wir82], Ada [oD80] and Standard
ML [MTH90] include features specifically designed to help improve modularity.
However, there is a very important point that is often missed. When writing
a modular program to solve a problem, one first divides the problem into sub-
problems, then solves the sub-problems and combines the solutions. The ways
in which one can divide up the original problem depend directly on the ways
in which one can glue solutions together. Therefore, to increase ones ability
to modularise a problem conceptually, one must provide new kinds of glue in
the programming language. Complicated scope rules and provision for separate
compilation only help with clerical details; they offer no new conceptual tools
for decomposing problems.

One can appreciate the importance of glue by an analogy with carpentry.
A chair can be made quite easily by making the parts - seat, legs, back etc. -
and sticking them together in the right way. But this depends on an ability
to make joints and wood glue. Lacking that ability, the only way to make a
chair is to carve it in one piece out of a solid block of wood, a much harder
task. This example demonstrates both the enormous power of modularisation
and the importance of having the right glue.

Now let us return to functional programming. We shall argue in the remain-
der of this paper that functional languages provide two new, very important
kinds of glue. We shall give many examples of programs that can be modu-
larised in new ways, and thereby greatly simplified. This is the key to functional
programming’s power - it allows greatly improved modularisation. It is also the
goal for which functional programmers must strive - smaller and simpler and
more general modules, glued together with the new glues we shall describe.
#18

[eluser]Randy Casburn[/eluser]
@sophistry -- Thanks for the article. It was very interesting, very academic, and not very pragmatic. I agree with your point above.

The term showed up! "Referentially transparent" is the term that is used to broadly describe the attribute I attempted to describe in my post above. Gosh, I'm old. I don't think I've read that since grad school about the time that paper was written!

To solidify your point about 'you can not just drop this library in...', here is another resource:
http://www.cs.nott.ac.uk/~gmh/faq.html
Check out the programming languages, when they were written and the descriptions of support, etc.

We're using a 4GL (if anyone remembers that term) that is purposely abstracted from the bowels of the 'system' so we don't have to worry ourselves over set theory or the implementation of this:
Code:
integrate f a b = integ f a b (f a) (f b)
integ f a b fa fb = cons ((fa+fb)*(b-a)/2)
(map addpair (zip (integ f a m fa fm)
(integ f m b fm fb)))
where m = (a+b)/2
fm = f m


in our web application for goodness sake.

I realize many of you are computer science students who are studying this stuff and are mezmorized by the sheer possibilities offered. I'm only suggesting that PHP+CI is not the best place to explore those possibilities.

@JomongKad -- Thanks for bringing this discussion and rekindling some very long lost thoughts. These days I spend more time actually creating solutions than thinking about what might be a better way of creating solutions.

Regards,

Randy
#19

[eluser]Jamongkad[/eluser]
Thanks Randy, I'm more that happy to shake a few things up every once in a while.
#20

[eluser]sophistry[/eluser]
so... Jamongkad... when you are feeling better... can you drop some super (applied) science on us and give a real-world example of how you use this library in your CI development process?

Maybe you have something that demonstrates an FP concept that is a little more (ahem) robust than string concatenation?

much thx :-)




Theme © iAndrew 2016 - Forum software by © MyBB