ডি (প্রোগ্রামিং ভাষা): সংশোধিত সংস্করণের মধ্যে পার্থক্য

বিষয়বস্তু বিয়োগ হয়েছে বিষয়বস্তু যোগ হয়েছে
ট্যাগ: মোবাইল সম্পাদনা মোবাইল ওয়েব সম্পাদনা উচ্চতর মোবাইল সম্পাদনা
 
ট্যাগ: মোবাইল সম্পাদনা মোবাইল ওয়েব সম্পাদনা উচ্চতর মোবাইল সম্পাদনা
২২ নং লাইন:
| operating_system = [[FreeBSD]], [[Linux]], [[macOS]], [[Microsoft Windows|Windows]]
}}
==কিছু উদাহরণ==
===উদাহরণ ১===
This example program prints its command line arguments. The <code>main</code> function is the entry point of a D program, and <code>args</code> is an array of strings representing the command line arguments. A <code>string</code> in D is an array of characters, represented by <code>char[]</code> in D1, or <code>immutable(char)[]</code> in D2.
 
<source lang="D" line highlight="3,5">
import std.stdio: writefln;
 
void main(string[] args)
{
foreach (i, arg; args)
writefln("args[%d] = '%s'", i, arg);
}
</source>
 
The <code>foreach</code> statement can iterate over any collection. In this case, it is producing a sequence of indexes (<code>i</code>) and values (<code>arg</code>) from the array <code>args</code>. The index <code>i</code> and the value <code>arg</code> have their types inferred from the type of the array <code>args</code>.
 
===উদাহরণ ২===
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.
 
<source lang="D" line highlight="5,7,9,10,11,14,16">
import std.stdio, std.algorithm, std.range, std.string;
 
void main() {
dstring[] [dstring] signs2words;
 
foreach (dchar[] w; lines(File("words.txt"))) {
w = w.chomp().toLower();
immutable key = w.dup.sort().release().idup;
signs2words[key] ~= w.idup;
}
 
foreach (words; signs2words) {
if (words.length > 1) {
writefln(words.join(" "));
}
}
}</source>
 
# <code>signs2words</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("&nbsp;")</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 UCFS (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's able to infer the type of <code>words</code>.
# <code>key</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 that use just UTF-8.
==তথ্যসূত্র==
{{সূত্র তালিকা}}