Top Computers Blogs

 

Categories

 

  • Blogroll

  •  

    Closures in Java?

    19 Aug 06

    TomTom ONE XL-S - a widescreen GPS

    Just a short notice, found it while browsing the digg for programming stories.
    There is a proposition to include closures in Java (probably 7.0), here is a pdf file with the proposition.

    I am quite new to this functional programming concept, but to give you some idea what is it (example taken from the pdf file):

    1. public static void main(String[] args) {
    2.  
    3.     // here is the definition of function type
    4.     int plus2(int x) { return x+2; }
    5.  
    6.     // here we have an alias for the same function
    7.     int(int) plus2b = plus2;
    8.     System.out.println(plus2b(2));
    9. }

    Maybe it doesn't look so great (aliases for functions, phi), but AFAIR there should be also a way to do this:

    1. public static void main(String[] args) {
    2.     int power(int x, int y) { return Math.pow(y,x); }
    3.  
    4.     // definition of x^2
    5.     int(int) power2 = power(2);
    6.  
    7.     // should print 10^2  = 100
    8.     System.out.println(power2(10));
    9. }

    But I could not find it in the pdf file, so maybe there won't be such possibility.
    I'll try to introduce more "functional" concepts in some of the future posts, so stay tuned.

    Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
    • DZone
    • del.icio.us
    • Digg
    • Technorati

    One Response to “Closures in Java?”  

    1. 1 Python versus Java &middot Programming tips for newbies - CodeRookie

    Leave a Reply