编写增量整数供应商
Writing an incremental integer supplier
我正在努力掌握 Java 8 函数式编程。我尝试编写以下内容 IntSupplier
"functionally" 但我总是遇到问题。
import java.util.function.IntSupplier;
@Test public void test_nonFunctional() {
IntSupplier supplier = new IntSupplier() {
private int nextInt = 0;
@Override public int getAsInt() {
return nextInt++;
}
};
}
这是我的尝试。这些问题在代码中标记为注释。
import org.junit.Test;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.IntSupplier;
public class IntSupplierTest {
@Test public void test_nonFunctional() {
IntSupplier supplier = new IntSupplier() {
private int nextInt = 0;
@Override public int getAsInt() { return nextInt++; }
}; // Works but is not functional.
}
@Test public void test_naive() {
int nextInt = 0;
IntSupplier supplier = () -> nextInt++; // Doesn't compile: requires nextInt to be final.
}
@Test public void test_nextIntIsFinal() {
final int nextInt = 0;
IntSupplier supplier = () -> nextInt++; // Doesn't compile: nextInt can't be incremented because it's final.
}
@Test public void test_useWrapper() {
final AtomicInteger nextInt = new AtomicInteger(0);
IntSupplier supplier = () -> nextInt.getAndIncrement(); // It is not the same as my original question as this test uses an extra object.
}
}
如果答案只是不使用额外的对象就无法完成,请直说。
您对问题的定义已经不起作用。在功能中,没有参数就不能有不同的输出。这就是定义。但是如何创建数字序列,您可以在 java 库中看到:java.util.function.IntUnaryOperator
。它是这样使用的:
IntStream.iterate(0, i -> i+1).limit(10).foreach(System.out::printLn);
有一种解决此问题的标准方法:
int[] nextInt = { 0 }; // optionally mark as final
IntSupplier supplier = () -> nextInt[0]++;
但它实际上并不比 "Works but is not functional" 解决方案更实用。
你可以这样做:
IntSupplier supplier = new AtomicInteger(0)::incrementAndGet;
我正在努力掌握 Java 8 函数式编程。我尝试编写以下内容 IntSupplier
"functionally" 但我总是遇到问题。
import java.util.function.IntSupplier;
@Test public void test_nonFunctional() {
IntSupplier supplier = new IntSupplier() {
private int nextInt = 0;
@Override public int getAsInt() {
return nextInt++;
}
};
}
这是我的尝试。这些问题在代码中标记为注释。
import org.junit.Test;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.IntSupplier;
public class IntSupplierTest {
@Test public void test_nonFunctional() {
IntSupplier supplier = new IntSupplier() {
private int nextInt = 0;
@Override public int getAsInt() { return nextInt++; }
}; // Works but is not functional.
}
@Test public void test_naive() {
int nextInt = 0;
IntSupplier supplier = () -> nextInt++; // Doesn't compile: requires nextInt to be final.
}
@Test public void test_nextIntIsFinal() {
final int nextInt = 0;
IntSupplier supplier = () -> nextInt++; // Doesn't compile: nextInt can't be incremented because it's final.
}
@Test public void test_useWrapper() {
final AtomicInteger nextInt = new AtomicInteger(0);
IntSupplier supplier = () -> nextInt.getAndIncrement(); // It is not the same as my original question as this test uses an extra object.
}
}
如果答案只是不使用额外的对象就无法完成,请直说。
您对问题的定义已经不起作用。在功能中,没有参数就不能有不同的输出。这就是定义。但是如何创建数字序列,您可以在 java 库中看到:java.util.function.IntUnaryOperator
。它是这样使用的:
IntStream.iterate(0, i -> i+1).limit(10).foreach(System.out::printLn);
有一种解决此问题的标准方法:
int[] nextInt = { 0 }; // optionally mark as final
IntSupplier supplier = () -> nextInt[0]++;
但它实际上并不比 "Works but is not functional" 解决方案更实用。
你可以这样做:
IntSupplier supplier = new AtomicInteger(0)::incrementAndGet;