Local functions
Rémi Forax
forax at univ-mlv.fr
Wed Feb 10 07:05:41 PST 2010
Le 10/02/2010 14:52, Mark Mahieu a écrit :
> On 10 Feb 2010, at 13:09, Rémi Forax wrote:
>
>> There is a simple solution, if you want to write recursive function,
>> create a static method
>> and reference it as a lambda:
>>
>> class A {
>> static int factorial(int x) {
>> return (x<= 1) ? 1 : x*factorial.(x-1);
>> }
>>
>> public static void main(String[] args) {
>> #int(int) lambda = A#factorial;
>> }
>> }
>>
>>
> I don't think that approach is quite so simple if your recursive function needs to use locals in the method which 'creates' it (eg. it references 'args' in 'main', in your example).
>
> Regards,
>
> Mark
>
In that case, the anonymous class syntax is your friend.
Or if lambdas are translated to method handles, you can write:
class A {
static int foo(String[] args, int x) {
return (x<= 1) ? args.length : x*foo.(x-1);
}
public static void main(String[] args) {
#int(int) lambda = A#foo.bindTo(args);
}
}
cheers,
Rémi
More information about the lambda-dev
mailing list