Dittos in Array Literals
Color is often represented in computers in triplets of red, green, and blue intensities. Expressing a color is a matter of finding the right mix of these intensities. For example, here are the triplets for six of the most famous colors:
red = [1, 0, 0] green = [0, 1, 0] blue = [0, 0, 1] yellow = [1, 1, 0] cyan = [0, 1, 1] magenta = [1, 0, 1]
Shades of gray require less understanding of how the intensities combine; all three intensities are the same. These are three shades of gray defined in the CSS standard:
gray = [0.5, 0.5, 0.5] darkgray = [0.66, 0.66, 0.66] lightgray = [0.83, 0.83, 0.83]
The names are confusing, but that’s a story for another day.
I am unhappy with how grays are expressed. If I find that a shade is too dark or too light, I have to change three numbers. But not when I’m the one writing the programming language! In Twoville, therefore, I have added a special ditto operator that can be used inside array literals to repeat the previous element.
gray = [0.5, ~, ~] darkgray = [0.66, ~, ~] lightgray = [0.83, ~, ~]
The symbol for ditto is the tilde. I wanted it to be a single character for compactness. The other punctuation characters have too much semantic baggage. Tilde is typically only used for bitwise negation, and I’ve got a named function for that. Bitwise negation doesn’t deserve a compact symbol.
Besides expressing grays, the ditto operator makes it easy to enforce square dimensions:
dimensions = [10, ~]
I could have achieved repeating array elements in other ways, perhaps through an array constructor or through some sort of replication operator. These might look something like this:
gray = array(3, 0.5) gray = [0.5] x 3
But the ditto operator can act as a term in an expression:
powersOfTwo = [1, ~ * 2, ~ * 2, ~ * 2, ~ * 2]
There’s nothing quite as gratifying as being in charge of a language. Before that language has users, anyway.