balanced ternary

Jun 13, 2012 11:02



# what if there was a scale
# for weighing stuff like maybe grain or silver
# and what if it had an API

interface Scale {
void putNthWeightOnOtherSide(int n);
void putNthWeightOnSameSide(int n);
void addStuffUntilItBalances();
}

# we might create "frozen" verb-like nouns
# so that, if someone gives us some stuff
# in exchange for some other stuff
# we could inscribe one of these frozen things
# maybe in some sort of aperiodic crystal
# like a book or strand of DNA
# so that later if they change their mind
# and want to reverse the transaction
# (so-called 'undo')
# we can weigh out the appropriate amount

interface FrozenThing {
void run(Scale s);
}
class SameSide implements FrozenThing {
constructor(int n, FrozenThing restofthing);
void run(Scale s) {
s.putNthWeightOnSameSide(n);
restofthing.run(s);
}
private:
int n;
FrozenThing restofthing;
}
class OtherSide implements FrozenThing {
constructor(int n, FrozenThing restofthing);
void run (Scale s) {
s.putNthWeightOnOtherSide(n);
restofthing.run(s);
}
private:
int n;
FrozenThing restofthing;
}
class Done implements FrozenThing {
constructor();
void run(Scale s) {
s.addStuffUntilItBalances();
}
}

# if for some reason you wanted to represent
# the idea of giving someone two quantities
# you could introduce a new kind of frozen thing

class AndThen implements FrozenThing {
constructor(FrozenThing firstthing, FrozenThing secondthing);
void run(Scale s) {
firstthing.run(s);
secondthing.run(s);
}
private:
FrozenThing firstthing;
FrozenThing secondthing;
};

# but it would be more efficient,
# if you need to do this 'addition' often,
# if there was a way to refactor two frozen things
# into one 'batch' thing
# that weighs out the appropriate quantity all in one go
#
# anyway
#
# I think this is called the Command pattern
# but the point is that these frozen serialized commands
# are actually numbers
# (some sort of sparse balanced ternary)
# (unless I'm mistaken)
#
# anyway
#
# the point actually is
#
# refactoring is conceptually
# and historically
# prior to factoring
Previous post Next post
Up