Skip to main content
GameDev.net gamedev.net
🔒 Locked

Code as Poetry?

Started by CloudNine Jul 29, 2006 at 8:05 AM 12 replies 2.4k views
Original Post
CloudNine
CloudNine
Hi, This thread will probably qualify me for the geekiest thing I've done in a while ;), but do you think some code does resemble an art form, and if so, what's the prettiest bit of code you've (ever) written? Of course, the prettiness of code is relative to the language of the code itself. Here's a contendor for mine - it's short, to the point and simple - from my operating system, Whitix (although the more I look at it, the less poetic it looks :):

/***********************************************************************
 *
 * FUNCTION: VmLookupPage
 *
 * DESCRIPTION: Looks up a page in a vNode by file offset to see if anyone
 *		has mapped the page already.
 *
 * PARAMETERS: vNode - vNode that contains the shared list.
 *	       offset - file offset needed.
 *
 * RETURNS: the virtual page containing the file data.
 *
 ***********************************************************************/

static struct VMMapPage* VmLookupPage(struct VNode* vNode,Offset offset)
{
	struct VMMapPage* page;

	/* Has anyone else mapped it in? */
	if (!vNode || vNode->refs == 1 || ListEmpty(&vNode->sharedList))
		return NULL;

	/* Loop through the shared list and return the one with the offset we need. */
	ListForEachEntry(page,&vNode->sharedList,list)	
		if (page->offset == offset)
			return page;

	/* Not found. It will now be mapped in. */
	return NULL;
}

Trapper Zoid
Trapper Zoid
Although only one line, my favourite is this function header from "Pierre and the Fish".
void UpdateFishie(Fishie *fish);	// this is pure poetry!

And yes, that comment was already there [grin]
MumbleFuzz
MumbleFuzz
Reversing a list in Haskell:
reverse [] = []reverse (x:xs) = reverse xs ++ [x]
MumbleFuzz
kSquared
kSquared
This thread probably has broader appeal than just GP, so I'm moving it to the Lounge.
- k2 "Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ<
Don Carnage
Don Carnage
#include <iostream>#define Beauty int main()#define is {#define in std::cout<<#define the #define eye "Hello, world!"#define of ;#define beholder return 0; }Beauty is in the eye of the beholder
It is I, the spectaculous Don Karnage! My bloodthirsty horde is on an intercept course with you. We will be shooting you and looting you in precisely... Ten minutes. Felicitations!
superpig
superpig
Quote:
Original post by MumbleFuzz
Reversing a list in Haskell:
reverse [] = []reverse (x:xs) = reverse xs ++ [x]


Beautiful indeed. And from it you build an in-place swap:

swaps n[a..b] with n[x..y]
> swap a b x y = reverse (reverse n[a..b] ++ reverse n[b+1..x-1] ++ reverse n[x..y])

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse
Oberon_Command
Oberon_Command
One possibility:

void SDLVideo::TextOut(SDL_Surface *screen, char *str, int x, int y){     char current_char; // this is the current character we are going to print     int character_code = 0; // the final code we will use to draw it     int length = strlen (str); // calculate the length     int x_pos = 0;       // where we are on the screen     int line = 0;        // what line of text we're on          // now we iterate through the string, and print the characters one by one     for (int loop = 0; loop < length; loop++)     {         current_char = str[loop]; // get the next character                  if (str[loop] == '\n') // check for newline         {                // if there's a newline go to the next line         	line++;                	x_pos = 0;         }                  // check what character we got; got to make it fit into the          // character set         if (current_char <= 57)             character_code = (int)current_char - 32;         if (current_char > 57)            character_code = (int)current_char - 39;         if (current_char > 90)            character_code = (int)current_char - 45;                          // actually blit the character         BlitRegion(font[currentFont], screen, (x + (x_pos)), y + (line*16), (character_code * 8), 0, 8, 16);         // go to the next character spot on the screen         x_pos+=10;     } // end for} // end function


Another possibility, though I don't know quite what I was thinking when I wrote it (it's a function that runs another task using function pointers...).
int runExecutable0(EXECUTABLE_FILE *execFile){	void *pointer;	void (*function)();	// figure out where the code is in memory		pointer = execFile->code;	if (pointer == 0)			BSOD("Executable not found");	function = (void *) pointer; 	// actually run the code        function();		// NO MORE LINES AFTER THIS OR IT CRASHES!!!	// return success	return 1;}
MindWipe
MindWipe
Back to the topic of geeky things (I aer a bit drunk). I told my gf I couldn't talk to her because I wanted to implemnt collisions between rigid bodies or something like that. And today when I told her I was working on my physics engine she answered "Where did you get the parts for it?" thinking it was a real engine sort-of-thing.

Yes, time to eat and sleep to sober up.

/MindWipe
"To some its a six-pack, to me it's a support group."
Way Walker
Way Walker
#define a#define and#define are     z%=sizeof x-1;#define became  "hps\nwLL?smr\nhhtwdekwWDkbtkhdkvt?thLr"#define by      }#define called  putchar(x[z]);#define calls   const char*strchr(const char*,int);#define child#define For     int putchar(int);#define He#define Himself int main(void){#define His     return 0;#define I       "WlL\nhhLwvdpmpWvLtkhLLwothmppsLtkh lD",#define is      =#define Lamb    char x[]#define lamb    while(*y++){#define little  "WlL\nhhLsimaaLhommwe?intspsk lD"#define meek    " heWntsarwdoip\nAvDmlk?Lb", *y#define mild    "qeLhektkhLsbehLmtkamaAdhLlktkhdkvsapthLr"#define name    }#define thou    z=1;#define We      z+=strchr(x,*y)-x;For He calls Himself a LambHe is meek and He is mildHe became a little childI a child and thou a lambWe are called by His name
Thevenin
Thevenin
#teleport *GM Thevenin* to (466,546)

TheRXTP = new Regex(@"^(#teleport)[\s][\x2a](?
[a-zA-Z_0-9\s]*)[\x2a][\s](to)[\s]
[\x28](?[0-9]*)[,](?[0-9]*)[\x29]");
Maega
Maega
Quote:
Original post by MindWipe
I told my gf I couldn't talk to her because I wanted to implemnt collisions between rigid bodies or something like that.


/MindWipe


Wouldn't it have been easier to just ask her for sex instead of encoding it in euphemisms?
kSquared
kSquared
Here's my submission for poetry as code:

void CreateLimerick() {#define scanning terriblySlick  if (lines == 5  && rhymesWith("live") {doLaugh(); perform(new Trick()); }}
- k2 "Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ<

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.