Monday, December 03, 2007

Will Closures in Java 7 help in making Functional Programming mainstream ?

Of late, there has been a lot of discussions on the usage of advanced programming idioms in the developers' community. Are we getting too much confined within the limits of strongly typed object-oriented paradigms and ignoring the power that dynamic non object oriented languages (read functional languages) have to offer ? Some of the blogs put forward the strawman's argument dismissing the usage of dynamic languages and more powerful abstractions as somewhat elitist and not suitable for the mass programmers. C# has added lots of functional paradigms to the language, Microsoft has positioned F# as a mainstream functional language for the .NET platform. Ruby has lots of functional features, Erlang has started being in the limelight and Haskell still reigns supreme amongst all favorites in reddit postings.

Still the adoption rate is not heartening enough and there are enough indications of apprehensions from the corners who make the masses. Is it the fear of dynamic typing or the lack of tools for refactoring support that's holding us back ? Or will the functional programming support from today's most widely used enterprise language can act as the catalyst towards easier adoption of folds and unfolds ?

I am not much of a believer in the strawman argument. But I think the push has to come from the medium which controls the balance and there is no denying the fact that today's enterprise programmers do Java for a living.

How close are we to getting closures in Java 7 ?

Do I get ..


public static <T,U> U fold(List<T> us,{T,U=>U} fun, U init) {
    U acc = init;
    for(T elem : us) {
        acc = fun.invoke(elem, acc);
    }
    return acc;
}



even though I would like to have ..


public static <T,U> U fold(List<T> us,{T,U=>U} fun, U init) {
    if (us.size() == 0) {
        return init;
    }
    return fold(us.subList(1, us.size()),
                fun, fun.invoke(us.get(0),
                init));
}



Either way I should be able to write ..


List<Integer> ints = new ArrayList<Integer>();
//.. populate ints
fold(ints,
      {Integer elem,Integer acc=>elem + acc},
    0);



With Neal Gafter's prototype, I can already write this, though we need TCO in order to play around with meaningful functional control structures.

10 comments:

Ricky Clarkson said...

Gah. Haskell is not dynamically typed at all. In fact, it has complete type erasure - no types exist at runtime.

Dynamic typing is not equivalent to functional programming whatsoever - in fact most of the innovation in functional programming comes from statically-typed languages like Haskell, Clean and Scala, these days.

Unknown said...

@Ricky: I did not tell that Haskell is dynamically typed. What I meant was that the fear for dynamic typing and lack of tools have also led to lesser adoption of functional paradigms in the programming world. Two of the most popular languages to support functional programming today - Ruby and Erlang are dynamically typed. Hence if we get static typing + functional paradigms from Java 7, then it can act as a catalyst for increased adoption of functional paradigms amongst today's enterprise programmers. A developer using Java today can more easily define a fold in Java 7, since he is familiar with the language and can relate that it is not that difficult to pick up a new technique of functional abstraction. Then tomorrow he will also try out Haskell since his initial fears towards functional programming have been allayed through Java.

Neal Gafter said...

I think there is a reasonable chance that jsr292 will provide support for TCO in JDK7.

Jon Harrop said...

Perhaps surprisingly, OCaml is already as popular as Haskell and Erlang on the Linux OS, according to the Debian and Ubuntu package popularity contests. OCaml and Erlang are also the fastest growing functional programming languages.

Ricky Clarkson said...

Presumably OCaml is needed by some programs then, because you're the only user I've noticed.

Many people refer to it as something they used to do, though often fondly.

By what measure is OCaml the fastest growing functional language?

Are you returning to Scala soon? I enjoyed parts of your posts to the Scala mailing list.

Jon Harrop said...

Hi Ricky,

The Debian and Ubuntu package popularity contests track the number of installs of each package among the subscribers to the contest.

Here are the current number of installed development environments for the most popular functional programming languages:

OCaml: 5757
Erlang: 5626
Haskell: 5601
Common Lisp: 4680
Scheme: 3651
Scala: 189

As you can see, OCaml is among the most popular, with many thousands of registered installs (and many more will not be registered). I already knew this, of course, because we have hundreds of paying customers using OCaml extensively in their work, ranging from chip design at Intel to shoe making in Italy.

I recently measured the increase in popularity over the past month:

OCaml: 551
Erlang: 548
Haskell: 498
Common Lisp: 360
Scheme: 334
Scala: 18

The error is quite big because these values fluctuate but you can see that all of the functional languages are basically growing at the same proportional rate of ~10% growth per month, i.e. there is no evidence for a current favorite language that is exploding in popularity.

