论坛首页 Java企业应用论坛

在java中小试FP(一)

浏览 2056 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-02-29  
最近看了点functional programming的东西,觉得可以借鉴的地方还不少,所以做了点尝试

就是想把java这样写


User user=new User("gordon",28,170);
if(user.getHeight()>160&&user.getName().equals("gordon"){
  do something...
}else{
  do something else...
}


User user=new User("gordon",28,170);
Functor process=taller.than(160).and(matchName.to("gordon")).case(true,someAction,false,otherAction);
process.apply(user);

在java中的fp尝试,可以用已有的程序创造新的程序
package com.gordon;

import java.util.HashMap;
import java.util.Map;

public class Functor {
     public Object apply(Object... args){
          return null;
     }
     
     public Functor and(final Functor... functors){
          final Functor first=this;
          return new Functor(){
               public Object apply(Object... args){
                    Boolean result=(Boolean)first.apply(args);
                    for(Functor f : functors){
                         result=result&&(Boolean)f.apply(args);
                    }
                    return result;
               }
          };
     }
     
     public Functor or(final Functor... functors){
          final Functor first=this;
          return new Functor(){
               public Object apply(Object... args){
                    Boolean result=(Boolean)first.apply(args);
                    for(Functor f:functors){
                         result=result||(Boolean)f.apply(args);
                    }
                    return result;
               }
          };
     }
     
     public Functor sw(final Map<Object,Functor> switchMap){
          final Functor f=this;
          return new Functor(){
               public Object apply(Object... args){
                    Object firstResult=f.apply(args);
                    if(!switchMap.containsKey(firstResult))return args;
                    return switchMap.get(firstResult).apply(args);
               }
          };
     }
     
     public Functor sw(Object... args){
          if(args.length>1&&args.length%2==0){
               Map<Object,Functor> sw=new HashMap<Object,Functor>();
               for(int i=0;i<args.length;i=i+2){
                    sw.put(args[i], (Functor)args[i+1]);
               }
               return this.sw(sw);
          }else throw new java.lang.IllegalArgumentException("incorrect number of arguments for Functor.sw , correct format should be (case,Functor,case,Functor...)");
     }
     
     public Functor curry(final Object... pargs){
          final Functor self=this;
          return new Functor(){
               public Object apply(Object... args){
                    Object[] arg=new Object[pargs.length+args.length];
                    System.arraycopy(pargs, 0, arg, 0, pargs.length);
                    System.arraycopy(args, 0, arg,pargs.length ,args.length);
                    return self.apply(arg);
               }
          };
     }
     
     public Functor then(final Functor...functors){
          final Functor first=this;
          return new Functor(){
               public Object apply(Object... args){
                    Object result=first.apply(args);
                    for(Functor f:functors){
                         result=f.apply(result);
                    }
                    return result;
               }
          };
     }
     
     public Functor to(final Object... args){
          return this.curry(args);
     }

     public Functor than(final Object... args){
          return this.curry(args);
     }

     public static void main(String args[]) throws Exception{
          Functor t=new Functor(){
               public Object apply(Object... args){
                    return true;
               }
          };
          Functor f=new Functor(){
               public Object apply(Object... args){
                    return false;
               }
          };
          Functor trueAction=new Functor(){
               public Object apply(Object... args){
                    System.out.println(""+true+" "+args.length);
                    return null;
               }
          };
          Functor falseAction=new Functor(){
               public Object apply(Object... args){
                    System.out.println(""+false+" "+args.length);
                    return null;
               }
          };
          Functor eq=new Functor(){
               public Object apply(Object... args){
                    return args[0].equals(args[1]);
               }
          };
          
          Functor matchUserName=new Functor(){
               public Object apply(Object... args){
                    return args[0].equals(((User)args[1]).getName());
               }
          };

          Functor matchUserAge=new Functor(){
               public Object apply(Object... args){
                    return args[0].equals(((User)args[1]).getAge());
               }
          };
          
          Functor taller=new Functor(){
               public Object apply(Object... args){
                    return (Integer)args[0]<((User)args[1]).getHeight();
               }
          };

          User u=new User("gordon",25,173);
          
          Functor eqGordon=eq.to("gordon");
          System.out.println(eqGordon.apply("gordon"));
          
          Map<Object,Functor> sw=new HashMap<Object,Functor>();
          sw.put(true, trueAction);
          sw.put(false, falseAction);
          
          Functor and=t.and(t,t);
          Functor or=f.or(f,f,f);
          or.sw(true,trueAction,false,falseAction).apply();
          
          matchUserName.to("gordon1").or(matchUserAge.to(26)).or(taller.than(160)).sw(true,trueAction,false,falseAction).apply(u);
          
     }
}
class User{
     private String name;
     private int age;
     private int height;
     public User(String name,int age,int height){
          this.name=name;
          this.age=age;
          this.height=height;
     }
     public String getName() {
          return name;
     }
     public void setName(String name) {
          this.name = name;
     }
     public int getAge() {
          return age;
     }
     public void setAge(int age) {
          this.age = age;
     }
     public int getHeight() {
          return height;
     }
     public void setHeight(int height) {
          this.height = height;
     }
     
}
   发表时间:2008-03-01  
补充以下,http://www.iteye.com/topic/7569 我看的是这个link,有很详尽的介绍,我觉得fp的核心思想还是很启发的
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics