posted by Owen Anderson on Mon 19th Apr 2004 05:43 UTC

"D language, Page 2/2"

So What Does It Look Like?

	//Copyright Walter Bright.  Used with permission.
	import std.c.stdio;
	import std.file;

	int main (char[][] args)
	{
		int w_total;
		int l_total;
		int c_total;
		int[char[]] dictionary;

		printf("   lines   words   bytes file\n");
		foreach (char[] arg; args[1 .. args.length])
		{
			char[] input;
			int w_cnt, l_cnt, c_cnt;
			int inword;
			int wstart;

			input = cast(char[])std.file.read(arg);

			for (int j = 0; j < input.length; j++)
 			{
				char c;

				c = input[j];
				if (c == '\n')
					++l_cnt;
				if (c >= '0' && c <= '9')
				{
				}
				else if (c >= 'a' && c <= 'z' ||
					c >= 'A' && c <= 'Z')
				{
					if (!inword)
					{
						wstart = j;
						inword = 1;
						++w_cnt;
					}
				}
				else if (inword)
				{
					char[] word = input[wstart .. j];

					dictionary[word]++;
					inword = 0;
				}
				++c_cnt;
			}
			if (inword)
			{
				char[] w = input[wstart .. input.length];
				dictionary[w]++;
			}
			printf("%8lu%8lu%8lu %.*s\n", l_cnt, w_cnt, c_cnt, arg);
			l_total += l_cnt;
			w_total += w_cnt;
			c_total += c_cnt;
		}

            if (args.length > 2)
		{
			printf("------------------------------\n%8lu%8lu%8lu total",
			l_total, w_total, c_total);
		}
		printf("--------------------------------------\n");

		foreach (char[] word1; dictionary.keys.sort)
		{
			printf("%3d %.*s\n", dictionary[word1], word1);
		}
		return 0;
	}

This program should look familiar to most C/C++ programmers: it's the classic "word count" program. While most of it should be easily comprehensible to anyone versed in C-like languages, I will highlight a few features:

	import std.c.stdio;
	import std.file;

These lines are D's version of includes. The first imports the plain C stdio functions (notably printf), while the second imports the D standard library (known as Phobos) file I/O systems.

	int main (char[][] args)

You'll notice here and throughout the program that D uses neither char* nor a string class for string values. D arrays are "smart arrays," which know their own length and are capable of most of the functionality of C++'s various STL array types.

	int[char[]] dictionary;

This declaration will look odd to C++ programmers, but it is in fact a familiar concept: it creates an array of integers that is indexed by strings, called an associative array. This is equivalent to Maps in both STL and JFC, but in D it is a core language feature rather than part of the standard library.

	foreach (char[] arg; args[1 .. args.length])

This line illustrates two new features of D: foreach and slicing. foreach replaces the need for iterators, as arg will take on the value of each element in the array as the loop executes. This is guaranteed to be in order if using a standard array, but no order guarantee is made for associative arrays. Slicing is the ability to declare an array as a subarray of another. In this case, args[1 .. args.length] is the subarray of args that includes all elements except the first. You can also note here that args, an array, knows its own length.

	foreach (char[] word1; dictionary.keys.sort)

Any array of entities that defines a comparison operator is sortable, such as the array of keys of an associative array. Again, this is a language feature rather than part of the standard library.

Beyond This Article

This article does not even begin to examine all the features of D. For instance, the above example program does not make use of any of D's object-oriented features, nor its generic programming capabilities, nor its built-in testing facilities. While all are certainly worthy of note, covering all in one article would be over-ambitious. Instead, here is a list of references where more information about D can be found:

The D Specification (Working Version)

The D Compiler (Linux and Windows)

The D Newsgroup (D Language Features Discussion)

The D.gnu Newsgroup (D for GCC Discussion)

The D Frontend for GCC (Work in Progress. Supports Linux and Mac OS X thus far.)

DSource.org (Newly formed host for D projects. Also hosts tutorials.)

D Links (Large list of D sites and libraries)

About the author:
The author, Owen Anderson, is a Computer Science student at Macalester College. He develops for Mac OS X and Linux, and has recently started the Docoa project to bridge D to Objective-C on Mac OS X.


If you would like to see your thoughts or experiences with technology published, please consider writing an article for OSNews.
Table of contents
  1. "D language, Page 1/2"
  2. "D language, Page 2/2"
e p (0)    103 Comment(s)

Technology White Papers

See More