2008-02-29
在java中小试FP(一)
关键字: fp
最近看了点functional programming的东西,觉得可以借鉴的地方还不少,所以做了点尝试
就是想把java这样写
从
到
在java中的fp尝试,可以用已有的程序创造新的程序
就是想把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;
}
}
评论
gordon@java
2008-03-01
补充以下,http://www.javaeye.com/topic/7569 我看的是这个link,有很详尽的介绍,我觉得fp的核心思想还是很启发的
发表评论
提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则
- 浏览: 728 次

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
在java中小试FP(一)
补充以下,http://www.javaeye.com/topic/7569 我 ...
-- by gordon@java -
在java中小试FP(二)
在java中实现functional programming的一些特性,并且可以 ...
-- by gordon@java -
在java中小试FP(二)
能不能先告诉我这一大段到底是要干什么?
-- by ray_linn -
javascript中的curry
gordon@java 写道笨笨狗 写道Prototype1.6就有这个扩展…… ...
-- by gordon@java -
javascript中的curry
笨笨狗 写道Prototype1.6就有这个扩展…… 嘿嘿,也是看了你的答复才 ...
-- by gordon@java






评论排行榜