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

Pages: 1-4041-

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 10:23

ENTERPRISE QUALITY ARITHMETICS

Name: Anonymous 2012-07-04 11:06

Still not as good as Lisp

Name: Anonymous 2012-07-04 11:10

GC is shit.

Name: Anonymous 2012-07-04 11:26

And there's just no way to fix that with the old system.
What if you did b = a?

Name: American 2012-07-04 11:29

*claps*

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.

Name: Anonymous 2012-07-04 11:38

>>4,7
ah, the retard mating dance.

Name: Anonymous 2012-07-04 11:55

>>5

No.

Name: Anonymous 2012-07-04 12:06

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.


for (int i=0; i < 10; i++) {
    System.out.println("fizz buzz");
}



class For {
    private Declaration decl;
    private TerminatingCondition cond;
    private IterationStatement iter;
    public For(decl, cond, count) {
        this.decl = decl;
        this.cond = cond;
        this.count = count;
    }
    public void execute() {
        decl.execute();
        while (cond.evaluate()) {
            iter.execute();
        }
    }
}

interface Declaration {
    public abstract void execute();
}

interface TerminatingCondition {
    public abstract boolean evaluate();
}

interface IterationStatement {
    public abstract void execute();
}



    /*
     * Wrapper array to permit access to the counter variable from anonymous inner class
     * methods Declaration.execute() and Condition.evaluate().
     */
    final int[] i = new int[1];
   
    For f0r = new For(
        new Declaration() {
            public void execute() {
                i[0] = 0;
            }
        },
        new Condition() {
            public boolean evaluate() {
                return i[0] < 10;
            }
        },
        new IterationStatement() {
            public void execute() {
                System.out.println("fizz buzz");
            }
        });
    f0r.execute();

Name: Anonymous 2012-07-04 12:07

>>8
But have you watched your Eva today?

Name: sage 2012-07-04 12:08

>>10 here. Responding to community demands, version 2.0 of my API will support a body statement in For loops.

Name: Anonymous 2012-07-04 12:27

>>10
Brilliant!

Name: Anonymous 2012-07-04 12:53

Excellent work.

Name: Anonymous 2012-07-04 13:11

It took me over an hour to realize it is a joke, and I still am not 100% sure if it is a joke. Does that make me retarded or enlightened?
Needs more eval, though.

Name: Anonymous 2012-07-04 13:47

>>10
you don't increase i anywhere, you are using an array instead of ArrayList i would suggest:

class For {
    private IterationStatement iter;
    private IterationObject var;

    public For(var, iter) {
        this.var = var;
        this.iter = iter;
    }
    public void execute() {
        var.setup();
        while (var.evaluateCondition()) {
            iter.execute();
            var.iterate();
        }
    }
}

interface IterationObject{
   public abstract void setup();
   public abstract boolean evaluate();
   public abstract void iterate();
}

interface IterationStatement {
    public abstract void execute();
}


Class MyFor implements IterationClass{
  private Integer i;
  MyFor(){
   setup();
  }
  public void setup(){
   i = 0;
  }
  public evaluate(){
     i < 10;
  }
  public iterate(){
    i = i + 1;
  }

}

    For f0r = new For(
        new MyFor(),
        new IterationStatement() {
            public void execute() {
                System.out.println("fizz buzz");
            }
        });
    f0r.execute();



learn to OOP scum

Name: Anonymous 2012-07-04 13:56

>>16
Learn to code tag scum

Name: Anonymous 2012-07-04 14:11

I lold

Name: Anonymous 2012-07-04 14:56

I System.out.println((new InternetAbbreviation(new InternetWord(Util.Internet.LOL.FirstWord), new InternetWord(Util.Internet.LOL.SecondWord), new InternetWord(Util.Internet.LOL.ThirdWord))).toString())'d

Name: Anonymous 2012-07-04 14:59

>>17
Oh, another idiot who needs syntax highlighting because he is dependent on an IDE, learn to program scum

Name: Anonymous 2012-07-04 16:54

Library to reimplement Java artihmetics
reimplement
rei

This thread is now about Rei.

Name: Anonymous 2012-07-04 16:56

>>20
u mad

Name: Anonymous 2012-07-04 17:28

>>19
Why would you do this to me.

Name: Rei 2012-07-04 17:50

>>21
TUMBLING DOWN
TUMBLING DOWN
TUMBLING DOWN

Name: Anonymous 2012-07-04 18:27

>>24
Are you really Rei? Could you please rub your white panties and bra on my face a bit. Thank you.

