Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Library to reimplement Java artihmetics

Name: Anonymous 2012-07-04 10:13

Hello /prog/

I have found out that Java is not the easiest language to understand. Lets look at following example about simple arithemtic.

2*1 + 4/(6+2)

Now, there are multiple problems here to understand for non-Java-experts. For instance, what are *, /, + characters supposed to mean? What are thos weird ( and ) characters? Is the order of numbers from left to right or right to left.

We can fix this. The object system of Java is powerful enough to handle this. When we replace this weird and cryptic internal arithmetic engine with our own OOP implementation, we can reduce the learning curve of Java language. After all, now programmer only needs to learn OOP.

Here's an implementation of classes that are easy to extend by inheritance.


public interface Node {
    public abstract double calculate();
}
public class OperatorNode implements Node {
    public static final int ADD = 1;
    public static final int SUB = 2;
    public static final int MUL = 3;
    public static final int DIV = 4;
    int calculationOperator;
    Node leftNode, rightNode;

    public OperatorNode(int oper, Node left, Node right) {
        calculationOperator = oper;
        leftNode = left;
        rightNode = right;
    }
    public double calculate() {
        double left = leftNode.calculate();
        double right = rightNode.calculate();
        switch (calculationOperator) {
            case ADD: return left + right;
            case SUB: return left - right;
            case MUL: return left * right;
            case DIV: return left / right;
        }
        return 0;
    }
}
public class ConstantNode implements Node {
    double value;
    public ConstantNode(double constant) {
        value = constant;
    }
    public double calculate() {
        return value;
    }
}


Now, how do we use this, you might ask. Look at the following example:


Node calculation = new OperatorNode(
        OperatorNode.ADD,
        new OperatorNode(
            OperatorNode.MUL,
            new ConstantNode(2),
            new ConstantNode(1)),
        new OperatorNode(
            OperatorNode.DIV,
            new ConstantNode(4),
            new OperatorNode(
                OperatorNode.ADD,
                new ConstantNode(6),
                new ConstantNode(2))
            )
        )
calculation.calculate();


This effectively evaluates the 2*1 + 4/(6+2) gibberlish we inspected earlier.

Our OOP solution also removes a lot of code duplication. Consider following example:


a = 2*1 + 4/(6+2);
b = 2*1 + 4/(6+2);


Terrible, right? And there's just no way to fix that with the old system. Now, lets rewrite that with clean OOP way.

Node calculation = new OperatorNode(
        OperatorNode.ADD,
        new OperatorNode(
            OperatorNode.MUL,
            new ConstantNode(2),
            new ConstantNode(1)),
        new OperatorNode(
            OperatorNode.DIV,
            new ConstantNode(4),
            new OperatorNode(
                OperatorNode.ADD,
                new ConstantNode(6),
                new ConstantNode(2))
            )
        )
a = calculation.calcultate();
b = calculation.calcultate()
calculation.calculate();


This effectively fixes our problem, where we had to change two arithmetic sentences when we wanted both variables result to change. Now we need to only change one place when changes to code is required.

This arithmetic library is now officially open sourced, with PPL (/prog/ public licence). You can do whatever you want with it. I hope some of you could contribute some extensions to it.

Also, if anyone has other ideas, how to improve Java language, by reimplementing language features with OOP, I would be very eager to see them.

Name: Anonymous 2012-07-04 11:10

GC is shit.

Name: Anonymous 2012-07-04 11:35

>>4
Agreed, but the following things are also shit:
-virtual memory allocation
-context switching
-serial processing
-electricity
-time
-concepts built from noise caused by the suboptimality other concepts (call this unavoidable. I say that it is possible to avoid by avoiding concepts that cause noise.)
-time-dependent concepts
-the 3 laws of thought

In general, the way the computers we nowadays use process information is similar to the brain of a primitive animal, except that would be an insult to the primitive animals since all the useful stuff is picked out and only the noise is left.
It goes like this:
-hurr I build a system to measure something
-durr I programmed a bug in the system
-derp the bug is funny so let's call it a feature
after a while the system is so full of concepts built on concepts that it does not even measure what originally was meant to be measured, but the noise between the concepts. That does not stop us from pretending the program is working just fine! Why not? Because we are so full of ourselves we can't see the all the "interesting phenomena" are what we originally programmed and then forgot, and not phenomena of "nature" or "life" itself! It's like building a house out of shit! And you must also build your house from shit because everyone else does it too! Our reasoning is, after all, shaped by our experiences!

And thanks to this we will never have quantum computers. Look at what happened with the QWERTY keyboard and you will understand why you will never get the x86 instructions out of your stream of consciousness.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List