support lambda expressions in lambda return statements

Ali Ebrahimi ali.ebrahimi1781 at gmail.com
Wed Jun 8 05:23:34 PDT 2011


Hi Maurizio,
yes, this is my bad.
The corrected version is:

SAM1<SAM1<Void,SAM1<Void,String>>,File> eachLine2=...

Also I corrected this line:
SAM1<SAM1<SAM1<Void,SAM1<Void,String>>,File>,ExceptionHandler<IOException>>
eachLine3=...

Best Regards,
Ali Ebrahimi
On Wed, Jun 8, 2011 at 2:52 PM, Maurizio Cimadamore <
maurizio.cimadamore at oracle.com> wrote:

> Hi Ali,
> in your example I noticed various instance of the following pattern:
>
>
> static interface SAM1<R,A> {
>   R apply(A n);
> }
>
> void test() {
>
>   SAM1<SAM1<Void,String>,File> eachLine2 = #{file ->
>      return #{ lineHandler -> lineHandler.apply(""); };
>   };
> }
>
> This seems to be wrong, right? I.e. method apply should not be available on
> 'lineHandler', given that 'lineHandler' would be inferred to be String from
> the corresponding SAM type (SAM1<Void, String>).
>
> Maurizio
>
>
> On 08/06/11 10:28, Ali Ebrahimi wrote:
>
>> 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);
>>            }
>>       );
>>
>
>


More information about the lambda-dev mailing list