🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: cn.bbs.comp.lang.java
Article #12870

Java�����еĺ���ʽ���(ת��)

#12870
From: צ�ۿ��Ȱ���
Date: Sat, 17 Jul 2004 15:51
119 lines
5677 bytes
�� ��������ת���� WebDevelop ������ ��
�� ԭ���� Groovy ��� ��

���� Effective Java �������г���һ�������ԭ�򣬼���Ҫ��
Java ��ʹ�ú���ʽ��̵ļ��ɣ��Ա��ⲻ��Ϥ���˿�������Ĵ��룬
���Dz��ɷ��ϣ�����ʽ�����һ��ǿ�����ij����ֶΣ��ܹ���ߴ�
���ģ�黯�̶ȣ���˻����Ƽ���ƪ���¡����������IBM DW��

�������ӣ�
http://www-106.ibm.com/developerworks/library/j-fp.html?ca=drs-tp2904

BTW������֤���˱հ�ͺ��ӵȵ�����������Ҳͬʱ������Java�ķ�����



import org.apache.commons.functor.*;
import org.apache.commons.functor.core.composite.*;
import org.apache.commons.functor.adapter.*;
import org.apache.commons.functor.UnaryFunction;
import org.apache.commons.functor.core.Constant;
import org.apache.commons.functor.core.IsEqual;
import org.apache.commons.functor.core.comparator.IsGreaterThanOrEqual;
import org.apache.commons.functor.core.comparator.Min;
import org.apache.commons.functor.core.Identity;

..

UnaryFunction getItemCat = 
   new UnaryFunction()
   {
        public Object evaluate (Object obj)
        {
                return ((SETLItem)obj).getCategory();
        }
   };
         
UnaryFunction getItemPrice = 
   new UnaryFunction()
   {
        public Object evaluate  (Object obj)
        {
                return new Double(((SETLItem)obj).getPrice());
        }
   };
                
Constant catA = new Constant("A");
Constant usd200 = new Constant(new Double(200));
                
BinaryPredicateUnaryPredicate belongsToCatA = 
    new BinaryPredicateUnaryPredicate
      (new UnaryCompositeBinaryPredicate(new IsEqual(), getItemCat, catA));
                
BinaryPredicateUnaryPredicate moreThanUSD200 = 
    new BinaryPredicateUnaryPredicate
       (new UnaryCompositeBinaryPredicate(new IsGreaterThanOrEqual(), 
            getItemPrice, usd200));
                
UnaryOr isEligibleForDiscount = new UnaryOr(new 
    UnaryAnd(belongsToCatA, moreThanUSD100), 
          new UnaryAnd(belongsToCatB, moreThanUSD200));

if (isEligibleForDiscount.test(item1))
    System.out.println("Item #1 is eligible for discount!");
else
    System.out.println("Item #1 is not eligible for discount!");


��֪��������δ���ĺ��壿û�£��������¾�֪���ˣ����߿��Կ���
�ȼ۵�Groovy���԰汾�������ˣ�

isEligibleForDiscount = { it.category == "A" && it.price > 200 }

if (isEligibleForDiscount(item1) {
  println "Item #1 is eligible for discount!"
}
else {
  println "Item #1 is not eligible for discount!"
}




Java�����еĺ���ʽ���
---------------------------------------------------------------------
ʹ�ñհ�͸߽׺�������дģ�黯��Java����

Functional programming in the Java language
---------------------------------------------------------------------
Use closures and higher order functions to write modular Java code

Level: Introductory

Abhijit Belapurkar (abhijit_belapurkar@infosys.com)
Senior Technical Architect, Infosys Technologies Limited
13 Jul 2004

�������´�߶ȵĿ�����Ŀ������϶���Ϥģ�黯������ŵ㡣��
�����á�ģ�黯�Ĵ�������ڱ�д�����ԡ��������á�Java������
�������Ǻ���ʽ��̷�ʽ��������ֻ��ͨ��Haskell��Scheme��Erlang
��Lisp��ר��������ʵ�֡��ڱ����У�����Abhijit Belapurkar����
չʾ�������Java������ʹ�ú���ʽ��̵Ľṹ������հ�͸߽׺�
������д�ṹ���á�ģ�黯�Ĵ��롣

If you work on large-scale development projects, then you're 
familiar with the advantages of writing modular code. Well-
structured, modular code is easier to write, debug, understand, 
and reuse. The problem for Java developers is that the 
functional programming paradigm has long been implemented 
only via specialized languages such as Haskell, Scheme, 
Erlang, and Lisp. In this article, author Abhijit Belapurkar 
shows you how to use functional programming constructs such 
as closures and higher order functions to write well-
structured, modular code in the Java language.

--
groovy - http://groovy.codehaus.org/
�� ��Դ:����ˮ˼Դ bbs.sjtu.edu.cn��[FROM: 221.137.56.87]
--
�� ת��:����ˮ˼Դ bbs.sjtu.edu.cn��[FROM: 221.137.56.87]
Technical Details
Message-ID: <4DGabF$ASJ@bbs.sjtu.edu.cn>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!news3.optonline.net!news.bylinux.net!netnews.sdu.edu.cn!SJTU

Show Full Headers
Complete Article Headers
X-Google-Thread: f6cb0,565752568ad461b5,start
X-Google-Attributes: gidf6cb0,public
X-Google-Language: Chinese,GB
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!news3.optonline.net!news.bylinux.net!netnews.sdu.edu.cn!SJTU
From: Groovy.bbs@bbs.sjtu.edu.cn (צ�ۿ��Ȱ���)
Newsgroups: cn.bbs.comp.lang.java
Subject: Java�����еĺ���ʽ���(ת��)
Date: 17 Jul 2004 15:51:27 GMT
Organization: ��ˮ˼Դ
Message-ID: <4DGabF$ASJ@bbs.sjtu.edu.cn>
Mime-Version: 1.0
Content-Type: text/plain; charset="gb2312"
Content-Transfer-Encoding: 8bit
X-Filename: Java/M.1090079487.A
Lines: 119