A piece of beautiful code
February 04, 2009
A couple of examples of beautiful code
A long time back, browsing through a book, I came across code for strcpy function which is part of standard library in C:
strcpy(char *s1,char *s2)
{
while (*s1++ = *s2++)
}
Just the beauty of the code made me fall in love with C and programming all over again. Then, I got busy with courses, jobs, projects and releases. Recenty, while programming with Ruby, I had to randomize an array of objects before it got displayed. I thought, I should consult Google's brain before I whip up the familiar Knuth algorithm. This is what I found:
(1..10).sort_by { rand }
Randomizes an array of numbers 1 to 10. Oh the beauty!
More details here if you want to learn about stcpy. And here is where I spotted the Ruby code.
{
while (*s1++ = *s2++)
}
Just the beauty of the code made me fall in love with C and programming all over again. Then, I got busy with courses, jobs, projects and releases. Recenty, while programming with Ruby, I had to randomize an array of objects before it got displayed. I thought, I should consult Google's brain before I whip up the familiar Knuth algorithm. This is what I found:
(1..10).sort_by { rand }
Randomizes an array of numbers 1 to 10. Oh the beauty!
More details here if you want to learn about stcpy. And here is where I spotted the Ruby code.