fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. class Tuple<A, B> {
  8. private final A fst;
  9. private final B snd;
  10. public Tuple(A fst, B snd) {
  11. this.fst = fst;
  12. this.snd = snd;
  13. }
  14. public A fst() {
  15. return fst;
  16. }
  17. public B snd() {
  18. return snd;
  19. }
  20. public String toString() {
  21. return String.format("(%s,%s)", fst, snd);
  22. }
  23. }
  24.  
  25. interface Function<A, B> {
  26. B apply(A x);
  27. }
  28.  
  29. class IntStr implements Function<Integer, String> {
  30. public String apply(Integer x) {
  31. return x.toString();
  32. }
  33. }
  34.  
  35. class MakeTpl<A> implements Function<A, Tuple<A, A>> {
  36. public Tuple<A, A> apply(A x) {
  37. return new Tuple(x, x);
  38. }
  39. }
  40.  
  41. class Ideone {
  42. public static <A, B> List<B> transform(Function<A, B> f, List<A> xs) {
  43. List<B> ys = new ArrayList<>(xs.size());
  44. for (A x : xs) {
  45. ys.add(f.apply(x));
  46. }
  47. return ys;
  48. }
  49.  
  50. static <A, B> void test(Function<A, B> f, List<A> xs) {
  51. System.out.println(transform(f, xs));
  52. }
  53. static class MakeChrTpl extends MakeTpl<Character> {}
  54. public static void main (String[] args) throws java.lang.Exception {
  55. List<Integer> xs = Arrays.asList(1, 2, 3);
  56. List<Character> ys = Arrays.asList('a', 'b', 'c');
  57. test(new IntStr(), xs);
  58. test(new MakeChrTpl(), ys);
  59. }
  60. }
Success #stdin #stdout 0.07s 380160KB
stdin
Standard input is empty
stdout
[1, 2, 3]
[(a,a), (b,b), (c,c)]