什么是Java相当于C ++的累积或Groovy的注入?

时间:2021-06-07 21:00:35

Essentially, I'd like to do the following as a one-liner:

基本上,我想做一个单行的以下内容:

        int sum = initialValue;
        for (int n : collectionOfInts) {
            sum += n;
        }
        return sum;

I see that there's http://functionaljava.org/examples/1.5/#Array.foldLeft, but I'd rather not have to copy the collection.

我看到有http://functionaljava.org/examples/1.5/#Array.foldLeft,但我宁愿不必复制该集合。

3 个解决方案

#1


3  

I see that there's http://functionaljava.org/examples/1.5/#Array.foldLeft, but I'd rather not have to copy the collection.

我看到有http://functionaljava.org/examples/1.5/#Array.foldLeft,但我宁愿不必复制该集合。

If you use the foldLeft from IterableW instead of Array, you won't have to copy anything.

如果您使用来自IterableW的foldLeft而不是Array,则不必复制任何内容。

#2


0  

Sorry, it still doesn't exist in Java 7. You'll have to wait for Java 8, where Closures shall be implemented.

对不起,它在Java 7中仍然不存在。您将不得不等待Java 8,其中将实现Closures。

In the meantime, you can use FunctionalJava, Guava, or a JVM-compatible, closure-enabled language such as Groovy.

在此期间,您可以使用FunctionalJava,Guava或兼容JVM的,支持闭包的语言,例如Groovy。

#3


0  

Just for fun - here's how to do it without an external library:

只是为了好玩 - 这里是没有外部库的方法:

return fold(collectionOfInts, 0, ADD);

Oh, and here's the rest :)

哦,这是剩下的:)

static <X, Y> X fold(final Iterable<? extends Y> gen, final X initial, final Function2<? super X, ? super Y, ? extends X> function) {
  final Iterator<? extends Y> it = gen.iterator();
  if (!it.hasNext()) {
    return initial;
  }
  X acc = initial;
  while (it.hasNext()) {
    acc = function.apply(acc, it.next());
  }
  return acc;
}

static final Function2<Integer, Integer, Integer> ADD = new Function2<Integer, Integer, Integer>() {
  @Override
  public Integer apply(Integer a, Integer b) {
    return a + b;
  }
};

interface Function2<A, B, C> {
  C apply(A a, B b);
}

#1


3  

I see that there's http://functionaljava.org/examples/1.5/#Array.foldLeft, but I'd rather not have to copy the collection.

我看到有http://functionaljava.org/examples/1.5/#Array.foldLeft,但我宁愿不必复制该集合。

If you use the foldLeft from IterableW instead of Array, you won't have to copy anything.

如果您使用来自IterableW的foldLeft而不是Array,则不必复制任何内容。

#2


0  

Sorry, it still doesn't exist in Java 7. You'll have to wait for Java 8, where Closures shall be implemented.

对不起,它在Java 7中仍然不存在。您将不得不等待Java 8,其中将实现Closures。

In the meantime, you can use FunctionalJava, Guava, or a JVM-compatible, closure-enabled language such as Groovy.

在此期间,您可以使用FunctionalJava,Guava或兼容JVM的,支持闭包的语言,例如Groovy。

#3


0  

Just for fun - here's how to do it without an external library:

只是为了好玩 - 这里是没有外部库的方法:

return fold(collectionOfInts, 0, ADD);

Oh, and here's the rest :)

哦,这是剩下的:)

static <X, Y> X fold(final Iterable<? extends Y> gen, final X initial, final Function2<? super X, ? super Y, ? extends X> function) {
  final Iterator<? extends Y> it = gen.iterator();
  if (!it.hasNext()) {
    return initial;
  }
  X acc = initial;
  while (it.hasNext()) {
    acc = function.apply(acc, it.next());
  }
  return acc;
}

static final Function2<Integer, Integer, Integer> ADD = new Function2<Integer, Integer, Integer>() {
  @Override
  public Integer apply(Integer a, Integer b) {
    return a + b;
  }
};

interface Function2<A, B, C> {
  C apply(A a, B b);
}