Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
D (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Example 2=== The following shows several D capabilities and D design trade-offs in a short program. It iterates over the lines of a text file named <code>words.txt</code>, which contains a different word on each line, and prints all the words that are anagrams of other words. <syntaxhighlight lang="D" line highlight="4,6,8,9,10,13,15"> import std.stdio, std.algorithm, std.range, std.string; void main() { dstring[] [dstring] signature2words; foreach (dchar[] w; lines(File("words.txt"))) { w = w.chomp().toLower(); immutable signature = w.dup.sort().release().idup; signature2words[signature] ~= w.idup; } foreach (words; signature2words) { if (words.length > 1) writeln(words.join(" ")); } }</syntaxhighlight> # <code>signature2words</code> is a built-in associative array that maps dstring (32-bit / char) keys to arrays of dstrings. It is similar to <code>defaultdict(list)</code> in [[Python (programming language)|Python]]. # <code>lines(File())</code> yields lines lazily, with the newline. It has to then be copied with <code>idup</code> to obtain a string to be used for the associative array values (the <code>idup</code> property of arrays returns an immutable duplicate of the array, which is required since the <code>dstring</code> type is actually <code>immutable(dchar)[]</code>). Built-in associative arrays require immutable keys. # The <code>~=</code> operator appends a new dstring to the values of the associate dynamic array. # <code>toLower</code>, <code>join</code> and <code>chomp</code> are string functions that D allows the use of with a method syntax. The name of such functions are often similar to Python string methods. The <code>toLower</code> converts a string to lower case, <code>join(" ")</code> joins an array of strings into a single string using a single space as separator, and <code>chomp</code> removes a newline from the end of the string if one is present. The <code>w.dup.sort().release().idup</code> is more readable, but equivalent to <code>release(sort(w.dup)).idup</code> for example. This feature is called UFCS (Uniform Function Call Syntax), and allows extending any built-in or third party package types with method-like functionality. The style of writing code like this is often referenced as [[Pipeline (Unix)|pipeline]] (especially when the objects used are lazily computed, for example iterators / ranges) or [[Fluent interface]]. # The <code>sort</code> is an std.algorithm function that sorts the array in place, creating a unique signature for words that are anagrams of each other. The <code>release()</code> method on the return value of <code>sort()</code> is handy to keep the code as a single expression. # The second <code>foreach</code> iterates on the values of the associative array, it is able to infer the type of <code>words</code>. # <code>signature</code> is assigned to an immutable variable, its type is inferred. # [[UTF-32]] <code>dchar[]</code> is used instead of normal [[UTF-8]] <code>char[]</code> otherwise <code>sort()</code> refuses to sort it. There are more efficient ways to write this program using just UTF-8.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)