posts - 56, comments - 77, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

           

          〇,簡(jiǎn)介

          Gavator(Generic Java Functor)試圖探索以普通Java語法進(jìn)行函數(shù)式編程的可行性,并盡可能的保證使用的方便性,良好的可讀性和基本的類型安全性

          一,功能

          所有功能都是類型安全的,無需涉及強(qiáng)制轉(zhuǎn)型

          • 提供了若干算法:transform/map, select/filter, accumulate/reduce, foreach/enumerate, findfirst等

          • 提供了若干適配器:bind,bind1st, bind2nd等

          • 提供了常用控制結(jié)構(gòu)的函數(shù)形式和functor形式:dowhile/whiledo, dountil/untildo, ifelse等

          • 提供了常用邏輯運(yùn)算結(jié)構(gòu)的函數(shù)形式和functor形式:and,or,not等

          • 提供了常用數(shù)據(jù)結(jié)構(gòu):filter,pipe等

          二,結(jié)構(gòu)

          Gavator的結(jié)構(gòu)是層次化的

          • 最核心的是一組接口:分別定義了Procedure,Predicate,F(xiàn)unction的零元,一元,二元形式

          • 基于這些接口,以functor的形式提供了常用概念的實(shí)現(xiàn),包括數(shù)據(jù)結(jié)構(gòu),控制結(jié)構(gòu),適配器等,如Filter/Pipe,Bind,And/Or/Not,WhileDo/IfElse等

          • 基于這些結(jié)構(gòu)和適配器,以function的形式實(shí)現(xiàn)了使用方便和可讀性好的包裝,如pipe,bind,and/or/not, ifelse等

          三,用法

          附帶的測(cè)試用例可用來簡(jiǎn)單說明一下用法;其實(shí)所有functor和algorithm都很直觀,看到名稱和參數(shù),即能領(lǐng)會(huì)它的用法;并且代碼量非常少,半個(gè)小時(shí)就能將所有代碼瀏覽一遍

          曾經(jīng)聽說ThoughtWorks的敏捷項(xiàng)目中平均每個(gè)函數(shù)代碼控制在三行以下,Gavator中每個(gè)函數(shù)平均只有不到兩行代碼

          package gavator.test; 

          import static gavator.algorithm.Algorithms.*;

          import gavator.datastructure.*;

          import junit.framework.*;

           

          class ToUpperFilter implements Filter<String> {

               public String eval(String obj) {

                   return obj.toUpperCase();

               }

          class ToLowerFilter implements Filter<String> {

               public String eval(String obj) {

                   return obj.toLowerCase();

               }

          class DirtyWordsFilter implements Filter<String> {

               public String eval(String obj) {

                   return obj.replace("dirty words", "***");

               }

          }

           

          public class FilterPipeTest extends TestCase {

           

               private Pipe lowerPipe, upperPipe, dirtyWordsPipe; 

               private String string = "abc"; 

               private String dws = "you said dirty words, so...";

           

               public void testFilter() {

                   Assert.assertEquals("ABC", new ToUpperFilter().eval(string));

                   Assert.assertEquals("abc", new ToLowerFilter().eval(new ToUpperFilter().eval(string)));

                   Assert.assertEquals("you said ***, so...", new DirtyWordsFilter().eval(dws));

               } 

               public void testPipe() {

                   lowerPipe = new Pipe<String>(new ToUpperFilter(), new ToLowerFilter());

                   upperPipe = new Pipe<String>(new ToLowerFilter(), new ToUpperFilter());

                   dirtyWordsPipe = new Pipe<String>(lowerPipe, new DirtyWordsFilter());

           

                   Assert.assertEquals("abc", lowerPipe.eval("abc"));

                   Assert.assertEquals("ABC", upperPipe.eval("abc"));

           

                   Assert.assertEquals("hehe***", dirtyWordsPipe.eval("hehedirty words"));

                   Assert.assertEquals("hehe***", pipe("hehedirty words", dirtyWordsPipe));

               } 

          }

          四,擴(kuò)展

          使用者可以在Gavator結(jié)構(gòu)的任何一次層次對(duì)Gavator進(jìn)行擴(kuò)充,如擴(kuò)充functor實(shí)現(xiàn)其它設(shè)計(jì)模式和常用概念,或編寫更多的算法,結(jié)合import static,提高客戶代碼的可讀性

          目前Gavator to be contributed的是 ContinuationFramework 和 QueryFramework

          五,問題

          • Gavator的主要思想來自C++ STL和Apache Common Funtor,我對(duì)純粹的函數(shù)式語言如Scheme,Haskell并沒有太多經(jīng)驗(yàn),因此Gavator最主要的問題是:它做的是否正確?

          • Gavator還不完整,如尚未將Functor實(shí)現(xiàn)為值語義等

          • 好的名稱有助于減少文檔,促進(jìn)理解,但同一個(gè)概念在不同的社團(tuán)可能有不同的慣用名稱,如transform和map,select和filter,accumulate和reduce,等等;Gavator選擇了使用STL和Apache Common Functor中的名稱

          • 序列操作的參數(shù)應(yīng)該是Collection還是Iterator ?STL和Apache Common Functor選擇了Iterator,我感覺大部分情況下Collection更方便,就用了Collection

          • 目前Gavator其實(shí)并不是很實(shí)用,像ifelse等基本是花拳繡腿,關(guān)鍵是要基于Gavator,發(fā)展出專注于某個(gè)應(yīng)用領(lǐng)域的framework,如Continuation和Query

          六、其它

          Java語言本身的簡(jiǎn)單,造成了對(duì)Functional Programming支持的限制,Apache Common Functor至今還是sandbox;這些限制包括:

          • 函數(shù)不是first class,不是一等公民,更別提lambda表達(dá)式

          • primitive type與class無法完全統(tǒng)一,你試試能否寫一個(gè)通用的accumulate支持int集合,long集合,double集合的累加就知道了

          • 不支持操作符重載,當(dāng)然,這不是本質(zhì)問題

          • 使用擦拭法實(shí)現(xiàn)generic,使你不能對(duì)T調(diào)用任意函數(shù),無法特化,沒有typedef,只能用繼承來模擬,任何動(dòng)態(tài)特性都將使generic喪失類型安全性,如序列化后反序列化,反射等

          • 甚至不能return void,這使Procedure和Function在實(shí)現(xiàn)上無法統(tǒng)一以減少代碼,雖然在概念上它們是獨(dú)立的

          或許Java平臺(tái)上的函數(shù)式編程的支持將使用專門的語言來解決

          Gavator起源于去年的一個(gè)項(xiàng)目,在那個(gè)項(xiàng)目里實(shí)現(xiàn)了一個(gè)基于Functor的查詢框架,增加一種條件只需實(shí)現(xiàn)Condition接口,當(dāng)然,Condition是一個(gè)Predicate,然后就可以和已有的各種Condition通過and,or,not等組合出各種復(fù)合查詢,提供服務(wù)的一方通過恒定不變的代碼select(Collection, Condition)來滿足任意的查詢;希望接下來實(shí)現(xiàn)一個(gè)封裝了各種查詢(對(duì)象,SQL,XPath/XQuery等)的QueryFramework

          另外對(duì)Continuation的支持也值得一做,希望得到幫助,mailto:ajaxchelsea@163.com

           


          評(píng)論

          # re: Generic Java Functor 發(fā)布首個(gè)版本  回復(fù)  更多評(píng)論   

          2005-09-07 11:33 by 江南白衣@ITO
          切爾斯基也搬過來啦~~~ 第一個(gè)過來串門:)

          # re: Generic Java Functor 發(fā)布首個(gè)版本  回復(fù)  更多評(píng)論   

          2005-09-07 15:46 by chelsea
          呵呵,從出租屋搬到經(jīng)濟(jì)適用房了;往來有白衣啊

          # re: Generic Java Functor 發(fā)布首個(gè)版本  回復(fù)  更多評(píng)論   

          2005-12-21 04:16 by ajooo
          我做過一個(gè)依照haskell MonadCont模型的continuation,如果計(jì)算模型嚴(yán)格遵照Monad,就可以用。


          不過,java 5的generics功能太弱,搞搞stl可能還勉強(qiáng)可以,搞Monad就沒戲了,只能cast!

          # re: Generic Java Functor 發(fā)布首個(gè)版本  回復(fù)  更多評(píng)論   

          2005-12-21 09:34 by 切爾斯基
          看來java和fp天生阻抗不匹配啊

          # re: Generic Java Functor 發(fā)布首個(gè)版本  回復(fù)  更多評(píng)論   

          2005-12-22 02:20 by ajooo
          exception本來就已經(jīng)是escape-only的continuation了。而這種continuation也是最沒爭(zhēng)議的一種.所以用exception來實(shí)現(xiàn)continuation比較簡(jiǎn)單實(shí)用.代價(jià)就是效率有點(diǎn)那個(gè).


          另外一種,象ruby和c#的return yield這種也可以說是continuation的應(yīng)用的東西,可以用多thread的wait(),notify()來模擬,效率差,但是看上去差不多。

          至于一般意義上的fp,不太可行。類型安全是一方面,高階函數(shù)的可讀性還有可調(diào)試性都很差。玩玩可以,我的一個(gè)玩具:http://forum.javaeye.com/viewtopic.php?t=7951&start=0
          實(shí)際真用還是不成熟。


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 四平市| 正蓝旗| 佳木斯市| 华阴市| 肃北| 丘北县| 穆棱市| 广南县| 龙胜| 延吉市| 溆浦县| 远安县| 鹤壁市| 丰顺县| 榕江县| 山东省| 南澳县| 宣恩县| 调兵山市| 扎鲁特旗| 铜川市| 双流县| 通榆县| 阳东县| 兴安盟| 枝江市| 斗六市| 连城县| 库尔勒市| 宿州市| 普定县| 石河子市| 虹口区| 南城县| 原阳县| 溆浦县| 邹平县| 绵阳市| 商城县| 福州市| 黑山县|