Scala is not yet anything like as popular as OCaml. As a company we have discovered that we can turn a profit from product lines built around languages that are at least as popular as OCaml, which means we could potentially earn money from Haskell and Erlang (and F#) but not yet from Scala.

Although Scala is an interesting language, as a pioneer of functional programming on the JVM, I feel that its design has not learned sufficiently from the ML family of languages (e.g. as F# did). The most glaring omission is the lack of automatic generalization in Scala, which means you must define lots of types unnecessarily. This is one of the main sources of the brevity of the ML family of languages and it is sorely missed, essentially rendering Scala much less suitable for the kind of interactive technical computing that we specialize in.

I believe that we need a new functional programming language that draws upon the relative merits of Haskell, OCaml and F#. I am working towards the goal of creating this language using LLVM as a backend. My preliminary findings are amazing: LLVM makes it easy to achieve and even exceed the performance of OCaml's current implementation (which is famous for being the fastest functional programming language there is) in a variety of important test cases.

The main problem is that we do not yet have a GC and run-time, not even a working example. However, I believe it will be easy to obtain performance between that of Scala/F# and OCaml for GC-intensive code simply because the JVM and .NET are optimized for Java/C# which makes them ~5x slower for functional programming.

Unknown said...

群馬 ハウスメーカー
埼玉 ハウスメーカー
盲導犬
治験
出産祝い
クレジットカード決済
24そんぽ24
自動車保険
自動車保険 比較
アメリカンホームダイレクト
不動産
出会い
出会い系
出会い系サイト
出会いサイト
チューリッヒ
自動車 保険 見積
ソニー損保
カード決済

Unknown said...

アクサダイレクト
インプラント
ショッピングカート
東京 ホームページ制作
不動産投資
知多半島 温泉
知多半島 旅館
埼玉 不動産
三井ダイレクト
カラーコンタクト
カーボンオフセット
コンタクトレンズ
クレジットカード 海外旅行保険
ゼネラリ
広島 不動産
お見合いパーティー
募金
松山市 不動産

Unknown said...

国際協力ユーザーの皆さまが大手ECサイトで商品をご購入される際、グリーンクリックを経由していただくだけでECサイトから支国際協力。グリーンクリックを利用することで、商品の購入価 格が変わったり、寄

Anonymous said...

情趣用品,情趣用品,情趣用品,情趣用品,情趣,情趣,情趣,情趣,按摩棒,震動按摩棒,微調按摩棒,情趣按摩棒,逼真按摩棒,G點,跳蛋,跳蛋,跳蛋,性感內衣,飛機杯,充氣娃娃,情趣娃娃,角色扮演,性感睡衣,SM,潤滑液,威而柔,香水,精油,芳香精油,自慰套,自慰,性感吊帶襪,吊帶襪,情趣用品加盟AIO交友愛情館,情人歡愉用品,美女視訊,情色交友,視訊交友,辣妹視訊,美女交友,嘟嘟成人網,成人網站,A片,A片下載,免費A片,免費A片下載愛情公寓,情色,舊情人,情色貼圖,情色文學,情色交友,色情聊天室,色情小說,一葉情貼圖片區,情色小說,色情,色情遊戲,情色視訊,情色電影,aio交友愛情館,色情a片,一夜情,辣妹視訊,視訊聊天室,免費視訊聊天,免費視訊,視訊,視訊美女,美女視訊,視訊交友,視訊聊天,免費視訊聊天室,情人視訊網,影音視訊聊天室,視訊交友90739,成人影片,成人交友,美女交友,微風成人,嘟嘟成人網,成人貼圖,成人電影,A片,豆豆聊天室,聊天室,UT聊天室,尋夢園聊天室,男同志聊天室,UT男同志聊天室,聊天室尋夢園,080聊天室,080苗栗人聊天室,6K聊天室,女同志聊天室,小高聊天室,上班族聊天室,080中部人聊天室,同志聊天室,聊天室交友,中部人聊天室,成人聊天室,一夜情聊天室,情色聊天室,寄情築園小遊戲情境坊歡愉用品,情境坊歡愉用品,情趣用品,成人網站,情人節禮物,情人節,AIO交友愛情館,情色,情色貼圖,情色文學,情色交友,色情聊天室,色情小說,七夕情人節,色情,情色電影,色情網站,辣妹視訊,視訊聊天室,情色視訊,免費視訊聊天,美女視訊,視訊美女,美女交友,美女,情色交友,成人交友,自拍,本土自拍,情人視訊網,視訊交友90739,生日禮物,情色論壇,正妹牆,免費A片下載,AV女優,成人影片,色情A片,成人論壇,情趣,免費成人影片,成人電影,成人影城,愛情公寓,成人影片,保險套,舊情人,微風成人,成人,成人遊戲,成人光碟,色情遊戲,跳蛋,按摩棒,一夜情,男同志聊天室,肛交,口交,性交,援交,免費視訊交友,視訊交友,一葉情貼圖片區,性愛,視訊,視訊聊天,A片,A片下載,免費A片,嘟嘟成人網,寄情築園小遊戲,女同志聊天室,免費視訊聊天室,一夜情聊天室,聊天室