19. ActionScript
package com.mindviewinc.functional {
public function reduce(a:Array, reducer:Function):* {
var result:* = a[0]
for each(var item:* in a.slice(1, a.length))
result = reducer(result, item)
return result
}
}
44. interface addable { Object add(Object b);}
class X implements addable {
public Object add(Object b) {
return new Object(); // Test
}
}
class AddableNotFoundException extends Exception {}
public class DuckTyping {
public static Object sum(Object a, Object b)
throws AddableNotFoundException {
Class[] intfs = a.getClass().getInterfaces();
for(int i = 0; i < intfs.length; i++)
if(intfs[i] == addable.class)
return ((addable)a).add(b);
throw new AddableNotFoundException();
}
public static void
main(String[] args) throws Exception {
X a = new X();
X b = new X();
Object c = sum(a, b);
}
}