function (object) literal
Moderator: General Moderators
function (object) literal
I know
1. functional literal is a function with no name, easy.
2. because it will not be used else where therefore, no need for a name. simple.
3. the purpose for using function is to save repeated code. sure.
4. since it will not be used elsewhere, why use function???
Why such a great name? I was also told such function is first discussed by Church in some lambda algebra?
What are their relation?
1. functional literal is a function with no name, easy.
2. because it will not be used else where therefore, no need for a name. simple.
3. the purpose for using function is to save repeated code. sure.
4. since it will not be used elsewhere, why use function???
Why such a great name? I was also told such function is first discussed by Church in some lambda algebra?
What are their relation?
Last edited by wvoyance on Sun Jun 17, 2012 9:31 pm, edited 2 times in total.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: function (object) literal
I have seen that before I asked the question, otherwise I won't know those point I wrote.Christopher wrote:http://en.wikipedia.org/wiki/Anonymous_function
But can you answer my question? My question is not addressed in that page.
Thanks.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: function (object) literal
I see four questions in your post. What specifically is your question?
(#10850)
Re: function (object) literal
Christopher wrote:I see four questions in your post. What specifically is your question?
There are 4 points, but 1,2,3 are facts we have known.
So, the question is 4 and the sentences follows.
4 appears to contradict with 1,2,3 that I have known.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: function (object) literal
The reason is the same as the use of polymorphism with objects. Because the anonymous function is passed into the code, the code using the function does not need to hard code the name of the function. That means that the outside code can pass in any function with an acceptable parameter list and it can be called. Polymorphism is key strategy in modern software design.
(#10850)
Re: function (object) literal
I do not understand polymorphism, so far. I only heard of its name, never used. Such things appears only recently.Christopher wrote:The reason is the same as the use of polymorphism with objects. Because the anonymous function is passed into the code, the code using the function does not need to hard code the name of the function. That means that the outside code can pass in any function with an acceptable parameter list and it can be called. Polymorphism is key strategy in modern software design.
I am old
I lived in the age of FORTRAN, COBOL, PASCAL, ALGO, FORTH, C,...etc.
Let me try to recapitulate what you said: Are you saying function literal can act just like a variable so that we can replace
it with another function at the call?
Why cannot a function with a name be passed at a call? If compiler do not like a name, it can just ignore it.
What is needed is only a starting point of a call. Isn't a function with no name the same as a function with fixed name?
'No-name' is unique.
So, it is only a strategy of programming language design? But how could it related to Church's lambda calculus?
Which language is the first one to introduce function literal?
If I say, the purpose to introduce function literal is to eliminate pointer (as in C), is that correct?
Re: function (object) literal
You know C ... so you know what a pointer to function means and how it's used. It's very close to anonymous functions - you declare/define the function inline.
PS: BTW, take a look at JavaScript Function object
PS: BTW, take a look at JavaScript Function object
There are 10 types of people in this world, those who understand binary and those who don't
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: function (object) literal
No variables are passed with a name. Only the value and type are passed. That is the definition of a passed variable -- it has its name defined locally. That is the whole point of passing things into functions. How and why would a function variable be any different?wvoyance wrote:Why cannot a function with a name be passed at a call?
(#10850)
Re: function (object) literal
standard programming language courses textbook told us, variables can be passed by name or by address.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: function (object) literal
No, they can be passed by value or address. You are confusing the fact that variables have both a name and a value.wvoyance wrote:standard programming language courses textbook told us, variables can be passed by name or by address.
Show me a language that preserves the variable name of a variable passed into a function? The whole point of passing a variable into a function is that it is accessed by the local name within the scope of the function -- regardless of the external name of the variable passed.
(#10850)
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Re: function (object) literal
Anonymous functions are useful when you want to pass them around as arguments to other functions (higher order functions). To PHP they are relatively new, even if they should probably have existed long before. Let's implement something like reduce in PHP.
Reduce calls a function for each element in an array, passing it two arguments each time it is invoked. The first argument is a memo variable, that is the result of each invocation of the anonymous function thus far. The second argument is the value of the current element in the array. The overall output is a single value computed by performing the same operation across all adjacent elements in the array. This will make more sense in code.
We'll pretend that PHP's array_reduce() function doesn't already exist
$fn in the reduce() function is an anonymous function; reduce() cannot work without it, since it provides half of its implementation.
This function cannot work without the help of some other function, because it makes no assumptions about how to actually reduce the array. Lets go with a simple example that just adds all the numbers in an array together:
The function $fn was first invoked with 1 and 2, so it returned 3. Then it was invoked with the same 3 and the 3 that is left in the array, so it returned 6. Because no data was left in the array, 6 was returned from reduce().
How about using reduce to implement something like PHP's implode() function? We can do that, because we specify the behaviour via our anonymous function!
These types of higher order functions are commonplace in functional programming languages like Lisp, Scheme and Haskell, which are based entirely around lambda calculus and thus depend heavily on functions to implement algorithms as basic as iteration data structures.
If the concept interests you, here's an implementation of map in Scheme, which applies the function map-fn to each expression in the list lst. Scheme is entirely driven by lists and functions. The variable name map-fn here is just a name. The actual function can either be anonymous (lambda), or a defined function with another name. The name is unimportant, since it is just a reference to the function, so it can be applied.
Reduce calls a function for each element in an array, passing it two arguments each time it is invoked. The first argument is a memo variable, that is the result of each invocation of the anonymous function thus far. The second argument is the value of the current element in the array. The overall output is a single value computed by performing the same operation across all adjacent elements in the array. This will make more sense in code.
We'll pretend that PHP's array_reduce() function doesn't already exist
Code: Select all
<?php
function reduce($array, $fn) {
if (count($array) < 2) {
return current($array); // special case
}
$memo = array_shift($array); // initial memo value is the first element
foreach ($array as $value) {
$memo = $fn($memo, $value); // memo is updated according to $fn for each element
}
return $memo;
}Code: Select all
echo reduce(array(1, 2, 3), function ($a, $b) { return $a + $b }); // 6How about using reduce to implement something like PHP's implode() function? We can do that, because we specify the behaviour via our anonymous function!
Code: Select all
echo reduce(array("eggs", "bacon", "tomatoes"), function ($a, $b) { return "$a and $b"; }); // eggs and bacon and tomatoesIf the concept interests you, here's an implementation of map in Scheme, which applies the function map-fn to each expression in the list lst. Scheme is entirely driven by lists and functions. The variable name map-fn here is just a name. The actual function can either be anonymous (lambda), or a defined function with another name. The name is unimportant, since it is just a reference to the function, so it can be applied.
Code: Select all
(define (map lst map-fn)
(cond
((null? lst) '())
(else
(cons (map-fn (car lst))
(map (cdr lst) map-fn)))))Re: function (object) literal
Not in this case. Anonymous functions are often used when you need a callable structure you won't be using anywhere else, such as callback passed to usort(). The whole point of using a separate callable structure provided by a calling code is to allow to express abstract algorithms (like sorting) without specifying implementation details (like the routine used to compare values).wvoyance wrote: 3. the purpose for using function is to save repeated code. sure.
This does allow to save on repeated code in the end because you don't have to implement the same abstract algorithm over and again. Anonymous function serves as a way to incapsulate non-reusable code so that reusable code can work with it, and they allow to do that without polluting your name space with the names you won't be using anywhere else.
consider the following function to sort points on a plane by their distance from the given point:
Code: Select all
function sqrdistance($x1, $y1, $x2, $y2) {
return pow($x1 - $x2, 2) + pow($y1 - $y2, 2);
}
$points = $plane->getPoints();
$reference = [2, 4]; // x, y
usort($points, function($a, $b) use ($reference) {
return sqrdistance($a[0], $a[1], $reference[0], $reference[1])
- sqrdistance($b[0], $b[1], $reference[0], $reference[1]);
});
$closest = reset($points);
what else would suit better as a way to pass a piece of code?4. since it will not be used elsewhere, why use function???
Re: function (object) literal
Classical implementation of reduce accept initial value as an argument. If you did that, you wouldn't have to special case 1 element arrays.Chris wrote: // initial memo value is the first element