|
|
||
Alexander Schunk's BlogJanuary 2008 ArchivesJava Closures: Functions or Objects?Posted by alexanderschunk on January 23, 2008 at 11:56 AM | Permalink | Comments (2)Java Closures? What are they?The question wheather closures are functions or objects came to my mind when reading several proposals concerning on closures. It looks like most authors regard them as both - functions and objects but giving not a real and concrete distinction in what case a closure is a function - or functional object - and an object. For example in Neil Gafters vision for closures, closures seem to be both functions or an anonymous function or method and objects - or even classes beecause its possible to extend a class or template from a closure. I think, if we follow the traditional closure syntax and usage - like in lisp or Perl where closures are regarded as procedures or sub routines. I have difficulties to imagĂne a case where it makes sense or its practical to have the ability to extend a template or class from a closure as follows:
class MyClass
extends
{ int x, int y => int }
or any such nomenclature. If a closure is nothing more than an anonymous function why do we need extend them to real object types? Is it just enough to have a short hand for anonymous methods in which the BGGA Syntax would just be enough? I even have diffiuclty to see what a class or object like:
{int x, int y => int }
in the above example would describe or what power closures provide to describe real world entitities. To me closures are to abstract to describe real world entities i would only allow them as a short hand for anonymous functions or methods as used in Lisp or Perl. Also it would make sense to allow closures as parameters for normal methods i.g:
int myClosureMethod(int x, {int y, int z = > int }){
//something
}
but dont apply them on classes or templates because templates are already a very powerful feature to describe real world entities. LinAlg API: Full List of FeaturesPosted by alexanderschunk on January 21, 2008 at 01:24 AM | Permalink | Comments (1)New Feautures: Eigenvalues and triple scalar productThe next release of LinAlg API will provide methods to compute Eigenvalues and the triple scalar product. Eigenvalues and the triple scalar product are common and helpful mathematical features that are useful for solving a lot of common math problems. The Eigenvalues are computed by solving the linear equation det(A-lambda*In) where A is a Matrix, lambda a free parameter and In the identity matrix. We can simplify this by simply determening the det(a) of the Matrix with a parameter and omitting the multplication with the identity matrix since a value multplied by 1 results in the same value. And the multiplication by the identity matrix just means: multiply all diagonal components of the matrix by 1 that is m11, m22, m33 and m44 if you have a 4x4 matrix. The triple scalar product is simply computed by computing the volume of three 3D Vectors by multiplying all components of the three 3D Vectors and perform some + and - operations on them.My View on Closures: Part 2Posted by alexanderschunk on January 20, 2008 at 05:09 AM | Permalink | Comments (4)No more symbols please>In my last Blog i did make some suggestions to get rid of the redundand => syntax of closures. I suggested to use := and to invent a keyword i.g. block or function to highlight the fact that closures are functional objects - basicall Neil Gafter regards them as equivalent to anonymous methods however i think there is no strict distinction in Gafters proposal wheather a closure is a pure function or an object According to Neil Gafter, a closure object needs to be invoked by calling the invoke() method for a particular closure. The invoke method can have parameters i.g.:
{ int x => x + 1 } //closure definition
{ int x = > x + 1 }invoke(10); //call closure with 10 as parameter
So if a closure is a function in this case - besides the fact that its also an object - a closure object - then i think it would make sense to declare this whole block of statemens as a function:
function { int x => x+ 1}(int);
In my last Blog i said that the => symbol is redundant and suggested to use := as a variation of =. With this variation the closure would be written as:
function{
int x := x+ 1;
}(int);
//call closure with 10 as argument
function{ int x:=x+1 }(10);
This syntax could also be used to simply define getters that return values:
function {int x:=10;}(); //always returns 10
int s = function sum{int x, int y := x+y }(3, 5); //call sum with 3 and 5 as parameters
Local variablesAlso its possible to declare local variables in closures:
function{
int j = 0;
int i=3;
switch(i){
//some switch statements
case 1: j++;
case 2: j--;
}
}();
Thus using a function keyword makes clear that a closure is both a function and an object that can be invoked. The invocation is executed by adding the () parantheses to the statement. Yet Another Closures ProposalPosted by alexanderschunk on January 17, 2008 at 09:47 AM | Permalink | Comments (19)First: Getting rid of =>Basically, when i read all the proposals, comments and pros and cons on closures in Java the first thing i would get rid of is the obviously redundant => declaration. So in case of block statements the following would be allowed:
{
int x = 0;
int y = 3;
int sum:= x+ y;
}
As i understood it right, => has at least two meanings. It means return for functions and := or = in the other case. Replace => by := for assignmentsI would use := for assignments rather than => simply because := is more naturial to mathematicians and logicians and is usually used to describe an assignment in math and logics. This could be used as outlined above:
{
int x = 4;
int y = 3;
int z = 3;
int sum:=x+y+z; //assignment
}
Use a function keyword instead of {int, int => int} syntaxFor the function declaration, i would suggest to use a keyword rather than a symbol. Why not invent a function keyword that has a similar meaning like => i.g.:
public int function add(int x, int y){
return x + y;
}
In the other case, i would use := instad of => because the arrow direction implies to read it left to right rather than right to left and there is no <= to represant the invert direction. Use a block keyword to declare block statementsRather than just simply type closures i would invent a block keyword that defines a block of anonymous statements:
//only valid for anonymous block statements, not be used in loops
//itereations
block{
int x = 3;
int y = 4;
}
So with these language features you could write a simple to read Java code that is clear and understandable by everyone:
class MyClass{
//declare a function type
public int function add(int x, int y){
return x + y;
}
public int function sub(int x, int y){
return x - y;
}
public void function(List
LinAlg API CVS trunk is openPosted by alexanderschunk on January 12, 2008 at 02:57 AM | Permalink | Comments (0)LinAlg API CVS trunk is openThere is a first source files upload avaiable for LinAlg API in CVS trunk. To check out the sources simply use your favorite CVS access programm and type the following commands: cvs -d :pserver:username@cvs.dev.java.net:/cvs login :pserver:username@cvs.dev.java.net:/cvs checkout linalg If you are using NetBeans you just need to configure the CVSRoot path and the set the checkout options. There seems to be a problem with NetBeans CVS under Windows but if you have a Linux CVS client that should work well. We will provide further CVS sources for different OS and Java platforms in the future. So far you need to become a member of LinAlg API project and download the sources and .jar files from the document/files section. LinAlg API: 20 fellows make up a CommunityPosted by alexanderschunk on January 09, 2008 at 07:12 AM | Permalink | Comments (0)20 Fellows make up a CommunityThe twentieth member has joined the LinAlg API community. Members background ranges from computer sciences and software engineers with up to 5 years of experience in Java programming and with industry background. In particular in the last two month many people joined LinAlg API and asked for help in developing or porting LinAlg API to other platforms. Thats a great news and i hope and will look forward to get more fellows that want to contribute and help improving LinAlg API. We are on a good way. | ||
|
|