简体   繁体   English

我怎么能写一个更高阶的函数,比如map,或者在java中减少?

[英]How can I write a higher order function like map, or reduce in java?

I read an article on Joel On Software about the idea of using higher order functions to greatly simplify code through the use of map and reduce. 我读了一篇关于Joel On Software的文章,关于使用高阶函数通过使用map和reduce来大大简化代码的想法。 He mentioned that this was difficult to do in Java. 他提到在Java中这很难做到。 The article: http://www.joelonsoftware.com/items/2006/08/01.html 文章: http//www.joelonsoftware.com/items/2006/08/01.html

The example from the article below, loops through an array, and uses the function fn that was passed as an argument on each element in the array: 下面的文章中的示例循环遍历数组,并使用作为数组中每个元素的参数传递的函数fn:

function map(fn, a)
{
    for (i = 0; i < a.length; i++)
    {
        a[i] = fn(a[i]);
    }
}

This would be invoked similar to the below in practice: 这将在实践中类似于以下调用:

map( function(x){return x*2;}, a );
map( alert, a );

Ideally I'd like to write a map function to work on arrays, or Collections of any type if possible. 理想情况下,我想编写一个map函数来处理数组,或者尽可能编写任何类型的Collections。

I have been looking around on the Internet, and I am having a difficult time finding resources on the subject. 我一直在互联网上四处看看,我很难找到有关这个问题的资源。 Firstly, are anonymous functions possible in java? 首先,java中的匿名函数是否可行? Is this possible to do in another way? 这有可能以另一种方式吗? Will it be available in a future version of java? 它将在未来的java版本中提供吗? If possible, how can I do it? 如果可能,我该怎么办?

I imagine that if this is not possible in Java there is some kind of 'pattern'/technique that people use to achieve the same effect, as I imagine anonymous functions are a very powerful tool in the software world. 我想如果在Java中这是不可能的,人们会用某种“模式”/技术来达到同样的效果,因为我认为匿名函数是软件世界中非常强大的工具。 the only similar question I was able to find was this: Java generics - implementing higher order functions like map and it makes absolutely no sense to me. 我能找到的唯一类似的问题是: Java泛型 - 实现像map这样的高阶函数 ,这对我来说毫无意义。

Guava provides map (but it's called transform instead, and is in utility classes like Lists and Collections2 ). Guava提供了map(但它被称为transform而是在ListsCollections2等实用程序类中)。 It doesn't provide fold/reduce, however. 但是,它不提供折叠/缩小。

In any case, the syntax for using transform feels really clunky compared to using map in Scheme. 无论如何,与在Scheme中使用map相比,使用transform的语法非常笨拙。 It's a bit like trying to write with your left hand, if you're right-handed. 如果你是右撇子,这有点像用左手写字。 But, this is Java; 但是,这是Java; what do you expect. 你能指望什么。 :-P :-P

Single method anonymous classes provide a similar, but much more verbose, way of writing an anonymous function in Java. 单方法匿名类提供了一种类似但更冗长的在Java中编写匿名函数的方法。 For example, you could have: 例如,您可以:

Iterable<Source> foos = ...;
Iterable<Destination> mappedFoos = foos.map(new Function<Source, Destination>() 
{
    public Destination apply(Source item) { return ... }
});

For an example of a Java library with a functional style, see Guava 有关具有功能样式的Java库的示例,请参阅Guava

Looks like this one? 看起来像这个?

How can I write an anonymous function in Java? 如何在Java中编写匿名函数?

PS: try Functional Java . PS:尝试使用Functional Java Maybe it could give you hints. 也许它可以给你提示。

interface Func<V,A> {
    V call (A a);
}

static <V,A> List<V> map (Func<V,A> func, List<A> as) {
    List<V> vs = new ArrayList<V>(as.size());
    for (A a : as) {
        Vs.add(func.call(a));
    }
    return vs;
}

Paguro has an open-source implementation of higher order functions . Paguro有一个高阶函数的开源实现 Initial test show it to be 98% as fast as the native Java forEach loop. 初始测试显示它的速度是原生Java forEach循环的98%。 The operations it supports are applied lazily without modifying the underlying collection. 它支持的操作是懒惰地应用而不修改底层集合。 It outputs to type-safe versions of the immutable (and sometimes mutable) Clojure collections. 它输出到不可变(有时是可变的)Clojure集合的类型安全版本。 Transformable is built into Paguro's unmodifiable and immutable collections and interfaces . Paguro的不可修改和不可变的集合和接口内置了Transformable To use a raw java.util collection as input, just wrap it with the xform() function. 要使用原始java.util集合作为输入,只需使用xform()函数将其包装起来。

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

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