Wednesday, April 27, 2016

When do two variables refer to the same object, and how to copy a variable

One of my greatest confusions of JavaScript (and sometimes, greatest vexation) is when two variables refer to the same object. For example, ab, but c is not the same object as d here:
var a = b = [1, 2, 3];
a.shift();                     // 1
b;                             // [2, 3]
var c = d = "Hello, World!";
c = c.slice(0, -1);            // "Hello, World"
d;                             // "Hello, World!"
Here is the rule: when assigning one variable to another, the variables refer to the same object if and only if the object is not a primitive. So, if the object is a string, boolean, or number, they do NOT refer to the same object. Otherwise, they refer to the same object.

Overcoming variable reference with clones!

So, what can be done? What if I want to copy an object? I use a trick for Arrays all the time, inline in code: array.slice() returns a new copy of array, so that you can modify it without affecting the original. For objects, this is rather intuitive: iterate over each property and put it in a new object. So here's some code for a JavaScript "clone" method:
function clone(value){
	// throw an error for undefined input
	if(typeof value === "undefined")
		throw new ReferenceError("expected argument at 0: value from {clone}");
	
	// check for primitives
	if(typeof value === "number" ||
	   typeof value === "string" ||
	   typeof value === "boolean")
	   return value;	// nothing needs to be done
	
	// check for array/sliceable objects
	if(typeof value.slice !== "undefined")
		return value.slice();
	
	// otherwise, let's iterate through the object
	var retObj = {};
	for(var prop in value){
		retObj[prop] = value[prop];
	}
	return retObj;
}
Rather succinct, I like it enough.

Wednesday, April 20, 2016

Null-coalescing operators: when and when not to use them

Alright, this is something I've seen too many times in JavaScript code—even professionally—so I am going to address it here. This mistake is an easy one to make, and I use to make this mistake all the time; it's easy, simple, tempting, and nigh impossible to debug. I present to you: the null-coalescing operator. That is, the simple double-bar ||. Now, you might be wondering, "what's so bad about it?" Well, nothing. It's just bad programmers using it incorrectly.

