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 } }
May 31st, 2010 at 9:53 pm
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.";
}
};
}
}
May 31st, 2010 at 10:27 pm
return (Function<A, C>)(Function)f;
May 31st, 2010 at 11:25 pm
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.
May 31st, 2010 at 11:28 pm
return new Function(){
C apply(A a){
return g.apply(f.apply(a));
}
}
May 31st, 2010 at 11:41 pm
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 ?
May 31st, 2010 at 11:49 pm
1 afaik:
return new Function() {
public C apply(A a) { return g.apply(f.apply(a)); }
};
May 31st, 2010 at 11:58 pm
return new Function(){
public C apply(A a){
return g.apply(f.apply(a));
}
};
June 1st, 2010 at 8:43 am
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):
June 1st, 2010 at 4:44 pm
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)); } };June 2nd, 2010 at 4:52 pm
Method body should be as follows:
return new Function() {
public C apply(A a) {
return g.apply(f.apply(a));
}
};
June 2nd, 2010 at 4:56 pm
Sorry, generics escaped…
return new Function() {
public C apply(A a) {
return g.apply(f.apply(a));
}
};
June 2nd, 2010 at 6:26 pm
Is there any reason for which my answer doesn’t get moderated?
June 2nd, 2010 at 11:52 pm
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.
June 4th, 2010 at 7:58 pm
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.
June 10th, 2010 at 5:01 pm
Ricky: casting was disallowed in the rules
June 11th, 2010 at 6:24 am
No, type-casting *is* disallowed in the rules, but wasn’t at the time I wrote my comment.
June 11th, 2010 at 8:52 pm
Yes, this is true Ricky. I had clarified the rules. Sorry for the confusion.
June 12th, 2010 at 3:32 pm
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.
June 12th, 2010 at 4:11 pm
Also, keep the Java posts coming for those of us that haven’t completely moved to Scala.
June 20th, 2010 at 1:57 pm
[...] λ Tony’s blog λ The weblog of Tony Morris « Java Trivia [...]