We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › functional programming, currying
Page Index Toggle Pages: 1
functional programming, currying (Read 474 times)
functional programming, currying
Jun 14th, 2008, 6:19pm
 
Hello,

I wonder if there is a way to write in a bit more functional style using processing. Are closures available?
(I've been searching the site for currying, but didn't find any results).

For example, how would I write these javascript-lines in processing (assuming functional programming is possible):

function mf(b){
 var fn = function(a){
   alert(b+" would say "+a);
 };
 return fn;
};

var ae=mf('Albert');
ae('e=mc**2');

Thanks in advance!

Re: functional programming, currying
Reply #1 - Jun 14th, 2008, 6:25pm
 
Java is an OO language, not a functional one. Functions are not first-class object, you can't even pass a reference to them as we do in C...
A common, heavy workaround is to wrap functions in anonymous (or not) classes, which is common for event callback functions, for example.
That's a point of Java I dislike, but hey, that's the way it works.
Re: functional programming, currying
Reply #2 - Jun 14th, 2008, 6:29pm
 
Ah, I see .. thank you!
Re: functional programming, currying
Reply #3 - Jun 14th, 2008, 7:09pm
 
How would the syntax look like, if I wanted to use this workaround (anonymous classes) in the Processsing IDE. Is it possible at all or do I have to fall back to Java?

I've been trying some examples found in the web (http://www.unix.com.ua/orelly/java-ent/jnut/ch03_12.htm), but they didn't really work out in the Processing IDE, using something like  

return new class-name ( [ argument-list ] ) { class-body }

If I used something like this within a Function inside the Processing IDE - what would I specify as the return-type of the function. This attempt didn't work:

Class make_a_class(){
 return new Anonymous () {};
}

Thanks in advance!
Re: functional programming, currying
Reply #4 - Jun 14th, 2008, 7:30pm
 
Okay, I am as far as this:

void setup(){
 Anonymous a = make_a_class();
 a.test();
}

interface Anonymous{void test();};

Anonymous make_a_class() {
 return new Anonymous(){
   void test(){
     print("test");
   }
 };
}

Unfortunately I can't get rid of the interface without processing throwing errors at me. This still doesn't feel very anonymous ;-)
Is there something like a Standard-Anonymous-Class?

Thanks in advance!
Re: functional programming, currying
Reply #5 - Jun 14th, 2008, 7:57pm
 
I think you'd have to do something like:

Code:
public interface Anonymous
{
public void test();
}

void setup()
{
doAnonymous(new Anonymous()
{
public void test()
{
println("Anonymous a");
}
}
);
doAnonymous(new Anonymous(){void test(){background(20);}});
}

void doAnonymous(Anonymous a)
{
a.test();
}
Re: functional programming, currying
Reply #6 - Jun 14th, 2008, 8:44pm
 
Okay, it works (as far as I need it), thanks!
Page Index Toggle Pages: 1