Page 1 of 1

function (object) literal

Posted: Fri Jun 15, 2012 2:54 am
by wvoyance
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?

Re: function (object) literal

Posted: Fri Jun 15, 2012 12:08 pm
by Christopher

Re: function (object) literal

Posted: Fri Jun 15, 2012 8:18 pm
by wvoyance
I have seen that before I asked the question, otherwise I won't know those point I wrote.
But can you answer my question? My question is not addressed in that page.
Thanks.

Re: function (object) literal

Posted: Sat Jun 16, 2012 10:38 am
by Christopher
I see four questions in your post. What specifically is your question?

Re: function (object) literal

Posted: Sat Jun 16, 2012 7:10 pm
by wvoyance
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.

Re: function (object) literal

Posted: Sun Jun 17, 2012 4:27 pm
by Christopher
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.

Re: function (object) literal

Posted: Sun Jun 17, 2012 7:02 pm
by wvoyance
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 do not understand polymorphism, so far. I only heard of its name, never used. Such things appears only recently.
I am old :(
I lived in the age of FORTRAN, COBOL, PASCAL, ALGO, FORTH, C,...etc. 8O

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

Posted: Tue Jun 19, 2012 3:05 pm
by VladSun
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 ;)

Re: function (object) literal

Posted: Tue Jun 19, 2012 4:59 pm
by Christopher
wvoyance wrote:Why cannot a function with a name be passed at a call?
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?

Re: function (object) literal

Posted: Wed Jun 20, 2012 7:34 am
by wvoyance
standard programming language courses textbook told us, variables can be passed by name or by address.

Re: function (object) literal

Posted: Wed Jun 20, 2012 10:43 am
by Christopher
wvoyance wrote:standard programming language courses textbook told us, variables can be passed by name or by address.
No, they can be passed by value or address. You are confusing the fact that variables have both a name and a value.

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.

Re: function (object) literal

Posted: Thu Jun 21, 2012 6:22 am
by Chris Corbyn
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.

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;
}
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:

Code: Select all

echo reduce(array(1, 2, 3), function ($a, $b) { return $a + $b }); // 6
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!

Code: Select all

echo reduce(array("eggs", "bacon", "tomatoes"), function ($a, $b) { return "$a and $b"; }); // eggs and bacon and tomatoes
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.

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

Posted: Sun Jun 24, 2012 10:30 am
by Weirdan
wvoyance wrote: 3. the purpose for using function is to save repeated code. sure.
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).

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);
4. since it will not be used elsewhere, why use function???
what else would suit better as a way to pass a piece of code?

Re: function (object) literal

Posted: Sun Jun 24, 2012 10:36 am
by Weirdan
Chris wrote: // initial memo value is the first element
Classical implementation of reduce accept initial value as an argument. If you did that, you wouldn't have to special case 1 element arrays.