Fun With Closures: transform your method calls into sentences

You can transform your method calls into sentences by separating parameters into their own functions. Your method calls would be as close as possible to a real sentence if it weren’t for the problem of this darn syntax, where all the inputs need to be listed at the end of the sentence. What if you could mention the inputs anywhere in the sentence? It would be so much like prose.

Take an example from a Javascript implementation of the Game of Life that I recently wrote. My friend Sam and I discovered that, with a little work, this:

can become this:

The hell?! How does that become this?! Here’s how:

Original:

Improved (maybe):

It’s sort of like a unit test generator since you can simply test all conditions by inputting all possible permutations of your inputs. But then again, so is the original function. The only real difference is the syntactic cheat (and probably computationally expensive cheat) that allows me to morph the appearance, but not the functionality, of the test. This, of course, only works in certain programming languages, and is only beneficial if readability outweighs performance. Still, pretty cool, if I may say so myself.

Tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *