support lambda expressions in lambda return statements

Ali Ebrahimi ali.ebrahimi1781 at gmail.com
Wed Jun 8 02:28:50 PDT 2011


Hi Remi & Maurizio,

The following is parts of my intend:


   static interface SAM1<R,A> {
        R apply(A n);
   }

   static interface SAM0<R> {
        R apply();
   }

   static interface SAM2<R,A,B> {
        R apply(A a,B b);
   }

   interface ExceptionHandler<E extends Throwable>{
                void handle(E e);
   }

....

       SAM1<SAM1<Integer,Integer>,Integer> sum = #{x -> #{y -> x + y }};

       int z = sum.apply(0).apply(5);

       SAM1<SAM1<Void,SAM0<Void>>, Integer> afterDelay = #{ x ->
            return #{ f ->
                        try {
                             Thread.sleep(x*1000);
                             f.apply();
                            } catch (Exception e) {}
                     };
       };

       afterDelay.apply(5).apply( #{System.out.println("foo")});

       SAM1<Void,SAM0<Void>> after10Seconds  = afterDelay.apply(10);

       after10Seconds.apply(#{System.out.println("bar")});


        SAM1<SAM1<Void,SAM1<Void,PrintWriter>>,File> withWriter = #{file ->
               return #{ doWithWriter ->
                    try {
                        try(PrintWriter writer = new PrintWriter(file)){
                            doWithWriter.apply(writer);
                            }
                        } catch (Exception e) {}
           };
        };

        File file = new File("log.txt");

        withWriter.apply(file).apply(#{writer ->
            // Printing to the file
            writer.println("foo");
            for (int i=0;i<10;i++) writer.println(i);
        });

        SAM1<Void,SAM1<Void,PrintWriter>> logger = withWriter.apply(file);

        logger.apply(#{writer ->
            writer.println("foo");
        });

        logger.apply(#{writer ->
            for (int i=0;i<10;i++) writer.println(i);
        });


        SAM2<Void,File,SAM1<Void,String>> eachLine = #{file,lineHandler ->
            try {
                    try(BufferedReader reader = new BufferedReader(new
InputStreamReader(new FileInputStream(file)))){
                        String line = null;
                        while((line = reader.readLine()) != null) {
                            lineHandler.apply(line);
                        }
                    }
                } catch (Exception e) {}
        };


       eachLine.apply(file, #{line ->
            System.out.println(line);
            }
       );

        SAM1<SAM1<Void,String>,File> eachLine2 = #{file ->
               return #{ lineHandler ->
                    try {
                        try (BufferedReader reader = new BufferedReader(new
InputStreamReader(new FileInputStream(file)))){
                            String line = null;
                            while((line = reader.readLine()) != null) {
                                lineHandler.apply(line);
                            }
                        }
                    } catch (Exception e) {}
                };
        };


       eachLine2.apply(file).apply(#{line ->
            System.out.println(line);
            }
       );

       SAM1<SAM1<SAM1<Void,String>,File>,ExceptionHandler<IOException>>
eachLine3 = #{exceptionHandler ->
             return #{file ->
               return #{ lineHandler ->
                    try {
                        try (BufferedReader reader = new BufferedReader(new
InputStreamReader(new FileInputStream(file)))){
                            String line = null;
                            while((line = reader.readLine()) != null) {
                                lineHandler.apply(line);
                            }
                        }
                    } catch (IOException e) {
                          exceptionHandler.handle(e);
                    }
                };
        };
        };



       eachLine3.apply(#{IOException e -> /* handle e
*/}).apply(file).apply(#{line ->
            System.out.println(line);
            }
       );

although current syntax is eye cutting.
{(}{P{, P}+}{)}-> expr
{(}{P{, P}+}{)}-> block

Best Regards,
Ali Ebrahimi

On Wed, Jun 8, 2011 at 12:03 PM, Maurizio Cimadamore <
maurizio.cimadamore at oracle.com> wrote:

> On 08/06/11 00:31, Rémi Forax wrote:
> > On 06/06/2011 10:38 AM, Ali Ebrahimi wrote:
> >> Hi all,
> >>
> >> I suggest support for lambda expressions in lambda return statements,
> this
> >> enables us to currying and uncurrying.
> >>
> >> Best Regards,
> >> Ali Ebrahimi
> >>
> > Could you be a little more specific please ?
> >
> > Rémi
> >
> >
> I'm currently working on this:
>
>
>
> interface F<A, B> {
>    B f(A a);
> }
>
> class Test {
>
>     F<String, Integer> m(F<String, F<String, Integer>> f) { return null; }
>
>     void test() {
>          m(#{ s1 -> #{ s2 -> new Integer(1) }});
>     }
> }
>
>
> [Note that the inner lambda appears in the return 'path' of the outer
> lambda]. I believe this is what Ali refers to but I may be wrong. In any
> case, supporting the above code is part of the plan - the only reason
> why the program is currently disallowed is that there are some inference
> glitches I need to take care of before enabling it ;-)
>
> Maurizio
>
>


More information about the lambda-dev mailing list