fork download
  1. import java.util.*;
  2. import java.util.function.*;
  3. import java.time.*;
  4. class Ideone {
  5. private static <X, Y> Function<List<X>, List<Y>> mapi(BiFunction<Integer, X, Y> f) {
  6. return (xs) -> {
  7. List<Y> ys = new ArrayList<>();
  8. for (int i = 0; i < xs.size(); i++) {
  9. ys.add(f.apply(Integer.valueOf(i), xs.get(i)));
  10. }
  11. return ys;
  12. };
  13. }
  14. private static <X> Function<List<X>, List<X>> sort(Comparator<X> c) {
  15. return (xs) -> {
  16. List<X> ys = new ArrayList<>(xs);
  17. ys.sort(c);
  18. return ys;
  19. };
  20. }
  21. private static <X, Y> Function<List<X>, List<Y>> map(Function<X, Y> f) {
  22. return (xs) -> {
  23. List<Y> ys = new ArrayList<>();
  24. for (X x : xs) ys.add(f.apply(x));
  25. return ys;
  26. };
  27. }
  28. private static <T> Function<T, Void> function(Consumer<T> c) {
  29. return (x) -> {c.accept(x); return null;};
  30. }
  31. private static <E extends Comparable<E>> List<Integer> sorti(List<E> cs) {
  32. /*
  33.   record IndexedValue<T extends Comparable<? super T>>(Integer index, T value) implements Comparable<IndexedValue<T>> {
  34.   public int compareTo(IndexedValue<T> other) {
  35.   return value.compareTo(other.value);
  36.   }
  37.   }
  38.   */
  39. class IndexedValue<T extends Comparable<? super T>> implements Comparable<IndexedValue<T>> {
  40. private Integer index;
  41. private T value;
  42. public Integer index() {return index;}
  43. public IndexedValue(Integer index, T value) {this.index = index;this.value = value;}
  44. public int compareTo(IndexedValue<T> other) {return value.compareTo(other.value);}
  45. }
  46. return map(IndexedValue<E>::index).compose(sort(IndexedValue<E>::compareTo)).compose(mapi(IndexedValue<E>::new)).apply(cs);
  47. }
  48. private static Function<List<? extends Comparable>, List<Integer>> _sorti() {
  49. return Ideone::sorti;
  50. }
  51. public static void main(String[] args) {
  52. var f = _sorti().compose(_sorti());
  53. var g = function(System.out::println).compose(f);
  54. g.apply(Arrays.asList(1,100,10,10000,1000));
  55. g.apply(Arrays.asList(3,1,4,1,5,9,2));
  56. g.apply(Arrays.asList(0,1,0,1,0,1,0,1));
  57. g.apply(Arrays.asList("A","C","B","E","D"));
  58. g.apply(Arrays.asList(Month.MARCH, Month.JANUARY, Month.APRIL, Month.JANUARY, Month.MAY, Month.SEPTEMBER, Month.FEBRUARY));
  59. }
  60. }
  61.  
Success #stdin #stdout 0.11s 55836KB
stdin
Standard input is empty
stdout
[0, 2, 1, 4, 3]
[3, 0, 4, 1, 5, 6, 2]
[0, 4, 1, 5, 2, 6, 3, 7]
[0, 2, 1, 4, 3]
[3, 0, 4, 1, 5, 6, 2]