Skip to content

Instantly share code, notes, and snippets.

@headius
Created September 17, 2012 03:26
Show Gist options
  • Save headius/3735400 to your computer and use it in GitHub Desktop.
Save headius/3735400 to your computer and use it in GitHub Desktop.
FASTRUBY!
public class Hello extends RObject {
public Hello() {
RObject __last = RNil;
}
public RObject hello_world() {
RObject __last = RNil;
__last = puts(new RFixnum(1)._plus_(new RFixnum(1)));
__last = puts(new RString("Hello, world"));
return __last;
}
public RObject fib(RObject a) {
RObject __last = RNil;
if (a._lt_(new RFixnum(2)).toBoolean()) {
__last = a;
} else {
__last = fib(a._minus_(new RFixnum(1)))._plus_(fib(a._minus_(new RFixnum(2))));
};
return __last;
}
}
class Hello
def initialize
end
def hello_world
puts 1 + 1
puts "Hello, world"
end
def fib(a)
if (a < 2)
a
else
fib(a - 1) + fib(a - 2)
end
end
end
public class HelloRunner {
public static void main(String[] args) {
new Hello().hello_world();
for (int i = 0; i < 10; i++) {
long start = System.currentTimeMillis();
new Hello().fib(new RFixnum(35));
System.out.println(System.currentTimeMillis() - start);
}
}
}
system ~/projects/fastruby $ java HelloRunner
2
Hello, world
363
239
195
193
209
193
194
192
201
193
public class RBoolean extends RObject {
public final boolean bool;
public RBoolean(boolean bool) {
this.bool = bool;
}
public RObject to_s() {
return new RString(Boolean.toString(bool));
}
}
public class RFixnum extends RObject {
public final long fix;
public RFixnum(long fix) {
this.fix = fix;
}
public RObject to_s() {
return new RString(Long.toString(fix));
}
public RObject to_int() {
return this;
}
public RObject _plus_(RObject other) {
return new RFixnum(fix + ((RFixnum)other.to_int()).fix);
}
public RObject _minus_(RObject other) {
return new RFixnum(fix - ((RFixnum)other.to_int()).fix);
}
public RObject _lt_(RObject other) {
return fix < ((RFixnum)other.to_int()).fix ? RTrue : RFalse;
}
}
public class RKernel {
public static final RObject RNil = new RObject();
public static final RObject RTrue = new RBoolean(true);
public static final RObject RFalse = new RBoolean(false);
public RObject puts(RObject... objects) {
for (RObject object : objects) System.out.println(object);
return RNil;
}
public RObject to_int() {
throw new RuntimeException("can't convert to Integer: " + getClass().getName());
}
public RObject to_s() {
return new RString("#<" + getClass().getName() + ">");
}
public String toString() {
return to_s().toString();
}
public boolean toBoolean() {
if (this == RNil || this == RFalse) return false;
return true;
}
}
public class RObject extends RKernel {
public RObject Hello() {
throw new RuntimeException("Hello");
}
public RObject hello_world() {
throw new RuntimeException("hello_world");
}
public RObject _plus_(RObject arg0) {
throw new RuntimeException("_plus_");
}
public RObject fib(RObject arg0) {
throw new RuntimeException("fib");
}
public RObject _lt_(RObject arg0) {
throw new RuntimeException("_lt_");
}
public RObject _minus_(RObject arg0) {
throw new RuntimeException("_minus_");
}
}
public class RString extends RObject {
private final String str;
public RString(String str) {
this.str = str;
}
public String toString() {
return str;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment