xxl.mathematica.functional.NestList Maven / Gradle / Ivy
package xxl.mathematica.functional;
import io.vavr.control.Try;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.function.Function;
/**
* 嵌套列表
*/
public class NestList {
/**
* 将函数作用于一个对象上n次,并返回中间过程列表
*
* @param function
* @param initValue
* @param n
* @param
* @return
*/
public static List nestList(Function function, T initValue, int n) {
return Try.ofCallable(new Callable>() {
@Override
public List call() throws Exception {
List res = new ArrayList<>();
T temp = initValue;
res.add(temp);
for (int i = 0; i < n; i++) {
temp = function.apply(temp);
res.add(temp);
}
return res;
}
}).getOrNull();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy