PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Oct 20, 2019 1:01 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 14 posts ] 
Author Message
PostPosted: Fri Jun 15, 2012 2:54 am 
Offline
Forum Contributor
User avatar

Joined: Tue Apr 17, 2012 8:24 pm
Posts: 135
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?


Last edited by wvoyance on Sun Jun 17, 2012 9:31 pm, edited 2 times in total.

Top
 Profile  
 
PostPosted: Fri Jun 15, 2012 12:08 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US
http://en.wikipedia.org/wiki/Anonymous_function

_________________
(#10850)


Top
 Profile  
 
PostPosted: Fri Jun 15, 2012 8:18 pm 
Offline
Forum Contributor
User avatar

Joined: Tue Apr 17, 2012 8:24 pm
Posts: 135


Top
 Profile  
 
PostPosted: Sat Jun 16, 2012 10:38 am 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US
I see four questions in your post. What specifically is your question?

_________________
(#10850)


Top
 Profile  
 
PostPosted: Sat Jun 16, 2012 7:10 pm 
Offline
Forum Contributor
User avatar

Joined: Tue Apr 17, 2012 8:24 pm
Posts: 135


Top
 Profile  
 
PostPosted: Sun Jun 17, 2012 4:27 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US
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)


Top
 Profile  
 
PostPosted: Sun Jun 17, 2012 7:02 pm 
Offline
Forum Contributor
User avatar

Joined: Tue Apr 17, 2012 8:24 pm
Posts: 135


Top
 Profile  
 
PostPosted: Tue Jun 19, 2012 3:05 pm 
Offline
DevNet Master
User avatar

Joined: Wed Jun 27, 2007 9:44 am
Posts: 4313
Location: Sofia, Bulgaria
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 ;)

_________________
There are 10 types of people in this world, those who understand binary and those who don't


Top
 Profile  
 
PostPosted: Tue Jun 19, 2012 4:59 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US

_________________
(#10850)


Top
 Profile  
 
PostPosted: Wed Jun 20, 2012 7:34 am 
Offline
Forum Contributor
User avatar

Joined: Tue Apr 17, 2012 8:24 pm
Posts: 135
standard programming language courses textbook told us, variables can be passed by name or by address.


Top
 Profile  
 
PostPosted: Wed Jun 20, 2012 10:43 am 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US

_________________
(#10850)


Top
 Profile  
 
PostPosted: Thu Jun 21, 2012 6:22 am 
Offline
Breakbeat Nuttzer
User avatar

Joined: Wed Mar 24, 2004 8:57 am
Posts: 13098
Location: Melbourne, Australia
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.

Syntax: [ Download ] [ Hide ]
<?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:

Syntax: [ Download ] [ Hide ]
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!

Syntax: [ Download ] [ Hide ]
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.

Syntax: [ Download ] [ Hide ]
(define (map lst map-fn)
  (cond
    ((null? lst) '())
    (else
      (cons (map-fn (car lst))
            (map (cdr lst) map-fn)))))


Top
 Profile  
 
PostPosted: Sun Jun 24, 2012 10:30 am 
Offline
Moderator
User avatar

Joined: Mon Nov 03, 2003 7:13 pm
Posts: 5978
Location: Odessa, Ukraine


Top
 Profile  
 
PostPosted: Sun Jun 24, 2012 10:36 am 
Offline
Moderator
User avatar

Joined: Mon Nov 03, 2003 7:13 pm
Posts: 5978
Location: Odessa, Ukraine


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group