What Color is Your Function? – journal.stuffwithstuff.com
archive
next >
What Color is Your Function?
I don’t know about you, but nothing gets me going in the morning quite like a<br>good old fashioned programming language rant. It stirs the blood to see someone<br>skewer one of those “blub” languages the plebians use, muddling through<br>their day with it between furtive visits to StackOverflow.
(Meanwhile, you and I, only use the most enlightened of languages. Chisel-sharp<br>tools designed for the manicured hands of expert craftspersons such as<br>ourselves.)
Of course, as the author of said screed, I run a risk. The language I mock<br>could be one you like! Without realizing it, I could have let the rabble into my<br>blog, pitchforks and torches at the ready, and my fool-hardy pamphlet could draw<br>their ire!
To protect myself from the heat of those flames, and to avoid offending your<br>possibly delicate sensibilities, instead, I’ll rant about a language I just made<br>up. A strawman whose sole purpose is to be set aflame.
I know, this seems pointless right? Trust me, by the end, we’ll see whose face<br>(or faces!) have been painted on his straw noggin.
A new language#a-new-language
Learning an entire new (crappy) language just for a blog post is a tall order,<br>so let’s say it’s mostly similar to one you and I already know. We’ll say it has<br>syntax sorta like JS. Curly braces and semicolons. if, while, etc. The<br>lingua franca of the programming grotto.
I’m picking JS not because that’s what this post is about. It’s just that it’s<br>the language you, statistical representation of the average reader, are most<br>likely to be able grok. Voilà:
function thisIsAFunction() {<br>return "It's awesome";<br>Because our strawman is a modern (shitty) language, we also have first-class<br>functions. So you can make something like this:
// Return a list containing all of the elements in collection<br>// that match predicate.<br>function filter(collection, predicate) {<br>var result = [];<br>for (var i = 0; i collection.length; i++) {<br>if (predicate(collection[i])) result.push(collection[i]);<br>return result;<br>This is one of those higher-order functions, and, like the name implies, they<br>are classy as all get out and super useful. You’re probably used to them for<br>mucking around with collections, but once you internalize the concept, you start<br>using them damn near everywhere.
Maybe in your testing framework:
describe("An apple", function() {<br>it("ain't no orange", function() {<br>expect("Apple").not.toBe("Orange");<br>});<br>});<br>Or when you need to parse some data:
tokens.match(Token.LEFT_BRACKET, function(token) {<br>// Parse a list literal...<br>tokens.consume(Token.RIGHT_BRACKET);<br>});<br>So you go to town and write all sorts of awesome reusable libraries and<br>applications passing around functions, calling functions, returning functions.<br>Functapalooza.
What color is your function?#what-color-is-your-function
Except wait. Here’s where our language gets screwy. It has this one peculiar<br>feature:
1. Every function has a color.
Each function—anonymous callback or regular named one—is either red or<br>blue. Instead of a single function keyword, there are two:
blue_function doSomethingAzure() {<br>// This is a blue function...
red_function doSomethingCarnelian() {<br>// This is a red function...<br>There are no colorless functions in the language. Want to make a function?<br>Gotta pick a color. Them’s the rules. And, actually, there are a couple more<br>rules you have to follow too:
2. The way you call a function depends on its color.
Imagine a “blue call” syntax and a “red call” syntax. Something like:
doSomethingAzure()blue;<br>doSomethingCarnelian()red;<br>When calling a function, you need to use the call that corresponds to its color.<br>If you get it wrong—call a red function with blue after the parentheses<br>or vice versa—it does something bad. Dredge up some long-forgotten<br>nightmare from your childhood like a clown with snakes for arms hiding under<br>your bed. That jumps out of your monitor and sucks out your vitreous humour.
Annoying rule, right? Oh, and one more:
3. You can only call a red function from within another red function.
You can call a blue function from within a red one. This is kosher:
red_function doSomethingCarnelian() {<br>doSomethingAzure()blue;<br>But you can’t go the other way. If you try to do this:
blue_function doSomethingAzure() {<br>doSomethingCarnelian()red;<br>Well, you’re gonna get a visit from old Spidermouth the Night Clown.
This makes writing higher-order functions like our filter() example trickier.<br>We have to pick a color for it and that affects the colors of the functions<br>we’re allowed to pass to it. The obvious solution is to make filter() red.<br>That way, it can take either red or blue functions and call them. But then we<br>run into the next itchy spot in the hairshirt that is this language:
4. Red functions are more painful to call.
For now, I...