简体   繁体   English

如何构造FunctionalJava TreeMap?

[英]How to construct a FunctionalJava TreeMap?

I'm just starting to use the FunctionalJava library and wanted to make use of the immutable TreeMap. 我刚刚开始使用FunctionalJava库,并想使用不可变的TreeMap。 However I can't figure out how to create an empty one to start with when using a user defined class or interface. 但是,我无法弄清楚如何使用用户定义的类或接口创建一个空的开始。

fj.data.TreeMap<IAddress, Optional<ScanNode>> nodes = TreeMap.empty(Ord<IAddress>);

All the examples use predefined types like Ord.stringOrd. 所有示例都使用预定义的类型,例如Ord.stringOrd。 I'm totally not understanding how to create the proper Ord<IAddress>. 我完全不了解如何创建正确的Ord <IAddress>。

Could someone explain how to do this? 有人可以解释如何做到这一点吗?

Thanks, Derek 谢谢,德里克

Essentially a tree-map has to have some sort of an ordering on its elements, so you must describe how to order your IAddress. 本质上,树图必须对其元素进行某种排序,因此您必须描述如何对IAddress进行排序。

for example, lets say IAddress has 2 strings and an int (city, street, number), you could do the following: 例如,假设IAddress有2个字符串和一个int(城市,街道,数字),则可以执行以下操作:

// translate an IAddress to a P3 containing the important data
F<IAddress, P3<String, String, Integer>> toP3 = new F<...> () {
        P3<String, String, Integer> f(IAddress addr) { 
              return P.p(addr.getCity(), addr.getStreet(), addr.getNumber());
}

main () {
    // first map IAddress to a P3 using the function above, then simply order it by its fields
    Ord<IAddress> addrOrd = Ord.P3Ord(Ord.StringOrd, Ord.StringOrd, Ord.IntOrd).comap(toP3);

    fj.data.TreeMap<IAddress, Optional<ScanNode>> nodes = TreeMap.empty(addrOrd);
}

co-mapping means it first applies the function toP3 on the IAddress, getting back the P3, and then ordering it with the given P3 order. 共同映射意味着它首先将函数应用于IAddress上的P3,取回P3,然后以给定的P3顺序对其进行排序。

如果IAddress实现Comparable ,则还可以使用Ord.<IAddress>comparableOrd()为其构造一个Ord

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM