Difference between revisions of "Programming in D for Python Programmers"

From D Wiki
Jump to: navigation, search
m (broken links corrected)
 
(23 intermediate revisions by 7 users not shown)
Line 1: Line 1:
 +
<table style="border-collapse: collapse;">
 +
 +
<tr style="border-bottom: 1px solid #ccc; vertical-align: top;">
 +
<td style="padding: 5px 10px; text-align: center">
 +
 +
[[File:51o29P-BnzL.jpg|100px||link=https://good-tech-school.thinkific.com/courses/dforpython]]
 +
 +
</td>
 +
<td style="padding: 5px 10px;">
 +
 +
(Non-accredicted) Masters D degree:
 +
'''[https://teachsector.com/dforpython/ D, the Best Programming Language, for Former Python Developers]'''<br>
 +
Victor Porton<br>
 +
19 May 2021
 +
 +
</td><td style="padding: 5px 10px;">
 +
"High Master" diploma of D development. No prerequisites (except of Python knowledge).
 +
 +
D programming language or DLang for Python programmers: rapid software development of high performance software and a reliable software technology. “There is no best programming language”, they say. There is, for most applications it is D.
 +
The course starts from a comparison of D to Python. Then it explains D starting from simple features (types, variables, functions, expressions, statements) up to advanced ones (object oriented programming, templates, mixins, contract programming, overloading of operators, etc.)
 +
 +
Currently we have only text lectures and exams, but addition of video lectures is planned.
 +
 +
</td><td style="padding: 5px 10px; ">
 +
 +
* [https://good-tech-school.thinkific.com/courses/dforpython Course]
 +
 +
</td>
 +
</tr>
 +
</table>
 +
 
"Often, programming teams will resort to a hybrid approach, where they will mix Python and C++, trying to get the productivity of Python and the performance of C++.  The frequency of this approach indicates that there is a large unmet need in the programming language department.
 
"Often, programming teams will resort to a hybrid approach, where they will mix Python and C++, trying to get the productivity of Python and the performance of C++.  The frequency of this approach indicates that there is a large unmet need in the programming language department.
  
Line 10: Line 41:
  
  
D is Like Native Python
+
== D is Like Native Python ==
-----------------------
 
 
*[http://bitbashing.io/2015/01/26/d-is-like-native-python.html  D is like Native Python]
 
*[http://bitbashing.io/2015/01/26/d-is-like-native-python.html  D is like Native Python]
  
Line 31: Line 61:
 
*[http://tech.adroll.com/blog/data/2014/11/17/d-is-for-data-science.html AdRoll: D is for Data Science]
 
*[http://tech.adroll.com/blog/data/2014/11/17/d-is-for-data-science.html AdRoll: D is for Data Science]
  
 +
*[https://d.readthedocs.org/en/latest/examples.html#plotting-with-matplotlib-python Plotting in D using python's matplotlib]
  
Generators and List Comprehensions
+
 
----------------------------------
+
== IPython Notebook / Jupyter ==
 +
An early-stage extension to Python exists to allow writing D extensions inline within an ipython/Jupyter notebook
 +
*[http://forum.dlang.org/post/dehleaecsihenaapyqhl@beta.forum.dlang.org Introduction on forum]
 +
*[https://github.com/DlangScience/PydMagic Source Code]
 +
 
 +
 
 +
== Generators and List Comprehensions ==
 
Python's generators and list comprehensions have been thought to be two of the most difficult concepts to replicate in other languages.  For the D solution to the problem solved by Python generators, see D Ranges and lazy evaluation.  For list comprehensions, see UFCS.
 
Python's generators and list comprehensions have been thought to be two of the most difficult concepts to replicate in other languages.  For the D solution to the problem solved by Python generators, see D Ranges and lazy evaluation.  For list comprehensions, see UFCS.
  
Line 41: Line 78:
 
*[http://www.informit.com/articles/printerfriendly/1407357 Theoretical article on ranges as a development of the iterator concept by the C++ guru, Dr Andrei Alexandrescu]
 
*[http://www.informit.com/articles/printerfriendly/1407357 Theoretical article on ranges as a development of the iterator concept by the C++ guru, Dr Andrei Alexandrescu]
 
*[http://ddili.org/ders/d.en/ranges.html Introduction to Ranges by Ali Çehreli - Part I]
 
*[http://ddili.org/ders/d.en/ranges.html Introduction to Ranges by Ali Çehreli - Part I]
*[http://ddili.org/ders/d.en/ranges.html Introduction to Ranges by Ali Çehreli - Part II]
+
*[http://ddili.org/ders/d.en/ranges_more.html Introduction to Ranges by Ali Çehreli - Part II]
 
*[http://dlang.org/d-array-article.html Introduction to D slices]
 
*[http://dlang.org/d-array-article.html Introduction to D slices]
  
Line 51: Line 88:
  
  
Parallel Programming
+
== Parallel Programming ==
--------------------
 
 
Parallel programming has become increasingly in focus as we approach the beginning of the end of the [http://www.gotw.ca/publications/concurrency-ddj.htm free lunch from Moore's Law].  D makes multiprocessing and threading as simple as possible, but not simpler:
 
Parallel programming has become increasingly in focus as we approach the beginning of the end of the [http://www.gotw.ca/publications/concurrency-ddj.htm free lunch from Moore's Law].  D makes multiprocessing and threading as simple as possible, but not simpler:
  
 
*[http://www.informit.com/articles/article.aspx?p=1609144 Free chapter from Dr Andrei Alexandrescu's book on parallelism in D]
 
*[http://www.informit.com/articles/article.aspx?p=1609144 Free chapter from Dr Andrei Alexandrescu's book on parallelism in D]
*[http://ddili.org/ders/d.en/parallelism.html Ali Cehreli on parallelism]
+
*[http://ddili.org/ders/d.en/parallelism.html Ali Çehreli on parallelism]
*[http://ddili.org/ders/d.en/concurrency.html Ali Cehreli on message-passing concurrency]
+
*[http://ddili.org/ders/d.en/concurrency.html Ali Çehreli on message-passing concurrency]
*[http://ddili.org/ders/d.en/concurrency_shared.html Ali Cehreli on data-sharing concurrency]
+
*[http://ddili.org/ders/d.en/concurrency_shared.html Ali Çehreli on data-sharing concurrency]
 +
*[http://ddili.org/ders/d.en/fibers.html Ali Çehreli on fibers (a topic actually on multitasking, not parallelism)]
 
*[http://dlang.org/library/std/parallelism.html std.parallelism]
 
*[http://dlang.org/library/std/parallelism.html std.parallelism]
  
 +
Simple example from std.parallelism documentation
 +
<syntaxhighlight lang="D">
 +
import std.algorithm, std.parallelism, std.range;
  
Interfacing D with an existing codebase
+
void main() {
---------------------------------------
+
    // Parallel reduce can be combined with
*[http://dlang.org/interfaceToC.html C interfacing is simple and complete]
+
    // std.algorithm.map to interesting effect.
 +
    // The following example (thanks to Russel Winder)
 +
    // calculates pi by quadrature  using
 +
    // std.algorithm.map and TaskPool.reduce.
 +
    // getTerm is evaluated in parallel as needed by
 +
    // TaskPool.reduce.
 +
    //
 +
    // Timings on an Athlon 64 X2 dual core machine:
 +
    //
 +
    // TaskPool.reduce:      12.170 s
 +
    // std.algorithm.reduce:  24.065 s
 +
 
 +
    immutable n = 1_000_000_000;
 +
    immutable delta = 1.0 / n;
 +
 
 +
    real getTerm(int i)
 +
    {
 +
        immutable x = ( i - 0.5 ) * delta;
 +
        return delta / ( 1.0 + x * x ) ;
 +
    }
 +
 
 +
    immutable pi = 4.0 * taskPool.reduce!"a + b"(
 +
        std.algorithm.map!getTerm(iota(n))
 +
    );
 +
}
 +
</syntaxhighlight>
 +
 
 +
== Interfacing D with an existing codebase ==
 +
*[http://dlang.org/interfaceToC.html C interfacing is simple and complete - D can call C, and C can call D]
 
*[http://dlang.org/cpp_interface.html C++ interfacing is a key priority of the D core team but much can already be done - in fact more than is described here]
 
*[http://dlang.org/cpp_interface.html C++ interfacing is a key priority of the D core team but much can already be done - in fact more than is described here]
 
*[https://github.com/ariovistus/pyd PyD] creates seamless interoperation between D and CPython, including for numpy arrays.  [http://www.youtube.com/watch?v=y-k7GyOcs3o "It just works".]  Make sure you visit the github code, and not the old version up at bitbucket.
 
*[https://github.com/ariovistus/pyd PyD] creates seamless interoperation between D and CPython, including for numpy arrays.  [http://www.youtube.com/watch?v=y-k7GyOcs3o "It just works".]  Make sure you visit the github code, and not the old version up at bitbucket.
 
*Other options are to use cython wrappings to connect to D, or to write in D directly to the Python API.  There are examples of this in the PyD examples directory within PyD
 
*Other options are to use cython wrappings to connect to D, or to write in D directly to the Python API.  There are examples of this in the PyD examples directory within PyD
 +
*[https://github.com/JakobOvrum/LuaD LuaD] creates a simple interface between D and Lua
  
 +
D calling embedded Python
 +
<syntaxhighlight lang="D">
 +
void main() {
 +
    auto context = new InterpContext();
 +
    context.a = 2;
 +
    context.py_stmts("print ('1 + %s' % a)");
 +
}
 +
</syntaxhighlight>
  
Web development, concurrency and JSON/BSON/XML
+
Lua D example
----------------------------------------------
+
<syntaxhighlight lang="D">
 +
import luad.all;
 +
 
 +
void main()
 +
{
 +
    auto lua = new LuaState;
 +
    lua.openLibs();
 +
 
 +
    auto print = lua.get!LuaFunction("print");
 +
    print("hello, world!");
 +
}
 +
</syntaxhighlight>
 +
 
 +
== Web development, concurrency and JSON/BSON/XML ==
 
Phobos includes bindings to the widely-used external curl library within Phobos (std.net.curl) and does include provision within std.csv, std.json and std.xml for processing structured data.  The JSON and XML implementations in Phobos could be better, and many people choose to use an external library.  The most popular solution for this is [http://vibed.org Vibe D], and this comes with a useful framework for web development, networking, fiber-based concurrency, JSON and BSON.  One can write fiber-oriented code without having to deal with callbacks.  (See [https://www.youtube.com/watch?v=Zs8O7MVmlfw CyberShadow's presentation] at Dconf 2013 for an excellent review of the differences).
 
Phobos includes bindings to the widely-used external curl library within Phobos (std.net.curl) and does include provision within std.csv, std.json and std.xml for processing structured data.  The JSON and XML implementations in Phobos could be better, and many people choose to use an external library.  The most popular solution for this is [http://vibed.org Vibe D], and this comes with a useful framework for web development, networking, fiber-based concurrency, JSON and BSON.  One can write fiber-oriented code without having to deal with callbacks.  (See [https://www.youtube.com/watch?v=Zs8O7MVmlfw CyberShadow's presentation] at Dconf 2013 for an excellent review of the differences).
  
Line 80: Line 170:
 
Other general solutions include those available in [https://github.com/adamdruppe/arsd Adam Ruppe's ARSD] micro-framework, and CyberShadow's [https://github.com/CyberShadow/ae AE] library.  See the forums for some further possibilities on the JSON and XML front.  D's slices facilitate fast and efficient parsing, and as of some time back, the XML parser in the Tango library was possibly the fastest in the world.
 
Other general solutions include those available in [https://github.com/adamdruppe/arsd Adam Ruppe's ARSD] micro-framework, and CyberShadow's [https://github.com/CyberShadow/ae AE] library.  See the forums for some further possibilities on the JSON and XML front.  D's slices facilitate fast and efficient parsing, and as of some time back, the XML parser in the Tango library was possibly the fastest in the world.
  
 +
Simple HTTP server
 +
<syntaxhighlight lang="D">
 +
import vibe.d;
 +
 +
shared static this()
 +
{
 +
auto settings = new HTTPServerSettings;
 +
settings.port = 8080;
 +
 +
listenHTTP(settings, &handleRequest);
 +
}
 +
 +
void handleRequest(HTTPServerRequest req,
 +
                  HTTPServerResponse res)
 +
{
 +
if (req.path == "/")
 +
res.writeBody("Hello, World!", "text/plain");
 +
}
 +
</syntaxhighlight>
 +
Declaring a REST interface
 +
<syntaxhighlight lang="D">
 +
interface IExampleAPI
 +
{
 +
    // Matches "GET /"
 +
    string getIndex();
 +
    // Matches "GET /data"
 +
    @property string data();
 +
    // Matches "PUT /data"
 +
    @property void data(string info);
 +
    // Matches "POST /sum"
 +
    // or "GET /sum?a=...&b=..."
 +
    int postSum(int a, int b);
 +
    // Matches "GET /item/<category>/<item>"
 +
    @path("item/:category/:item")
 +
    int getItem(string _category, int _item);
 +
}
 +
</syntaxhighlight>
 +
 +
 +
 +
REST Client
 +
<syntaxhighlight lang="D">
 +
class Example : IExampleAPI
 +
{
 +
    override:
 +
        string getIndex() { return "Index!"; }
 +
        string _data;
 +
        @property string data() { return _data; }
 +
        @property void data(string v) { _data=v; }
 +
        int postSum(int a, int b) {
 +
            return a + b;
 +
        }
 +
      int getItem(string _category,string _item) {
 +
          // ...
 +
      }
 +
}
 +
 +
void main(string[] args)
 +
{
 +
    auto api = new RestInterfaceClient!IExampleAPI("http://localhost/");
 +
    auto index = api.getIndex();
 +
    api.data ="My data";
 +
    assert(api.data =="My data");
 +
    assert(api.postSum(2, 3) == 5);
 +
}
 +
</syntaxhighlight>
 +
REST Server
 +
<syntaxhighlight lang="D">
 +
class Example : IExampleAPI
 +
{
 +
    override:
 +
        string getIndex() { return "Index!"; }
 +
        string _data;
 +
        @property string data() { return _data; }
 +
        @property void data(string v) { _data=v; }
 +
        int postSum(int a, int b) {
 +
          return a + b;
 +
      }
 +
      int getItem(string _category,string _item) {
 +
          // ...
 +
      }
 +
}
 +
 +
shared static this() // vibed replacement for main
 +
{
 +
    auto routes = new URLRouter;
 +
    registerRestInterface!IExampleAPI(routes, new Example(), "/");
 +
    auto settings = new HTTPServerSettings;
 +
    settings.port = 8080;
 +
    listenHTTP(settings, routes);
 +
}
 +
</syntaxhighlight>
  
Email
+
*[https://d.readthedocs.org/en/latest/examples.html#web-application Simple web application]
-----
 
The standard library has SMTP support in [http://dlang.org/phobos/std_net_curl#.SMTP std.net.curl.SMTP].
 
  
Some options in ARSD and AE Utils.  No IMAP as yet, but bindings are under development.
 
  
 +
== Email ==
 +
There is support for sending emails by SMTP in the standard library under [http://dlang.org/phobos/std_net_curl#.SMTP std.net.curl.SMTP] in vibe.d, [https://github.com/adamdruppe/arsd arsd] and [https://github.com/CyberShadow/ae AE Utils]. 
  
Numerical computing
+
Vibe.d Send Email Example
-------------------
+
<syntaxhighlight lang="D">
 +
import vibe.core.log;
 +
import vibe.mail.smtp;
 +
 
 +
 
 +
void main()
 +
{
 +
auto settings = new SMTPClientSettings("smtp.example.com", 25);
 +
settings.connectionType = SMTPConnectionType.startTLS;
 +
settings.authType = SMTPAuthType.plain;
 +
settings.username = "username";
 +
settings.password = "secret";
 +
 
 +
auto mail = new Mail;
 +
mail.headers["From"] = "<user@isp.com>";
 +
mail.headers["To"] = "<recipient@domain.com>";
 +
mail.headers["Subject"] = "Testmail";
 +
mail.bodyText = "Hello, World!";
 +
 +
logInfo("Sending mail...");
 +
sendMail(settings, mail);
 +
logInfo("done.");
 +
}
 +
</syntaxhighlight>
 +
 
 +
For MIME support see Arsd and AE Utils (under code.dlang.org there are bindings for gmime).  No IMAP as yet, but bindings are under development.
 +
 
 +
 
 +
== Numerical computing ==
 
*[http://dlang.org/d-floating-point.html D Floating Point Features]
 
*[http://dlang.org/d-floating-point.html D Floating Point Features]
 
*[http://dlang.org/phobos/std_bigint.html arbitrary precision - 'bignum' - arithmetic]
 
*[http://dlang.org/phobos/std_bigint.html arbitrary precision - 'bignum' - arithmetic]
Line 97: Line 306:
  
  
Libraries - what is the D equivalent of pypi and pip?
+
== Libraries - what is the D equivalent of pypi and pip ==
------------------------------------------------------
+
See [http://code.dlang.org code.dlang.org] and the [[DUB | DUB package manager]] .
See [http://code.dlang.org code.dlang.org] and the DUB package manager.
+
 
 +
 
 +
== Scripting ==
 +
Since D provides type inference, high-level constructs, and fast compile-time it is a great language for writing [https://github.com/Laeeth/tools/blob/master/FetchDlang/fetchdlang.d scripts]. The first line of the file is ignored if it begins with #! - by combining this with [http://dlang.org/rdmd.html rdmd] which handles dependency resolution, D becomes a leader in machine-code scripting language.
  
 +
Reading comma separated / CSV text - example from std.csv
 +
<syntaxhighlight lang="D">
 +
auto text = "Name,Occupation,Salary\r"
 +
    "Joe,Carpenter,300000\nFred,Blacksmith,400000\r\n";
  
Scripting
+
foreach(record; csvReader!(string[string])
---------
+
        (text, null))
Since D provides type inference, high-level constructs, and fast compile-time it is a great language for writing scripts. The first line of the file is ignored if it begins with #! - by combining this with [http://dlang.org/rdmd.html rdmd] which handles dependency resolution, D becomes a leader in machine-code scripting language.
+
{
 +
    writefln("%s works as a %s and earns $%s per year.",
 +
            record["Name"], record["Occupation"],
 +
            record["Salary"]);
 +
}
 +
</syntaxhighlight>
  
 +
Generating optimized machine code at compile time for compiled regex pattern matching
 +
<syntaxhighlight lang="D">
 +
  string phone = "+31 650 903 7158";
 +
  auto phoneReg = ctRegex!r"^\+([1-9][0-9]*) [0-9 ]*$";
 +
  auto m = match(phone, phoneReg);
 +
  assert(m);
 +
  assert(m.captures[0] == "+31 650 903 7158");
 +
  assert(m.captures[1] == "31");
 +
</syntaxhighlight>
  
Next Steps
+
== Next Steps ==
----------
 
 
* [http://dlang.org/download.html Download] and install D for your platform
 
* [http://dlang.org/download.html Download] and install D for your platform
 
* Play with the D REPL or [http://dpaste.dzfl.pl/ Pastebin]
 
* Play with the D REPL or [http://dpaste.dzfl.pl/ Pastebin]
* Consider purchasing Dr Andrei Alexandrescu's [http://www.amazon.com/D-Programming-Language-Andrei-Alexandrescu/dp/0321635361 The D Programming Language], [http://www.amazon.com/dp/1783287217/?tag=packtpubli-20 Adam Ruppe's D Cookbook], and reading the free book [http://ddili.org/ders/d.en/index.html 'Programming in D' by Ali Çehreli]
+
* Consider purchasing  
 +
**[http://www.amazon.com/D-Programming-Language-Andrei-Alexandrescu/dp/0321635361 ''The D Programming Language''] by Andrei Alexandrescu
 +
**[http://www.amazon.com/dp/1783287217/?tag=packtpubli-20 ''D Cookbook''] by Adam Ruppe
 +
**[https://smile.amazon.com/Programming-Tutorial-Reference-Ali-Cehreli-ebook/dp/B019AQNQ96/ref=sr_1_1 ''Programming in D: Tutorial and Reference''] by Ali Çehreli [http://ddili.org/ders/d.en/index.html (free web version)]
 +
**[https://smile.amazon.com/Learning-D-Michael-Parker-ebook/dp/B010BEEIIE/ref=sr_1_1 ''Learning D''] by Michael Parker
 +
**[https://teachsector.com/dforpython/ ''Programming Language D, the Best Programming Language, for Former Python Developers''] course by Victor Porton
 
* Read the D solutions to [http://rosettacode.org/wiki/Category:D Rosetta Code] problems
 
* Read the D solutions to [http://rosettacode.org/wiki/Category:D Rosetta Code] problems
* Take a look at the [http://wiki.dlang.org/Videos D language videos]
+
* Take a look at the [[Videos | D language videos]]
 
* Subscribe to [http://arsdnet.net/this-week-in-d/ This Week in D]
 
* Subscribe to [http://arsdnet.net/this-week-in-d/ This Week in D]
* Attend a meeting of a [http://wiki.dlang.org/D_User_Groups D User Group] near you
+
* Attend a meeting of a [[D_User_Groups |D User Group]] near you
 
* Follow D on [http://www.linkedin.com/groups/D-Developer-Network-3923820 LinkedIn], [https://plus.google.com/communities/100033468228217743303 Google+], [https://www.facebook.com/dlang.org Facebook], [https://www.xing.com/net/dlang Xing], [http://www.reddit.com/r/d_language/ Reddit], [https://www.quora.com/D-programming-language Quora]
 
* Follow D on [http://www.linkedin.com/groups/D-Developer-Network-3923820 LinkedIn], [https://plus.google.com/communities/100033468228217743303 Google+], [https://www.facebook.com/dlang.org Facebook], [https://www.xing.com/net/dlang Xing], [http://www.reddit.com/r/d_language/ Reddit], [https://www.quora.com/D-programming-language Quora]
 
* Follow some [http://planet.dsource.org/ D blogs]
 
* Follow some [http://planet.dsource.org/ D blogs]

Latest revision as of 10:25, 25 March 2023

51o29P-BnzL.jpg

(Non-accredicted) Masters D degree: D, the Best Programming Language, for Former Python Developers
Victor Porton
19 May 2021

"High Master" diploma of D development. No prerequisites (except of Python knowledge).

D programming language or DLang for Python programmers: rapid software development of high performance software and a reliable software technology. “There is no best programming language”, they say. There is, for most applications it is D. The course starts from a comparison of D to Python. Then it explains D starting from simple features (types, variables, functions, expressions, statements) up to advanced ones (object oriented programming, templates, mixins, contract programming, overloading of operators, etc.)

Currently we have only text lectures and exams, but addition of video lectures is planned.

"Often, programming teams will resort to a hybrid approach, where they will mix Python and C++, trying to get the productivity of Python and the performance of C++. The frequency of this approach indicates that there is a large unmet need in the programming language department.

D intends to fill that need. It combines the ability to do low-level manipulation of the machine with the latest technologies in building reliable, maintainable, portable, high-level code. D has moved well ahead of any other language in its abilities to support and integrate multiple paradigms like imperative, OOP, and generic programming"

-- Walter Bright


This section is under development - feel free to suggest improvements or additions.


D is Like Native Python

  • D for the Win A former Python programmer explores the benefits from moving to D
  • A hedge fund quant finds that Python begins to choke on the data volumes requiring processing, but that D can cope and is productive.


AdRoll is known for their use of Python elsewhere, but their data scientists use D. According to Andrew Pascoe, senior data scientist at AdRoll, "One of the clearest advantages of using D compared to other typical data science workflows is that it compiles down into machine code. Without an interpreter or virtual machine layer, we can rip through data significantly faster than other tools like a Java hadoop framework, R, or python would allow. But D’s compiler is fast enough that in many cases it can be run as if it were a scripting language....The key thing here that separates D from other efficient languages like the oft-suggested C or C++ is that D frees you to program in the style you feel most comfortable with at the given time". He says that they have found that they "can rapidly prototype new infrastructure and analysis tasks, and when efficiency becomes a core concern, we have the ability to refactor that same code base to squeeze as much performance out as possible".


IPython Notebook / Jupyter

An early-stage extension to Python exists to allow writing D extensions inline within an ipython/Jupyter notebook


Generators and List Comprehensions

Python's generators and list comprehensions have been thought to be two of the most difficult concepts to replicate in other languages. For the D solution to the problem solved by Python generators, see D Ranges and lazy evaluation. For list comprehensions, see UFCS.


Parallel Programming

Parallel programming has become increasingly in focus as we approach the beginning of the end of the free lunch from Moore's Law. D makes multiprocessing and threading as simple as possible, but not simpler:

Simple example from std.parallelism documentation

import std.algorithm, std.parallelism, std.range;

void main() {
    // Parallel reduce can be combined with
    // std.algorithm.map to interesting effect.
    // The following example (thanks to Russel Winder)
    // calculates pi by quadrature  using
    // std.algorithm.map and TaskPool.reduce.
    // getTerm is evaluated in parallel as needed by
    // TaskPool.reduce.
    //
    // Timings on an Athlon 64 X2 dual core machine:
    //
    // TaskPool.reduce:       12.170 s
    // std.algorithm.reduce:  24.065 s

    immutable n = 1_000_000_000;
    immutable delta = 1.0 / n;

    real getTerm(int i)
    {
        immutable x = ( i - 0.5 ) * delta;
        return delta / ( 1.0 + x * x ) ;
    }

    immutable pi = 4.0 * taskPool.reduce!"a + b"(
        std.algorithm.map!getTerm(iota(n))
    );
}

Interfacing D with an existing codebase

D calling embedded Python

 
void main() {
    auto context = new InterpContext();
    context.a = 2;
    context.py_stmts("print ('1 + %s' % a)");
}

Lua D example

import luad.all;

void main()
{
    auto lua = new LuaState;
    lua.openLibs();

    auto print = lua.get!LuaFunction("print");
    print("hello, world!");
}

Web development, concurrency and JSON/BSON/XML

Phobos includes bindings to the widely-used external curl library within Phobos (std.net.curl) and does include provision within std.csv, std.json and std.xml for processing structured data. The JSON and XML implementations in Phobos could be better, and many people choose to use an external library. The most popular solution for this is Vibe D, and this comes with a useful framework for web development, networking, fiber-based concurrency, JSON and BSON. One can write fiber-oriented code without having to deal with callbacks. (See CyberShadow's presentation at Dconf 2013 for an excellent review of the differences).

Atila Neaves has done one benchmark study on Vibed vs Go vc C vs Erlang in a MQTT broker implementation

Officially vibed is in beta, but the author seems to have high standards, and for many purposes you may find that this is good enough to be production-ready. (Of course, caveat emptor, it goes without saying).

Other general solutions include those available in Adam Ruppe's ARSD micro-framework, and CyberShadow's AE library. See the forums for some further possibilities on the JSON and XML front. D's slices facilitate fast and efficient parsing, and as of some time back, the XML parser in the Tango library was possibly the fastest in the world.

Simple HTTP server

import vibe.d;

shared static this()
{
	auto settings = new HTTPServerSettings;
	settings.port = 8080;

	listenHTTP(settings, &handleRequest);
}

void handleRequest(HTTPServerRequest req,
                   HTTPServerResponse res)
{
	if (req.path == "/")
		res.writeBody("Hello, World!", "text/plain");
}

Declaring a REST interface

interface IExampleAPI
{
    // Matches "GET /"
    string getIndex();
    // Matches "GET /data"
    @property string data();
    // Matches "PUT /data"
    @property void data(string info);
    // Matches "POST /sum"
    // or "GET /sum?a=...&b=..."
    int postSum(int a, int b);
    // Matches "GET /item/<category>/<item>"
    @path("item/:category/:item")
    int getItem(string _category, int _item);
}


REST Client

class Example : IExampleAPI
{
    override:
        string getIndex() { return "Index!"; }
        string _data;
        @property string data() { return _data; }
        @property void data(string v) { _data=v; }
        int postSum(int a, int b) {
            return a + b;
        }
       int getItem(string _category,string _item) {
           // ...
      }
}

void main(string[] args)
{
    auto api = new RestInterfaceClient!IExampleAPI("http://localhost/");
    auto index = api.getIndex();
    api.data ="My data";
    assert(api.data =="My data");
    assert(api.postSum(2, 3) == 5);
}

REST Server

class Example : IExampleAPI
{
    override:
        string getIndex() { return "Index!"; }
        string _data;
        @property string data() { return _data; }
        @property void data(string v) { _data=v; }
        int postSum(int a, int b) {
           return a + b;
       }
      int getItem(string _category,string _item) {
          // ...
       }
}

shared static this() // vibed replacement for main
{
    auto routes = new URLRouter;
    registerRestInterface!IExampleAPI(routes, new Example(), "/");
    auto settings = new HTTPServerSettings;
    settings.port = 8080;
    listenHTTP(settings, routes);
}


Email

There is support for sending emails by SMTP in the standard library under std.net.curl.SMTP in vibe.d, arsd and AE Utils.

Vibe.d Send Email Example

import vibe.core.log;
import vibe.mail.smtp;


void main()
{
	auto settings = new SMTPClientSettings("smtp.example.com", 25);
	settings.connectionType = SMTPConnectionType.startTLS;
	settings.authType = SMTPAuthType.plain;
	settings.username = "username";
	settings.password = "secret";

	auto mail = new Mail;
	mail.headers["From"] = "<user@isp.com>";
	mail.headers["To"] = "<recipient@domain.com>";
	mail.headers["Subject"] = "Testmail";
	mail.bodyText = "Hello, World!";
	
	logInfo("Sending mail...");
	sendMail(settings, mail);
	logInfo("done.");
}

For MIME support see Arsd and AE Utils (under code.dlang.org there are bindings for gmime). No IMAP as yet, but bindings are under development.


Numerical computing


Libraries - what is the D equivalent of pypi and pip

See code.dlang.org and the DUB package manager .


Scripting

Since D provides type inference, high-level constructs, and fast compile-time it is a great language for writing scripts. The first line of the file is ignored if it begins with #! - by combining this with rdmd which handles dependency resolution, D becomes a leader in machine-code scripting language.

Reading comma separated / CSV text - example from std.csv

auto text = "Name,Occupation,Salary\r"
    "Joe,Carpenter,300000\nFred,Blacksmith,400000\r\n";

foreach(record; csvReader!(string[string])
        (text, null))
{
    writefln("%s works as a %s and earns $%s per year.",
             record["Name"], record["Occupation"],
             record["Salary"]);
}

Generating optimized machine code at compile time for compiled regex pattern matching

  string phone = "+31 650 903 7158";
  auto phoneReg = ctRegex!r"^\+([1-9][0-9]*) [0-9 ]*$";
  auto m = match(phone, phoneReg);
  assert(m);
  assert(m.captures[0] == "+31 650 903 7158");
  assert(m.captures[1] == "31");

Next Steps