fork download
  1. import java.util.*;
  2. import java.util.function.*;
  3. import java.util.stream.*;
  4.  
  5. class Mappers {
  6. public static final Function<List<IntUnaryOperator>, UnaryOperator<List<Integer>>> reduce =
  7. opList -> argList -> argList.stream()
  8. .flatMap(arg -> IntStream.range(0, opList.size())
  9. .mapToObj(iLastOp -> opList.subList(0, 1 + iLastOp).stream()
  10. .reduce(IntUnaryOperator::andThen).get()
  11. .applyAsInt(arg)))
  12. .collect(Collectors.toList());
  13.  
  14. public static final Function<List<IntUnaryOperator>, UnaryOperator<List<Integer>>> collect =
  15. opList -> argList -> argList.stream()
  16. .flatMap(arg -> opList.stream()
  17. .collect(Collector.of(
  18. ArrayList<Integer>::new,
  19. (a, b) -> a.add(b.applyAsInt(a.isEmpty() ? arg : a.get(a.size() - 1))),
  20. (a, b) -> { throw new UnsupportedOperationException(); }))
  21. .stream())
  22. .collect(Collectors.toList());
  23.  
  24. public static final Function<List<IntUnaryOperator>, UnaryOperator<List<Integer>>> sane =
  25. opList -> argList -> {
  26. var r = new ArrayList<Integer>();
  27. for (var arg : argList)
  28. for (var op : opList) {
  29. arg = op.applyAsInt(arg);
  30. r.add(arg);
  31. }
  32. return r;
  33. };
  34. }
  35.  
  36. class Main {
  37. public static void main(String[] args) {
  38. List.of(Mappers.reduce, Mappers.collect, Mappers.sane).forEach(mapper -> {
  39. var list = mapper.apply(List.of(x -> x, x -> x + 1, x -> x * x)).apply(List.of(1, 2));
  40. System.out.println(list);
  41. });
  42. }
  43. }
Success #stdin #stdout 0.1s 34408KB
stdin
Standard input is empty
stdout
[1, 2, 4, 2, 3, 9]
[1, 2, 4, 2, 3, 9]
[1, 2, 4, 2, 3, 9]