O'Reilly Forums: Function (Object Etc.) Literal - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Function (Object Etc.) Literal

#1 User is offline   librorum 

  • New Member
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 16-June 12

Posted 16 June 2012 - 04:23 PM

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.

Question:
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?
0

#2 User is offline   AndPog7 

  • New Member
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 16-July 12

Posted 16 July 2012 - 02:58 PM

Basically you are referring to an anonymous function; they are generally used as a method to an object. This object is usually declared as variable that can be called and passed as an object. They are passed and called in much the same way as named functions except that they are called by their variable name rather than their function name.
var variableName = function(){
//you notice here the function actually has no name, it is called through the variable name.
//anonymous do something
}
function functionName(){
//here the function would now be called by its function name
//function do something
}
There isn’t much difference in using one over the other, sometimes this is done for object clarity but there are some minor differences. They both return “function” when using the typeof operator, but when you need to know the name of the function (anynomousFunction.name returns an empty string
“ “)

>>> var anynomousFunction = function (){ alert("Hello World"); } typeof anynomousFunction
"function"

>>> var anynomousFunction = function () { alert("Hello World"); } anynomousFunction.name
""

>>> function functionName (){ alert("Hello World"); } typeof alertFunction
"function"


>>> function functionName(){ alert("Hello World"); } functionName.name
"functionName"

When creating objects that is going to create multiple instances that contains multiple pieces of data that you want to return as a single object anonymous functions are more than just useful they then serve a more productive purpose.

function Person(fullName){
this.firstName=firstName;
this.lastName=lastName;
this.fullName=function(){
return this.firstName + " " + this.lastName;
}
}

Look carefully this.fullName is an anonymous function. Either way can be used but I prefer this way when creating objects that will store data for multiple instances.
There is also;
var Person ={
firstName: “SomeName”,
lastName: “TheirLastName”,
fullName: function(){
return this.firstName + “ “ + this.lastName
}
};
but this makes for much more coding when creating new instances an is simply better to use an object literal if you’re only concerned with a single instance. I hope that this is some help to you.

This post has been edited by AndPog7: 16 July 2012 - 03:22 PM

0

#3 User is offline   librorum 

  • New Member
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 16-June 12

Posted 17 July 2012 - 06:20 AM

View PostAndPog7, on 16 July 2012 - 02:58 PM, said:

Basically you are referring to an anonymous function; they are generally used as a method to an object. This object is usually declared as variable that can be called and passed as an object. They are passed and called in much the same way as named functions except that they are called by their variable name rather than their function name.
var variableName = function(){
//you notice here the function actually has no name, it is called through the variable name.
//anonymous do something
}
function functionName(){
//here the function would now be called by its function name
//function do something
}
There isn’t much difference in using one over the other, sometimes this is done for object clarity but there are some minor differences. They both return “function” when using the typeof operator, but when you need to know the name of the function (anynomousFunction.name returns an empty string
“ “)

>>> var anynomousFunction = function (){ alert("Hello World"); } typeof anynomousFunction
"function"

>>> var anynomousFunction = function () { alert("Hello World"); } anynomousFunction.name
""

>>> function functionName (){ alert("Hello World"); } typeof alertFunction
"function"


>>> function functionName(){ alert("Hello World"); } functionName.name
"functionName"

When creating objects that is going to create multiple instances that contains multiple pieces of data that you want to return as a single object anonymous functions are more than just useful they then serve a more productive purpose.

function Person(fullName){
this.firstName=firstName;
this.lastName=lastName;
this.fullName=function(){
return this.firstName + " " + this.lastName;
}
}

Look carefully this.fullName is an anonymous function. Either way can be used but I prefer this way when creating objects that will store data for multiple instances.
There is also;
var Person ={
firstName: “SomeName”,
lastName: “TheirLastName”,
fullName: function(){
return this.firstName + “ “ + this.lastName
}
};
but this makes for much more coding when creating new instances an is simply better to use an object literal if you’re only concerned with a single instance. I hope that this is some help to you.



Thank you for such detail reply.
However, you did not answer some, and actually most of mine,
questions. My questions was:
=========================================================
Question:
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?
==========================================================
0

#4 User is offline   AndPog7 

  • New Member
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 16-July 12

Posted 18 July 2012 - 05:35 AM

View Postlibrorum, on 17 July 2012 - 06:20 AM, said:

Thank you for such detail reply.
However, you did not answer some, and actually most of mine,
questions. My questions was:
=========================================================
Question:
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?
==========================================================

0

#5 User is offline   AndPog7 

  • New Member
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 16-July 12

Posted 18 July 2012 - 05:48 AM

JavaScript was originally created to be a simple load and go language for non-programmers. It was supposed to be easy to implement and easy to understand. With that said Lambda amongst the already confusing implementations within JavaScript would have just added more confusion to the language. For the most part the naming conventions in JavaScript are straight forward.
Function: the action for which a person or thing is specially fitted or used or for which a thing exists.
Method: a procedure or process for attaining an object.
These definitions are much more comprehensible than the use of the word lambda. It is one of the things that they got right early on in JavaScript’s creation. Remember when Brandon Eich was under the gun to get JavaScript out it was during the browser wars where Microsoft was also in a battle to get out a scripting language and Sun Microsystems was trying to win over the web with Java.
Of course this can be explained much deeper, but that’s basically it. It was an early naming convention to describe an operation that novice programmers would understand but not so radical that it syntax would lose its relationship with other languages.
In short on lambda, functions evaluate the same as the mathematical lambda expression. Now with this lambda expression or first class citizens we have (x, y, z) as a set of parameters, a pointer => and then x + y + z which is
(x, y, z) => x + y +z;
as our lambda expression. This is the same as our function;
var someVariable = function (x, y, z){this.x, this.y, this.z};
where our parameters receive the values of x, y, and z. This in our case would become our anonymous function as in the case of our Person example. This would match the lambda definition that “is used to bind variables to values which are then evaluated within an expression”.
Whole books can be written on naming conventions and they can go on and on. JavaScript went for simplicity and understanding. Look up Douglass Crawford on You Tube, he give a very good history on the JavaScript or look up his video “JavaScript: Master Class” he gives a very good historic account of JavaScript, or Brandon Eich, both will give you the frantic history of JavaScript’s creation and thus some of its hurried naming conventions.
I hope this clears things up for you.

This post has been edited by AndPog7: 18 July 2012 - 05:59 AM

0

#6 User is offline   librorum 

  • New Member
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 16-June 12

Posted 18 July 2012 - 06:00 PM

View PostAndPog7, on 18 July 2012 - 05:48 AM, said:

JavaScript was originally created to be a simple load and go language for non-programmers. It was supposed to be easy to implement and easy to understand. With that said Lambda amongst the already confusing implementations within JavaScript would have just added more confusion to the language. For the most part the naming conventions in JavaScript are straight forward.
Function: the action for which a person or thing is specially fitted or used or for which a thing exists.
Method: a procedure or process for attaining an object.
These definitions are much more comprehensible than the use of the word lambda. It is one of the things that they got right early on in JavaScript’s creation. Remember when Brandon Eich was under the gun to get JavaScript out it was during the browser wars where Microsoft was also in a battle to get out a scripting language and Sun Microsystems was trying to win over the web with Java.
Of course this can be explained much deeper, but that’s basically it. It was an early naming convention to describe an operation that novice programmers would understand but not so radical that it syntax would lose its relationship with other languages.
In short on lambda, functions evaluate the same as the mathematical lambda expression. Now with this lambda expression or first class citizens we have (x, y, z) as a set of parameters, a pointer => and then x + y + z which is
(x, y, z) => x + y +z;
as our lambda expression. This is the same as our function;
var someVariable = function (x, y, z){this.x, this.y, this.z};
where our parameters receive the values of x, y, and z. This in our case would become our anonymous function as in the case of our Person example. This would match the lambda definition that “is used to bind variables to values which are then evaluated within an expression”.
Whole books can be written on naming conventions and they can go on and on. JavaScript went for simplicity and understanding. Look up Douglass Crawford on You Tube, he give a very good history on the JavaScript or look up his video “JavaScript: Master Class” he gives a very good historic account of JavaScript, or Brandon Eich, both will give you the frantic history of JavaScript’s creation and thus some of its hurried naming conventions.
I hope this clears things up for you.


Presumably, you over-estimated my knowledge in programming etc. I have experience in programming language, but most before those object oriented languages, i.e. FORTRAN, C, etc.
Let me try to re-capitulate what you said:

1. Those great names are related to the history of javascript creation?
2. Lambda expression is just => ?
This is what I found in google search and for c# only.
But where did mathematician talk about "lambda expression"?
I studied more mathematics than programming languages, but never meet this name. Please advice me where do they come from? In the age of Church there is no such languages like c#.

