java 函数中的函数_Java中的函数

我正在尝试在

Java中定义类似于Haskell的仿函数的类.因此,仿函数定义为:

/**

* Programming languages allow only (just simply enough) endofunctor, that are functors from and to the same category.

* In this case, the category is the one of the datatypes (in here Type, in order to make it more clear)

*/

public interface EndoFunctor extends Type {

/**

* The basic implementation for any element fx

* @param map Transformation function for the type parameter

* @param fx Element of the current class

* @param Target type

* @return transformed element through map

*/

EndoFunctor fmap(Function map, EndoFunctor fx);

}

如果我想实现一个XtbyC.gif仿函数,我必须写一些类似的东西

public class Id implements EndoFunctor {

protected X witness;

Id(X witness) { this.witness = witness; }

@Override

public Id fmap(Function map, Id fx) {

return new Id<>(map.apply(fx.witness));

}

}

该代码的问题在于Id< X>与EndoFunctor< X>类型不匹配.我如何在EndoFunctor接口中确定fmap,以便如果任何类型K< T>实现EndoFunctor< T>并且给出映射函数T-> U,然后K< U>.作为值返回,没有任何类型转换(也就是说,因为我知道我的对象是Id< T>,然后fmap的结果“必须是”Id< U>“,因此我向下转换类型的结果EndoFunctor< U>到这种类型)?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值