The || operator is used in JavaScript not only to describe a disjunction, but also to specify default parameters to a function. (It's used in place of default parameter assignment, i.e. the one in function foo(bar=5){}, setting bar to 5.) It might be used (correctly) in the following situation:
function add(x, y, z){
 z = z || 0;
 return x + y + z;
}
This function successfully does what it's supposed to: add x and y, and z if specified. This works because of the short-circuiting property of ||. This operator is roughly equivalent to the following function:
function disjunction(LHS, RHS){
 if(LHS){
  return LHS;
 } else {
  return RHS;
 }
}
In this case, when the LHS is undefined, the value of the expression is RHS. However, the obverse of the statement (i.e. when LHS is defined then the value is not RHS and is thus is LHS) does not hold. Observe:
function multiply(x, y, z){
 z = z || 1;
 return x * y * z;
}
This works for most testcases (try it yourself) except for when z === 0; the expected result is 0, but what we get is x * y. Why? Well, let's look at what the disjunction yields. z = z || 1 becomes z = 0 || 1, which becomes z = 1, the incorrect result. This is why I follow this rule: when the RHS to the disjunction is truthy, use typeof z === "undefined" ? default : z; otherwise, one can use ||.

Saturday, April 2, 2016

Atoms and Tokens

The atomic score (or a.s., as abbreviated further in this post) of a program is the number of functional tokens in the program. Sometimes, the atomic score does not take into count separators such as parentheses and semicolons, in some languages. However, it is not immediately clear as to how many of these there are in a program.

I found a neat rule-of-thumb technique for calculating the a.s. of a JavaScript program, and any other line-independent program: put the smallest amount of code you can en each line without changing the program (i.e., without causing a syntax error). Let's look at the following program:
var userInput = Number(Prompt("Give me some input!!"));
userInput += 20;
alert("Your number plus twenty: " + userInput);
userInput >>= 1;
alert(userInput);
Let's line-ify this! We'll remove what whitespace we can in the process.
var
userInput
=
Number
(
Prompt
(
"Give me some input!!"
)
)
;
userInput
+=
20
;
alert
(
"Your number plus twenty: "
+
userInput
)
;
userInput
>>=
1
;
alert
(
userInput
)
;
This is 31 lines, or roughly 31 tokens. If we remove separators (parens etc.) and remove non-significant tokens (e.g. var and the trailing semicolon), this becomes 21 tokens:
userInput
=
Number
Prompt
"Give me some input!!"
;
userInput
+=
20
;
alert
"Your number plus twenty: "
+
userInput
;
userInput
>>=
1
;
alert
userInput

Examples

Let's try a some more examples that compare JavaScript, Jolf, a procedural golfing language, and some other various languages. For each task, I will provide a table for each language/language style and its respective a.s. Each program will be golfed to the intent of minimizing the a.s. This means that they will retain whitespace for readability. Each tokenized program will be followed by its a.s.

Task 1: Add two inputs

JavaScript (full program) Tokenized
alert(Number(prompt()) + Number(prompt()));
alert
Number
prompt
+
Number
prompt
a.s. = 5
JavaScript (ES6 arrow function) Tokenized
(a, b) => a + b;
a
,
b
=>
a
+
b
a.s. = 7
Jolf Tokenized
+jJ
+
j
J
a.s. = 3
Japt Tokenized
U+V
U
+
V
a.s. = 3
J, Jelly, APL Tokenized
+
+
a.s. = 1

Task 2: Summation over array

Takes input as [a, b, c, …, z] and outputs a + b + c + … + z.
JavaScript (full program) Tokenized
alert(prompt().split(",").reduce(function(p, c){
    return p + c;
}));
alert
prompt
.
split
","
.
reduce
function
p
,
c
return
p
+
c
a.s. = 15
JavaScript (ES6 arrow function) Tokenized
x => x.reduce((p, c) => p + c);
x
=>
x
.
reduce
p
,
c
=>
p
+
c
a.s. = 11
Jolf Tokenized
ux
u
x
a.s. = 2
Japt Tokenized
UrAB{A+B
U
r
A
B
{
A
+
B
a.s. = 8
J, Jelly, APL Tokenized
+/
+
/
a.s. = 2
Jelly Tokenized
S
S
a.s. = 1

Final remarks

Jelly is wicked short, and it's absolutely fantastic. Japt is beautiful, and I probably did something horribly wrong in my summation attempt. Finally, Jolf is my child, and it will also be first place in my mind.

Also, feel free to correct me if I'm wrong.

Thursday, March 17, 2016

Thinking more functionally: composition

Recently, I've been learning how to program in J (website wikipedia online interpreter random link). It can be thought of as a “functional” programming language, being based off of arrays and their modification; in fact, everything is an array: numbers are singleton arrays, lists are arrays, tables are arrays of arrays, etc.; I'm not entirely sure about where boxes fall, but that's a different story.)

Anyhow, functional programming is my favorite form of programming, and J and I become friends instantly. I tried APL, but I didn't feel like non-ASCII with a useful language. So J it was.

How is this related, you may ask? Well, for my favorite project Jolf I am planning on adding an aspect of functionality to it. And this is the content of the post: to walk you through how to make JavaScript functions more functional. This is the first post in a planned three-part series. This post will cover the code behind function composition, as well as provide an overview of the topic at hand.


Step 1: outline

So what do I plan on adding? Well, to put it simply, I'm planning on emulating J's feature. To illustrate this, I will assume we have functions add, multiply, etc. defined. So, let this serve as a basic list:
  1. Function composition. That is, for functions f and , the composition f ○ g is basically this:
    [f ○ g](x0,…,xn) ≡ f(g(x0,…,xn))
    [square ○ add](x, y) = (x + y)2
    Put concisely, it's performing function f on the result of function g(x0,…,xn).
  2. Function reflection. This is, for my purpose, converting an n-ary function to a unary one by using a single argument for all of its arguments. For a binary function f, it is thus:
    f~(x) = f(x, x)
    add~(x) = x + x
  3. Function Argument Binding. “Simply” stated, to bind (or link) an argument to a function is to have that argument be passed to that function; the function that describes this behaviour is the binding of that argument to the original function. So, if we have f(x,y) being a binary function, and f ⌒ n symbolically represents the action of binding, then
    [f ⌒ n](x) = f(x, n)
    [multiply ⌒ 2](x) = 2x

Show us some code already!

Yeah, yeah, alrighty then. Let's tackle function composition first. As we know, [f ○ g] = f(g(x)). For this, we're going to modify Array.prototype. (Don't give me any flak for doing this, as I honestly don't care if it conflicts with other programs modifying the prototype. I will slap you with a trout if you do!)

We are going to write a function that returns another function that will take an arbitrary amount of arguments. It will then call the argument to the prototype with the arguments to the returned function, then the result of that will be called by the this function. That's very wordy, huh? Let's work on function composition without a prototype before converting it. This is simple enough, no? We can return a function that calls f with the application of g on the arguments. For this, we can use Array#apply in tandem with Array.from.

(NOTE! Array.from is a feature with spotty support! If you are worried with cross-browser compatabiliy, use Array.prototype.slice.call(window, arrayLike) instead.)
function compose(f, g){
 return function(){
  return f(g.apply(window, Array.from(arguments)));
 }
}
You can test this code out and see that this works. Now, for some abstraction to the prototype! Since we are using the this value within the function, I will be using a closure for consistant behaviour. This is what the basic function may look like:
Function.prototype.compose = function(otherFunc){
 return (function(orig){
  return function(){
   return orig(otherFunc.apply(window, Array.from(arguments)));
  }
 })(this);
}
This works by creating a closure that takes the this as a variable. Other than that, the code is pretty much the same as the original code constructed.

Ready for a bit of a challenge? Good, because I'm not asking. You have homework!

Exercise A: it would be nice to be able to call Function#compose with multiple arguments without having to link multiple .compose calls together. Implement this in the body of Function#compose, either your own or the one I have provided. Here following are some test cases for you that your function should pass. Beforehand I have defined the actual functions used.
function square(x){ return x*x; }
function halve(x){ return x/2; }
function add(x, y){ return x+y; }
function id(x){ return x; }

id.compose(square,add)(5, 6);         // (5*6)^2 = 30^2 = 900
id.compose(square,halve)(3);          // (3/2)^2 = 1.5^2 = 2.25
                                      // make sure you implement it in this order!
halve.compose(square,id,add)(10, 4);  // ((10+4)^2)/2 = (14^2)/2 = 196/2 = 98
Exercise B: Do this code in the shortest amount of bytes. You may convert the code to ES6, if it helps. It must, however, be in JavaScript ;). The shortest code by the time the next post is made wins an honourable mention. (You may post it in the comments below.)

Tuesday, December 1, 2015

JavaScript: editing the built-ins

Whilst working on my newest project Jolf, I found the need to “edit” the built-ins of JavaScript. What does that mean? I wanted to edit the alert function so that it would also apply JSON.stringify on the input. How can I do this? Abusing JavaScript closures, of course!

JavaScript is interesting in that it really doesn't care. For the record. JavaScript doesn't care what you do with it. It grumbles only when it cannot possibly complete the action. Other than arguing with you when you use undefined variables (or make a mistake with variables at all) and when you completely garble the text you type, JavaScript rolls with it. That is why the aim of the post is even feasible.

Now, let's do a simple example. Let's fix the abomination of the isNaN function. One would expect that this function would return true if and only the input is NaN; however, this function tries first to evaluate the input as a number, then detecting if the result is NaN. So, isNaN(30) => false, but isNaN("Now in 3d!") => true. We want it so that isNaN(x) iff x is NaN. How? Observe that typeof NaN === "number". So, we check for a number type first, then call the old isNaN function, like so:

(function(oldNaNTest){
  return isNaN = function(inputNum){
    if(typeof inputNum!=="number") return false;  // NaN is a number
    else return oldNaNTest(inputNum);
  }
})(isNaN);

This creates an anonymous function with isNaN as an argument. And, as an argument, oldNaNTest is it's own, “separate” function (i.e. being distinct from `isNaN` and not merely a reference), so it retains `isNaN`s value. We do a type-check on the input, and proceed with returning the correct results.

But why an anonymous function? Well, consider what might happen if it wasn't. We would use something like this:

isNaN = function(inputNum){
  if(typeof inputNum!=="number") return false;
  else return isNaN(inputNum);
}

All would be well with non-numbers, as they return false; however, when given a number, we look to the else statement for our instruction. And what are we pointed to? Yep, isNaN. Is this wrong? Oh good heavens, yes it is! JavaScript looks at this statement and thinks, now, I need to look at the scope for a function named isNaN. Does that exist…yes! I found one! Here, take it! JavaScript hands you the function you just made. Why? Because you overwrote the old one, silly. We could create a reference, say, oldNaNTest, in the global scope, then proceed as normal. Two problems with that: one, you have an extra variable in the global scope. That's dirty! Secondly, if you try to get rid of that variable, you encounter an error, because 'ol JavaScript cannot find the function reference. :( It works, but it is neither elegant nor efficient.

Let's edit the eval command so that it it only evaluates the argument if it is a valid number. We can even use our new, correct isNaN function! We'll assume that is appended in the code snippet below.

(function(oldEval){
  return eval = function(arg){
    // test if valid number
    var testArg = Number(arg);  // will be NaN if invalid
    return isNaN(testArg)?arg:oldEval(arg);
  }
})(eval);

Simplex enough, eh?

Tuesday, November 10, 2015

The Cosmic Number

The Cosmic Problem (JSFiddle)

 ^ Well... what else do you want? Haha, just kidding. Of course I'm going to tell 'ya about it.

Pick a number. Any integer! Because four is the cosmic number.

“Um... 7?”

Seven is five, five is four, four is the cosmic number!

“... 27.”

Twenty-seven is twelve, twelve is six, six is three, three is five, five is four, four is the cosmic number!

What is the trick? (scroll down)


















Give up? Okay, you're going to hit yourself once I tell you: count the lengths of the number. Not the number itself, just the English.

I can just hear you. Don't feel too bad: it's a hard trick; I'm making you think of math when you need to be thinking of words. Now you've learned a neat party trick, but what gives? Of course, I did link to you a working example (that works with any integer between ±undecillion, I think), but that's not what's interesting.

Here's my thought process. First, I wonder if all numbers tend to four. Obviously, four tends to four, it being the only number whose value numbers its letters; thus, it is a natural tendency. Is it, however, a hard-and-fast rule?

I conjecture that this is indeed a rule. Now, a proof is needed. So let us begin!

First, let us describe the process.
  1. Start with a number, call it N0.
  2. Create a marker, call it k. Set k initially to 0.
  3. Let E(x) be the English representation of a number x; for example, E(20) is twenty, E(25) is twenty-five (notice the hyphen), and E(123) is one hundred twenty-three (notice the lack of the improper “and” and punctuation other than the dash).
  4. Let length(S) be the number of characters in string S. For example, length("Hello!") is 6, and length("O'Brien") is 7.
  5. Set Nk+1 = length(E(Nk))
  6. Does Nk+1 = 4?
    1. If so, terminate the process, returning k.
    2. Otherwise, increment k and go to step 5.

Conjecture: All integers, when the above process is repeatedly applied, tend to four, i.e., have “fourness”; equivalently, there is no integer for which the above process does not halt.

My idea at first was to find the name of the longest number within the context (call it M), calculate length(E(M)), and prove that every number from 1 to M tends to four. However, this is [a] a laborious task (also consider that length(E(10M±1)) > length(E(M) ) and [b] is unsatisfactory for all numbers. But what then shall be the argument form?

As it stands, this problem seems to be in, or at the very least related to, a currently unsolvable class of problems. A famous example would be the Collatz Conjecture. As Paul Erdős said,

Mathematics may not be ready for such problems.

So perhaps the proof is unattainable; the Cosmic Problem, like the Collatz Conjecture, involves in a case function's n-ness (i.e. that functions values tending towards n unconditionally). They both have common “seed” strings that are found (e.g. three to five to four). I, however, like to think that, since the Cosmic Problem is different in that it requires a non-mathematical conversion, that a proof can indeed be placed.

As of now, I have not found such a proof. But I'm still looking... ;)

Cheers!

Thursday, October 1, 2015

Esoteric - Simplex

No, no, no, not a simplex. I mean Simplex. Simplex is a single-char programming language, that is, all of its commands are single characters. I'll give a list of commands. Note that there are missing characters. These are either non-serious commands or are unimplemented.
Char Description
A    Adds the current byte to the previous byte and removes the current byte, decrements pointer
B    Converts the byte to binary
C    Asks for input and outputs it
D    Sets the current byte to twice its value
E    Raises the previous byte the (current byte)th power and removes the current byte, decrements pointer
F    Goes back n characters in the source code. Deletes the current byte.
G    Asks for single-char input and Manhattan-adds it to the current byte
I    Increments the byte
J    Binary left-shift
K    Binary right-shift
L    Goes left a byte
M    Decrements the byte
N    Goes (current byte) bytes backwards in the strip.
O    Goes to the (current byte)th position in the source code
R    Goes right a byte
S    Subtracts the current byte from the previous byte and removes the current byte, decrements pointer
T    Multiplies the previous byte by the current byte and removes the current byte, decrements pointer
U    Sets the current byte to half its value
V    Divides the previous byte by the current byte and removes the current byte, decrements pointer
W    Converts character to lower case (ASCII only)
X    Sets the current byte to a random integer from 0 to the current byte
Y    Floors the current value
Z    Converts character to upper case (ASCII only)
0-9  Manhattan-adds the number to the current byte (a+_m b=10a+b).
a    Make the number negative
b    Asks for input that will store each character into a byte on the strip in order.
c    Copies the current byte to the next byte and increments the pointer.
d    Reverses the entire strip/tuple (tuple check first)
e    Manhattan adds e = 2.718281828459045… to the stack.
f    Turns off safety mode.
g    Clears the strip and outputs it as ASCII characters.
 Tuple: deletes the tuple and output its characters as ASCII.
i    Asks for input as a number and stores it.
j    Inserts a new cell at the pointer, pushing everything right.
k    Skips the next command
l    Writes the number of non-empty cells in the strip to the current cell
m    Writes the index of the pointer to the cell
n    Logically negates the current byte
o    Outputs the current byte as a number
p    Silent prime detection
q    Checks for membership in a tuple (tupleByte member <=> member IN tupleByte)
 Like this:
   (t1,…,tN) M → (q) → (t1,…,tN) C
 C is whether (1) or not (0) M in (t1,…,tN)
r    Reverses the current number, removing leading zeroes. (1000→1)d
s    Outputs the current byte as a string character
t    Applies the next character's function (support […]) to every member of the current strip.
u    Increments the strip number
v    Decrements the strip number
x    Proceed (confirms)
y    Takes the current byte n and takes the n previous entries and pushes it into a tuple, then storing the tuple in the byte n bytes away OR if the current byte is a tuple, expands the tuple into n bytes (inclusive of the current byte, going out.)
z    Resets the byte to zero
|    Traverse the stack in the opposite direction
%    Modulo
?    Skips the next command if the byte is zero.
[…]  Block statement. Read by ? as "one character".
{…}  While loop. Execution continues until the current byte is zero.
!…!  Comments.
#    Stops evaluation
;    Pushes the current character to the outer program (ability to compile to itself); if there are characters on the outer program at the end of execution, evaluates the outer program as the inner program.
`    Suppresses evaluation of outer program and instead outputs it.
=    N = current index. S = strip. S[N] = S[N] == S[N-1] (Equality testing)
>    N = current index. S = strip. S[N] = S[N] > S[N-1] (Inequality testing)
<    N = current index. S = strip. S[N] = S[N] < S[N-1] (Inequality testing)
"    Toggle string-parsing mode. In string-parsing mode, char codes are written to the current byte and the pointer incremented. E.g., "Alive" would write values 65, 108, 105, 118, 101 to the current byte, the byte after that, etc.
(…)n Repeats … n times (pre-code compilation). If left off, assumed to be repeated once.
~~   Comment (pre-code, single-line)
Here are some example programs:

BF Interpreter

Since there is a surjection from Simplex to BF, BF can compile to Simplex. The compiler can be written in simplex:

br{j">"=?[v"R";Ru]"<"=?[v"L";Ru]"+"=?[v"I";Ru]"-"=?[v"M";Ru]"."=?[v"s";Ru]","=?[v"G";Ru]"["=?[v"{";Ru]"]"=?[v"}";Ru]LL}

Hello, World!

"Hello, World!"g