My knowing "lambda expression" is through WiKi. They said it is related to Church. :(

3. One question you seems did not answered me (perhapse you answered but in some way I cannot perceive.) is
"if this code is used only once (hence no need for a name), why use function?"
0

#7 User is offline   AndPog7 

  • New Member
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 16-July 12

Posted 20 July 2012 - 03:45 AM

1. Those great names are related to the history of javascript creation?
Yes, when referring to names used in JavaScript, the naming conventions were mostly to make JavaScript easy for a novice to understand. The language itself was built to be very simple, to be embedded right into the html page. It did borrow some naming styles from other languages but its ultimate goal was simplicity.
2. Lambda expression is just => ?
Close but it is the full expression (x, y, z) => x + y +z;
“is used to bind variables to values which are then evaluated within an expression” that is the lambda expression. Lambda uses an arrow as one of its operators C# uses “ => “ in its place. The part in parentheses is the variables which point to, or are bound to the arguments that they receive from the right side of the pointer.
3. But where did mathematician talk about "lambda expression"?
Without going into too much detail back in the 1930's a mathematician named Alonzo Church amongst others worked on mathematical formulas they referred to as Lambda Calculus, or Church's Theorem, stating that the recursive functions are the only functions that can be mechanically calculated. The theorem implies that the procedures of arithmetic cannot be used to decide the consistency of statements formulated in accordance with the laws of arithmetic.

In 1956 American social scientist and Nobel laureate Herbert Simon and American physicist and computer scientist Allan Newell at Carnegie Mellon University in Pennsylvania devised a program called Logic Theorist that simulated human thinking on computers. In the course of their work on the Logic Theorist and GPS, Allan Newell, Herbert Simon, and Shaw developed their Information Processing Language (IPL), a computer language tailored for AI programming. At the heart of IPL was a highly flexible data structure that they called a list. In 1960 John McCarthy combined elements of IPL with the lambda calculus to produce the programming language LISP (List Processor). LISP a list-oriented computer programming language was used primarily to manipulate lists of data.
JavaScript’s is often said to have more in common with LISP than to C from where it gets its syntax, but from LISP is where it get its functionality

JavaScript is the first lambda language to go mainstream. Deep down, JavaScript has more in common with LISP than with Java. It is LISP in C’s clothing.
Douglass Crawford: JavaScript: The Good Parts.

So in essence JavaScript got a lot of functional programming logic from LISP while LISP got it from LOGIC and LOGIC got it from Lambda Calculus. The name Lambda or L stood for arithmetic logic. This is similar how we got BOOLEAN LOGIC in JavaScript from Boolean algebra from George Boole.

3. One question you seems did not answered me (perhapse you answered but in some way I cannot perceive.) is
"if this code is used only once (hence no need for a name), why use function?"


The function is only referenced back to through its prototype, thus it is reused through reference but not through a new action. Thus new objects that you create refers back to it for its own data structure and layout.
var Person ={
firstName: “SomeName”,
lastName: “TheirLastName”,
fullName: function(){
return this.firstName + “ “ + this.lastName
}
};
the anonymous function actually is the value of fullName in this example, thus every time that you create the Person object “this.firstName” is concatenated with “this.lastName” and becomes the value of fullName. You will see others write the code differently at times where the Person or custom may have parameters and receive a number of arguments; both ways a perfectly valid. There are times though when you may wish that a function is used only once such as a custom function created to submit payment. Programmers sometime make function non-reusable to avoid individuals from either ordering or making duplicate payments. The bottom line is that it will be referenced back to through its object or variable name but not through its own.

copy this in your text editor for now do not change the bolded function but create duplicates of John and Mary, changing the names. this is a very small data structure. Now imagine if it were a building with data containing, height, width, widows, electrical, material, heating, cooling and pricing data. It could all be stored much easier by creating a prototype (a mini data base if you will)then referenced only by this.buildingInfo

function Person(firstName, lastName){
this.firstName = firstName;
this.lastName = lastName;
this.fullName = function(){
return this.firstName + " " + this.lastName;
}
}


var John=new Person ("John", "Doe");
var Mary = new Person("Mary ", "Jane");

alert(John.fullName ());

This post has been edited by AndPog7: 20 July 2012 - 03:51 AM

0

#8 User is offline   librorum 

  • New Member
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 16-June 12

Posted 20 July 2012 - 06:47 AM

Just two short questions first:
1. So, from today's view, is Lambda calculus correct? I mean Church's theorem saying that mechanical calculable function must be recursive.

The theorem sounds like halting theorem today?

2. Is Functional Literal trying to replace pointers in C?
In C++ pointers disappeared, but functional literal appears.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users