10 One Liners to Impress Your Friends

As a bit of a programming language geek I find that comparing programming languages is a worthwhile exercise mainly because of the different techniques and styles that you are exposed to.
Learning multiple programming languages is one of the best things you can do to gain intuition about the most expressive way to write your code in any language.
No matter how much of an expert you are in a given language, learning a new one is often a humbling and educational experience and one that we should all try to do every so often to keep ourselves sharp.

I’ve seen several articles going around showcasing ‘one-liners’ (single lines of code) in different languages to perform a set of 10 simple tasks.
The first one I saw was done in Scala, and quickly spawned a number of imitations in other languages:

They are worth a look if only for a nice take on FizzBuzz – the Jeff Atwood smoke-test of programmer competency:

Enumerable.Range(1, 15).Select((i)=>i + (i%3==0?"fizz":"") + (i%5==0?"buzz":""));

C# is getting much more expressive with LINQ extension methods and lambda expressions but I think F# still has the edge with some of the inbuilt list processing functions.
Take partitioning a list into two lists based on a predicate in C#:

var passed = new List<int>();
var failed = new List<int>();
(from bucket in new[] { passed, failed } from i in new[] { 49, 58, 76, 82, 88, 90 } select new { bucket, i }).ToList().ForEach((tuple) => tuple.bucket.AddRange(Enumerable.Repeat(tuple, 1).Where((tup) => (tup.bucket == passed && tup.i > 60) || (tup.bucket == failed && tup.i <= 60)).Select((tup) => tup.i)));

Now in F#:

[49; 58; 76; 82; 88; 90] |> List.partition (fun i -> i > 60);;

If you look at the links above and see which language is most expressive for solving each problem you can really start to understand how languages can affect productivity.
Just being Turing Complete isn’t enough these days, and thank god for that

Tagged , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: