Java Trivia

Implement the missing function body. These rules must be followed:

  • No using null
  • No throwing an exception/error
  • Function must terminate
  • No side-effecting
  • No type-casing (instanceof)
  • No type-casting

How many different ways of achieving the objective?

interface Function<A, B> {
  B apply(A a);
}
 
class C {
  static <A, B, C> Function<A, C> c(final Function<A, B> f,
                                    final Function<B, C> g) {
      .... todo
  }
}

20 Responses to “Java Trivia”

  1. Bas de Bakker Says:


    class C {
      static <A, B, C> Function<A, C> c(final Function<A, B> f, final Function<B, C> g) {
        return new Function() {
          public Object apply(Object a) {
            return "We don't need no composition.";
          }
        };
      }
    }

  2. Ricky Clarkson Says:

    return (Function<A, C>)(Function)f;

  3. Ionut G. Stan Says:

    I believe this is the answer:


    return new Function<A, C> {
        C apply(A a) {
            g.apply(f.apply(a));
        }
    }

    And the concept is function composition.

  4. dogstar Says:

    return new Function(){
    C apply(A a){
    return g.apply(f.apply(a));
    }
    }

  5. Alin Popa Says:

    Not very sure that I understand, but, this is a function composition ?
    e.g.

    // inc = i +1
    // dbl = i * 2
    Function c = C.c(inc, dbl);
    System.out.println(c.apply(2)); // (2 + 1) * 2 -> 6

    Right ?

  6. Peter Kofler Says:

    1 afaik:

    return new Function() {
    public C apply(A a) { return g.apply(f.apply(a)); }
    };

  7. Alin Popa Says:

    return new Function(){
    public C apply(A a){
    return g.apply(f.apply(a));
    }
    };

  8. Stefan W. Says:

    Is it intendet, that the classname C collides/is identical with the type-parameter C in ?

    “The type parameter C is hiding the type C” says eclipse.

    I come up with the following solution (without loocking at Rickys solution):

    		class XtoY implements Function 
    		{
    			public C apply (A a)
    			{
    				return g.apply (f.apply (a));
    			}
    		}
    		XtoY xtoy = new XtoY ();
    		return xtoy;
    

  9. Martin Krauskopf Says:

    Function composition (at least rot13-ed to not spoil directly for others).

    pbzcbfr :: (n -> o) -> (o -> p) -> (n -> p)
    pbzcbfr s t = t . s
    
    erghea arj Shapgvba() {
         choyvp P nccyl(N n) {
             erghea t.nccyl(s.nccyl(n));
         }
    };
    
  10. Andrey Vityuk Says:

    Method body should be as follows:

    return new Function() {
    public C apply(A a) {
    return g.apply(f.apply(a));
    }
    };

  11. Andrey Vityuk Says:

    Sorry, generics escaped…
    return new Function() {
       public C apply(A a) {
          return g.apply(f.apply(a));
       }
    };

  12. Ionut G. Stan Says:

    Is there any reason for which my answer doesn’t get moderated?

  13. Jim Says:

    Am I missing something?

    I feel I must be, b/c while I still haven’t been able to work myself through ‘Beginner Java Exercise with Data Types’ in 20 minutes, it took me about 10 seconds to independently come up with the solution Andrey posted above.

  14. Ricky Clarkson Says:

    In case it’s not obvious, I’d just like to point out that my answer was a joke, though a joke that will compile and will sometimes work at runtime. It gives a compile warning.

  15. Tom Says:

    Ricky: casting was disallowed in the rules :)

  16. Ricky Clarkson Says:

    No, type-casting *is* disallowed in the rules, but wasn’t at the time I wrote my comment.

  17. Tony Morris Says:

    Yes, this is true Ricky. I had clarified the rules. Sorry for the confusion.

  18. Berlin Brown Says:

    I don’t know why I found this so interesting but I did and wrote a verbose impl.

    So, like Bas, use anony innner-class impl. I didn’t see that immediately

    https://doingitwrongnotebook.googlecode.com/svn/trunk/doingitwrong_phase1/src/java/src/org/berlin/tonyblog/Trivia1.java

    Here is an example test case, don’t know if that is the intended purpose or not.

    public static void test2() {
    System.out.println(”Trivia 2 - return Function“);
    final Function[Integer, Integer] f_ab = new Function[Integer, Integer]() {

    @Override
    public Integer apply(Integer a) {
    return a * 2;
    }
    }; // End of Anon a-b

    final Function[Integer, Integer] g_bc = new Function[Integer, Integer]() {
    @Override
    public Integer apply(Integer b) {
    return b * 3;
    }
    }; // End of Anon a-b

    System.out.println(”Result: ” + c(f_ab, g_bc).apply(10));
    }

    —-

    Output is 60.

  19. Berlin Brown Says:

    Also, keep the Java posts coming for those of us that haven’t completely moved to Scala.

  20. λ Tony’s blog λ » Blog Archive » Optional -> a (negative proof) Says:

    [...] λ Tony’s blog λ The weblog of Tony Morris « Java Trivia [...]

Leave a Reply