Removing code duplication

前端 未结 5 884
我寻月下人不归
我寻月下人不归 2020-12-17 22:14

I am trying to create a little functional programming library for Java (just to scratch my own itch). While defining the higher-order functions for Lists,

5条回答
  •  臣服心动
    2020-12-17 22:57

    I don't believe Java's type system is sophisticated enough to address this, but Scala's is. With the 2.8 version of the collections library, they built a system to automatically create a collection of the appropriate type based on the collection you're working with. So, if you call filter on a List, it returns a new List. Call filter on a Set and you'll get a Set back. It does this while still only having a single implementation of filter.

    To learn more, take a look at Traversable and the stuff that uses it. I believe CanBuildFrom is where a lot of the magic happens.

提交回复
热议问题