Java 函数式编程实验(新添Keyword Message)

实验了以下内容:高阶函数,Currying,Lazy Evaluation,无穷流,Monad。都是很基本的东西。实现也是基于内部类的。没啥是了不起的。只是在给Lazy Evaluation造语法糖的时候,用了一下bytecode动态增强。给Lazy函数的lambda定义内部的所有的局部变量的读取操作前加了Lazy Evaluation过程。

[code]
private final static F<Boolean> TRUE = $(true);
private final static F<Integer> ONE = $(1);
private final static F<Integer> TWO = $(2);
private final static F<Integer> THREE = $(3);

private void demo() {
LF2<Integer, Integer, Integer> add = new LF2<Integer, Integer, Integer>() {
protected Integer lambda(Integer left, Integer right) {
System.out.println("calc " + left + " + " + right);
return left + right;
}
};
LF3<Boolean, Integer, Integer, Integer> select = new LF3<Boolean, Integer, Integer, Integer>() {
protected Integer lambda(Boolean arg1, Integer arg2, Integer arg3) {
if (arg1) {
return arg2;
}
return arg3;
}
};
LF1<Integer, Integer> onePlus = add._(ONE);
F<Integer> onePlusTwo = onePlus._(TWO);
F<Integer> onePlusThree = onePlus._(THREE);
System.out.println(select._(TRUE, onePlusThree, onePlusTwo)._());
}
[/code]

运行结果

[quote]
calc 1 + 3

4
[/quote]

结果分析:
1、实现了Curry(利用匿名类,绑定参数)
2、实现了Lazy Evaluation(利用载入时字节码修改,会添加进参数是否已经被求值的判断)
3、类型安全

缺点:
匿名类的语法
调用语法(用_表示调用仍然会有很多括号)

12.9:
更新为最新的语法
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值