Code Golf

Handwritten by TVD

golf

The only thing better than Chamomile Tea is Chamomile Tea and Code Golf. Code Golf is a Stack Exchange community for programming puzzle enthusiasts and code golfers.

Coder sporting his lucky golf outfit. Not a real viking.

If you’ve ever met me in-person, you know I love talking development in general and code in particular. What’s interesting about Code Golf is the ability to learn from a global assortment of your peers in a condensed and rather fun period of time.

Let’s look at a few code snippets from an example Code Golf: Reinvent the For loop.

Reinventing the For Loop

The for loop is a fundamental iteration construct in many programming languages:

for(int i = 0; i < 100; i++) 
{
JustDoIt();
}

This Code Golf’s author challenges developers to:

Execute a basic for loop without using any repetition structures (for, foreach, while, do, etc).

The responses are nothing short of astounding really. There were submission for C, Haskell, x86 Assembly, Python, C#, Java, Ruby, Closure, Scala, LISP, Perl, Groovy, Basic and JavaScript.

I’m a big fan of JavaScript. In fact, I believe The Future is JavaScript. So let’s look at some of the more interesting JavaScript submission in Reinventing the For Loop.

JavaScript Submissions

Entry #1:

function f(m,c) {
m ? c()&f(--m,c) : 0
}

Entry #2:

function f(m,c) {
m&&c()&f(--m,c)
}

This version was inspired by Entry #1.

Entry #3:

(function i(s,r){
r += s + ' ';
s++ < 1e3 ? i(s,r) : console.log(r)
})(0,'')

Code Golf is pretty interesting. Hopefully, it gives you a good opportunity to sit back, relax and learn from our peers.

Happy Coding and Enjoy!

discuss on twitter


every day thousands of developers use our charts & gauges to get the job done right

JavaScript Charts JavaScript Gauges JavaScript Flight Gauges


← return to all articles