在java中小试FP(一)

最近看了点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;
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值