1
Name:
Anonymous
2008-05-15 5:55
#include <stdio.h>
#include <stdlib.h>
typedef int(*intfunc_t)(int);
typedef struct {
intfunc_t call;
intfunc_t first;
intfunc_t second;
} composed_func;
void compose(intfunc_t a, intfunc_t b, composed_func *ret){
ret->first = a;
ret->second = b;
int f(int n){
return((*(ret->first))((*(ret->second))(n)));
}
ret->call = &f;
}
int main(){
int n = arc4random() % 64;
int f(int n){
return n << 1;
}
printf("f(%d) = %d\n", n, f(n));
printf("f(f(%d)) = %d\n", n, f(f(n)));
composed_func c;
compose(f, f, &c);
printf("compose(f,f)(%d) = %d\n", n, c.call(n));
return 0;
}
20
Name:
Anonymous
2008-05-15 12:20
>>19
C can't, but you can get pretty damn close in C++
#include <iostream>
struct IFoo {
virtual int func() = 0;
};
struct Bar {
IFoo* make_ifoo() {
struct Foo : IFoo {
virtual int func() {
return 42;
}
};
return new Foo();
}
};
int main( int argc, char *argv[] ) {
Bar b; std::cout << b.make_ifoo()->func() << std::endl;
return 0;
}
hur hur hurrr.