Re: understanding lambda



lawpoop wrote:
On Jun 17, 10:56 am, Jerry Stuckle <jstuck...@xxxxxxxxxxxxx> wrote:

What do you mean by "created dynamically"?
I mean it is created dynamically - on the fly.

What's an example of something in php that isn't created dynamically?
What does dynamic mean?


A non-lambda function is not created dynamically. It is created when the script is parsed. All functions in the script are created when the script is parsed. A lambda function is created when the script is executing - and is not created unless that statement is executed.

If you don't execute this statement, the function doesn't exist.

Isn't that also true of a regular function declaration? That if
function myFunc() {}
isn't executed, certainly myFunc() wouldn't exist as a function, would
it?
That can't therefore be a defining property of a lambda function,
right?


No. See above. Non-lambda functions are created at parse time, not execution time.

And the function only exists as long as the variable exists (unless, of course, you assign
another variable to this one). unset() $func and the function goes away.

This sounds like it harkens back to my original definition of a lambda
function being a function whose name is a variable , rather than a
hard-coded strnig.


No. You can have a real function name in a variable, also. But if you unset the variable, the function does not go away. It is still defined and can be used other times.

You create it like a variable, dynamically, "on the fly",( right? ).
You reference it like a variable. You unset it like a variable --
well, if it talks, walks, and acts like a duck, isn't it a duck? It's
name is a variable, no?


No. A lambda function has no name. The variable is the function.

This is different from non-lambda functions. In that case, the
functions are created when the file is parsed and exist for the life of
the script.

Okay, so what you are saying is that in the process of parsing the
file, any function declarations are parsed before variables are, so
regular function definitions cause functions to exist before any
lamdba functions ( or any other variable, for that matter ) are
parsed, and caused to exist. Have I got that right?


There are two steps to executing a PHP script. The first one is parsing the entire file. Fatal errors (i.e. syntax) will stop the parser. No statements in the script are executed.

Once the entire script is parsed, then the statements in the file are executed.

Parsing the file is the PHP (and most interpreted languages) equivalent of compiling/linking a program in languages such as C and C++. Executing the file is the equivalent of executing a compiled program.


So, if I can venture a guess, what you have meant by 'dynamic' or 'on
the fly' is "existence is not guaranteed throughout the life of the
script" ...?


It means the function is created at executing time and has the same lifetime as the contents of any variable.

Like Erwin, I have found no real use for lambda functions. I know they
exist, and I'm sure there is a valid use for them. I just haven't found it.

I've been thinking about this, and I can see a case where you might
want to pass the name of a function as an argument. I've wanted to do
this in the past, but I can't recall the exact situation presently.


Passing the name of the function is not the same as passing the function itself, as in lambda functions.

Basically, when you would want to specify the function by name as an
argument, you might want to use a lambda function. It doesn't help you
when you write the main code, but it can cut down tremendously when
writing functions.


This can be done without lambda functions.

Something like, say you wanted to caculate the volume of a arbitrary 3-
dimensional solid. You might be dealing with different solids, so you
would have a different method, or function, for calculating the
volume. If it's a sphere, you want 4/3 * PI * (radius * radius *
radius) , if it's a cube, you want length * width * height, if it's a
cone, you want ((h*pi)*r^2)/3 ( I think I've got that right ) -- so
three different functions for three different solids. You need some
way to decide which function to use.


That's not a problem. You can just pass the name of the function, i.e. <?php

function func1() {
echo "In func1\n";
}

function func2($f) {
echo "In func2\n";
$f();
}

$var = "func1";
$var();
func2($var);
func2("func1");
?>

func1() and func2() are not lambda functions. But by passing the name of fun1 to func2, I can call it.


One way to do it would be to have a function for each solid, and a
master function that calculates the c:

function calc_cube_volume( $length, $width, $height ) { ... }
function calc_cone_volume( $radius, $height ) { ... }
function calc_sphere_volume( $radius ) { ... }

Then, a master function

function calc_volume ( $arrMeasurements, $solid_type ) {
if ( $solid_type == "sphere" ) {
return calc_sphere_volume( $arrMeasurements['radius']);
}
if ( $solid_type == "cube" ) {
return calc_cube_volume( $arrMeasurements['length'],
$arrMeasurements['width'] , $arrMeasurements['width'] ) ;
}
...
}

But essentially what you're doing with the $solid_type argument is
just naming which function you want to use. The if ( $solid_type ==
"cube" ) is just matching a string with a function. If I wanted to add
a new solid -- say, a cylinder, I would take two steps: one to create
the function calculate_cylinder_volume(), and two, modify the
calculate_volume() function with an if statement to check to see if
the $solid_type is cylinder, and then execute the appropriate
function.

Wouldn't it be nice if you could just specify which function you want
directly, instead of creating a string::function association with if
statements? Well, it looks like that's the kind of job a lambda
function is cut out for.

$funcCalcSphereVolume = function($arrDimensions) { return pow
($arrDimension['radius'],3) * M_PI * ( 4/3 ); };

function calculate_volume( $arrDimensions, $lmbFunction ) {
return $lmbFunction($arrDimensions);
}

The usage of such functions would be the same, but the coding of the
generic volume function is much less, and very easy to add on to --
just create a new lambda function as needed! So when I need to add a
cylinder volume feature, I only need to take one step now, the
creation of its lambda function:

$funcCalcCylinderVolume = function($arrDimensions) { ... };

Like I said, *usage* of either kind of calculate_volume() functions is
basically the same ( you're specifying the function either way,
whether naming it through a string, or referencing the appropriate
lambda function ) , but *writing* and *adding to* the master function
is easier with lambda functions -- in fact, the adding to is already
taken care of, automagically! How ya like them apples? :)

As I indicated above, this can easily be done without lambda functions.

And BTW - this would be much easier if you use classes :-)

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@xxxxxxxxxxxxx
==================
.



Relevant Pages

  • Jumpstart: syntax error and trying to load from /cdrom media, which disk to use for Solaris 8 image
    ... I'm not sure if I need to use that disk or the Solaris 8 Installation ... Using RPC Bootparams for network configuration information. ... Using finish script: diag_drv.fin ... Executing JumpStart preinstall phase... ...
    (SunManagers)
  • QUESTION: shared libraries
    ... how would I create a package easy-to-install for a new user? ... we could use a script to install the package. ... I have seen some scripts executing from a ... Gouvernement du Canada / Government of Canada ...
    (comp.os.linux.development.apps)
  • QUESTION: shared libraries
    ... how would I create a package easy-to-install for a new user? ... we could use a script to install the package. ... takes root privileges to do so. ... I have seen some scripts executing from a ...
    (comp.os.linux.development.system)
  • Re: What does this script do?
    ... This is the Machine Startup script: ... dim installMediaPath, installMediaFileName ... local drive and executing it. ...
    (microsoft.public.scripting.wsh)
  • QUESTION: shared libraries
    ... how would I create a package easy-to-install for a new user? ... we could use a script to install the package. ... takes root privileges to do so. ... I have seen some scripts executing from a ...
    (alt.os.linux.redhat)