Name: Anonymous 2012-07-05 2:14

I have updated the OperatorNode class. It now supports exponent operators.

Here's the code:

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;
    public static final int POW = 5;
    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;
            case POW: return Math.pow(left, right);
        }
        return 0;
    }
}

Name: Anonymous 2012-07-05 2:15

>>10
BTW, keep up the good work.

Name: Anonymous 2012-07-06 2:20

New node types, FunctionNode and SqrtNode


class FunctionNode implements Node {
    Node parameter;
    public FunctionNode(Node parameter) {
        this.parameter = parameter
    }
    public double calculate() {
        return function(parameter.calculate());
    }
    public abstract double function(double value);
}

class SqrtNode extends FunctionNode {
    public double function(double value) {
        return Math.sqrt(value);
    }
}


Example usage:

Node double_two = new FunctionNode() {
    public double function(double value) {
        return 2 * value;
    }
};
Node sqrt_of_four = new SqrtNode(new ConstantNode(4))

Name: Anonymous 2012-07-06 13:45

If you think YHBT, take a look at the real actual BigInteger package from java.math and reconsider.

Name: Anonymous 2012-07-06 13:49

>>29
OH GOD NO

Name: Anonymous 2012-07-06 17:10

quality shit posts

Name: Anonymous 2012-07-06 23:34

>>13
You mean brillant!

Name: Anonymous 2012-07-07 14:21

new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new

Name: Anonymous 2012-07-07 19:26

>>33
delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete delete

Name: Anonymous 2012-07-11 13:09

>>1
Hello,

After many days of coding, I finally managed to make useful contribution to your library.

It's a class that gives constant value of PI to you.

Here's the code:

class PiNode extends ConstantNode {
    public PiNode() {
        value = 3.14;
    }
}

Name: Anonymous 2012-07-11 13:57

>>35
OP here,
you need to document your code before I can accept it to my library. Please post here when you have done so.

Name: Anonymous 2012-07-11 14:05

>>35
I have improve your code:
class PiNode extends ConstantNode {
    public PiNode() {
        value = 3.14;
    }
}

class PiNodeFactory {
    public PiNode node;
    public PiNodeFactory() {
        node = new PiNode();
    }
}

See how I used a factory class to create the object automatically rather than using its constructor. This is more robust and scalable, perfect for enterprise modules.

Name: Anonymous 2012-07-11 14:33

>>37
OP here
You forgot to document your factory code.

Name: Anonymous 2012-07-11 20:01

>>38
I don't document my code because I'm /hc/.

Name: Anonymous 2012-07-11 22:50


(define (node fn . nodes)
  (lambda ()
    (apply fn (map (lambda (f) (f)) nodes))))

(define (constant value)
  (lambda ()
    value))

(define root (node + (node - (constant 2) (constant 23)) (constant 34)))
(root)

Name: Anonymous 2012-07-11 23:00

This is why God created operator overloading.

Name: Anonymous 2012-07-11 23:13

>>41
fuck off, terry

Name: Anonymous 2012-07-12 2:14

>>40
lamb duh

Name: Anonymous 2012-07-12 2:17

DRINK THE BLOOD OF THE LAMBDA

Name: Anonymous 2012-07-12 10:25

>>36
Ok, I documented the code
[code]
class // declare class
PiNode // named PiNode
extends // that extends
ConstantNode // class named ConstantNode
{ // begin block
public // declare public identifier
PiNode // named  PiNode
( // begin argument list
) // end argument list
{ // begin block
value = // set value of "value"
3.14 // to 3.14
; // end statement
} // end block
} // end block
[code]

Name: Anonymous 2012-07-12 10:26

Sorry mister, forgot to close code tags

class // declare class
 PiNode // named PiNode
 extends // that extends
 ConstantNode // class named ConstantNode
 { // begin block
 public // declare public identifier
 PiNode // named  PiNode
 ( // begin argument list
 ) // end argument list
 { // begin block
 value = // set value of "value"
 3.14 // to 3.14
 ; // end statement
 } // end block
 } // end block

Name: Anonymous 2012-07-16 13:38

Name: Anonymous 2012-07-17 16:52

Our thinking is influenced by the majority of our experience.
This is the deepest thing I have heard, other than "even in heaven, Satan is present".

What I am trying to say, that we might actually need to code shittily, to have our code evolve naturally.

Name: Anonymous 2012-07-17 21:40

>>21
that was lame as fuck

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