Skip to content

Instantly share code, notes, and snippets.

@SiAust
Created September 19, 2020 09:59
Show Gist options
  • Save SiAust/c9ff4181eb393fbc7cd40478035b1fad to your computer and use it in GitHub Desktop.
Save SiAust/c9ff4181eb393fbc7cd40478035b1fad to your computer and use it in GitHub Desktop.
A functional supplier that returns incremented int
import java.util.function.*;
class FunctionUtils {
public static Supplier<Integer> getInfiniteRange() {
int[] n = {0};
return new Supplier<Integer>() {
@Override
public Integer get() {
return n[0]++;
}
};
}
public static void main(String[] args) {
Supplier<Integer> sup = getInfiniteRange();
for(int i = 0; i < 5; i++) {
System.out.print(sup.get() + " ");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment