Current State of Closures

Peter Levart peter.levart at marand.si
Tue Jul 27 01:44:10 PDT 2010


I didn't see that mentioned in the drafts. But with current prototype and it's syntax using target typing with inferal of lambda's argument types, the alternative is not so much longer and is more general, since you have the control over argument positions:

public class TestClosures
{
  public static #Integer(Integer) partial(final #Integer(Integer, Integer) func, final int arg1)
  {
    return #(arg2){ func.(arg1, arg2) };
  }

  public static void main(String[] args)
  {
    #Integer(Integer,Integer) pow = #(x, y){x * y};
    #Integer(Integer) part = partial(pow, 2);
    System.out.println(part.(2));
  }
}

Regards, Peter
 
On 07/27/10, Dr Andrew John Hughes wrote:
> Will the following be possible now or in the future as part of Project Lambda?
> 
> public class TestClosures
> {
>   public static #Integer(Integer) partial(#Integer(Integer, Integer)
> func, int arg1)
>   {
>     return func.(Integer.valueOf(arg1));
>   }
> 
>   public static void main(String[] args)
>   {
>     #Integer(Integer,Integer) pow = #(Integer x, Integer y)(x * y);
>     #Integer(Integer) part = partial(pow, 2);
>     System.out.println(part.(2));
>   }
> }
> 
> i.e. being able to supply a partial set of arguments to a method so
> that a new method is returned.
> 
> In Haskell,
> 
> > :t (\x -> \y -> x * y)
> (\x -> \y -> x * y) :: (Num a) => a -> a -> a
> > :t (\x -> \y -> x * y) 2
> (\x -> \y -> x * y) 2 :: (Num a) => a -> a
> 
> Currently this gives:
> 
> TestClosures.java:5: lambda expression cannot be applied to given types
>     return func.(Integer.valueOf(arg1));
>                 ^
>   required: Integer,Integer
>   found: Integer
> 


More information about the lambda-dev mailing list