translated

This commit is contained in:
geekpi 2021-02-09 10:39:56 +08:00
parent 68aa5983a3
commit b1747f5822
2 changed files with 208 additions and 26 deletions

View File

@ -7,25 +7,24 @@
[#]: via: (https://opensource.com/article/21/1/d-scripting)
[#]: author: (Lawrence Aberba https://opensource.com/users/aberba)
Why I use the D programming language for scripting
我为什么要用 D 语言写脚本?
======
The D programming language is best known as a system programming
language, but it's also a great option for scripting.
D 语言是以系统编程语言而闻名,但它也是编写脚本的一个很好的选择。
![Business woman on laptop sitting in front of window][1]
The D programming language is often advertised as a system programming language due to its static typing and metaprogramming capabilities. However, it's also a very productive scripting language.
D 语言由于其静态类型和元编程能力,经常被宣传为系统编程语言。然而,它也是一种非常高效的脚本语言。
Python is commonly chosen for scripting due to its flexibility for automating tasks and quickly prototyping ideas. This makes Python very appealing to sysadmins, [managers][2], and developers in general for automating recurring tasks that they might otherwise have to do manually.
由于 Python 在自动化任务和快速实现原型想法方面的灵活性,它通常被选为脚本语言。这使得 Python 对系统管理员、[管理者][2]和一般的开发人员非常有吸引力,因为它可以自动完成他们可能不得不手动完成的重复性任务。
It is reasonable to expect any other script-writing language to have these Python traits and capabilities. Here are two reasons why I believe D is a good option.
我们有理由期待任何其他的脚本编写语言具有 Python 的这些特性和能力。以下是我认为 D 是一个不错的选择的两个原因。
### 1\. D is easy to read and write
### 1\. D 很容易读和写
As a C-like language, D should be familiar to most programmers. Anyone who uses JavaScript, Java, PHP, or Python will know their way around D.
作为一种类似于 C 的语言D 应该是大多数程序员所熟悉的。任何使用 JavaScript、Java、PHP 或 Python 的人对 D 语言都很容易上手。
If you don't already have D installed, [install a D compiler][3] so that you can [run the D code][4] in this article. You may also use the [online D editor][5].
如果你还没有安装 D请[安装 D 编译器][3],这样你就可以[运行本文中的 D 代码][4]。你也可以使用[在线 D 编辑器][5]。
Here is an example of D code that reads words from a file named `words.txt` and prints them on the command line:
下面是一个 D 代码的例子,它从一个名为 `words.txt` 的文件中读取单词,并在命令行中打印出来:
```
@ -35,7 +34,7 @@ is
cool
```
Write the script in D:
用 D 语言写脚本:
```
@ -58,21 +57,20 @@ void main(){
}
```
This code is prefixed with a [shebang][6] that will run the code using [rdmd][7], a tool that comes with the D compiler to compile and run code. Assuming you are running Unix or Linux, before you can run this script, you must make it executable by using the `chmod` command:
这段代码的前缀是 [shebang][6],它将使用 [rdmd][7] 来运行这段代码rdmd 是 D 编译器自带的编译和运行代码的工具。假设你运行的是 Unix 或 Linux在运行这个脚本之前你必须使用` chmod` 命令使其可执行:
```
`chmod u+x print_words.d`
```
Now that the script is executable, you can run it:
现在脚本是可执行的,你可以运行它:
```
`./print_words.d`
```
This should print the following on your command line:
这将在你的命令行中打印以下内容:
```
@ -82,9 +80,9 @@ is
cool
```
Congratulations! You've written your first D script. You can see how D enables you to chain functions in sequence to make reading the code feel natural, similar to how you think about problems in your mind. This [feature makes D my favorite programming language][8].
恭喜你,你写了第一个 D 语言脚本。你可以看到 D 是如何让你按顺序链式调用函数,这让阅读代码的感觉很自然,类似于你在头脑中思考问题的方式。这个[功能让 D 成为我最喜欢的编程语言][8]。
Try writing another script: A nonprofit manager has a text file of donations with each amount on separate lines. The manager wants to sum the first 10 donations and print the amounts:
试着再写一个脚本:一个非营利组织的管理员有一个捐款的文本文件,每笔金额都是单独的一行。管理员想把前 10 笔捐款相加,然后打印出金额:
```
@ -124,13 +122,14 @@ void main()
}
```
The `!` operator used with `each` is the syntax of a [template argument][9].
`each` 一起使用的 `!` 操作符是[模板参数][9]的语法。
### 2\. D is great for quick prototyping
### 2\. D 是快速原型设计的好帮手
D is flexible for hammering code together really quickly and making it work. Its standard library is rich with utility functions for performing common tasks, such as manipulating data (JSON, CSV, text, etc.). It also comes with a rich set of generic algorithms for iterating, searching, comparing, and mutating data. These cleverly crafted algorithms are oriented towards processing sequences by defining generic [range-based interfaces][10].
D 是灵活的它可以快速地将代码敲打在一起并使其发挥作用。它的标准库中包含了丰富的实用函数用于执行常见的任务如操作数据JSON、CSV、文本等。它还带有一套丰富的通用算法用于迭代、搜索、比较和 mutate 数据。这些巧妙的算法通过定义通用的[基于范围的接口][10]面向处理序列。
The script above shows how chaining functions in D provides a gist of sequential processing and manipulating data. Another appeal of D is its growing ecosystem of third-party packages for performing common tasks. An example is how easy it is to build a simple web server using the [Vibe.d][11] web framework. Here's an example:
上面的脚本显示了 D 中的链式调用函数如何提供顺序处理和操作数据的要领。D 的另一个吸引人的地方是它不断增长的第三方包的生态系统,用于执行普通任务。一个例子是,使用 [Vibe.d][11] web 框架构建一个简单的 web 服务器很容易。下面是一个例子:
```
@ -148,13 +147,13 @@ void main()
}
```
This uses the official D package manager, [Dub][12], to fetch the vibe.d web framework from the [D package repository][13]. Dub takes care of downloading the Vibe.d package, then compiling and spinning up a web server on localhost port 8080.
它使用官方的 D 软件包管理器 [Dub][12],从 [D 软件包仓库][13]中获取 vibe.d web 框架。Dub 负责下载 Vibe.d 包,然后在本地主机 8080 端口上编译并启动一个 web 服务器。
### Give D a try
### 尝试一下 D 语言
These are only a couple of reasons why you might want to use D for writing scripts.
这些只是你可能想用 D 来写脚本的几个原因。
D is a great language for development. It's easy to install from the D download page, so download the compiler, take a look at the examples, and experience D for yourself.
D 是一种非常适合开发的语言。你可以很容易从 D 下载页面安装,因此下载编译器,看看例子,并亲自体验 D 语言。
--------------------------------------------------------------------------------
@ -162,7 +161,7 @@ via: https://opensource.com/article/21/1/d-scripting
作者:[Lawrence Aberba][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出

View File

@ -0,0 +1,183 @@
[#]: collector: (lujun9972)
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Why I use the D programming language for scripting)
[#]: via: (https://opensource.com/article/21/1/d-scripting)
[#]: author: (Lawrence Aberba https://opensource.com/users/aberba)
我为什么要用 D 语言写脚本?
======
D 语言是以系统编程语言而闻名,但它也是编写脚本的一个很好的选择。
![Business woman on laptop sitting in front of window][1]
D 语言由于其静态类型和元编程能力,经常被宣传为系统编程语言。然而,它也是一种非常高效的脚本语言。
由于 Python 在自动化任务和快速实现原型想法方面的灵活性,它通常被选为脚本语言。这使得 Python 对系统管理员、[管理者][2]和一般的开发人员非常有吸引力,因为它可以自动完成他们可能不得不手动完成的重复性任务。
我们有理由期待任何其他的脚本编写语言具有 Python 的这些特性和能力。以下是我认为 D 是一个不错的选择的两个原因。
### 1\. D 很容易读和写
作为一种类似于 C 的语言D 应该是大多数程序员所熟悉的。任何使用 JavaScript、Java、PHP 或 Python 的人对 D 语言都很容易上手。
如果你还没有安装 D请[安装 D 编译器][3],这样你就可以[运行本文中的 D 代码][4]。你也可以使用[在线 D 编辑器][5]。
下面是一个 D 代码的例子,它从一个名为 `words.txt` 的文件中读取单词,并在命令行中打印出来:
```
open
source
is
cool
```
用 D 语言写脚本:
```
// file print_words.d
#!/usr/bin/env rdmd
// import the D standard library
import std;
void main(){
    // open the file
     File("./words.txt")
         //iterate by line
        .byLine
        // print each number
        .each!writeln;
}
```
这段代码的前缀是 [shebang][6],它将使用 [rdmd][7] 来运行这段代码rdmd 是 D 编译器自带的编译和运行代码的工具。假设你运行的是 Unix 或 Linux在运行这个脚本之前你必须使用` chmod` 命令使其可执行:
```
`chmod u+x print_words.d`
```
现在脚本是可执行的,你可以运行它:
```
`./print_words.d`
```
这将在你的命令行中打印以下内容:
```
open
source
is
cool
```
恭喜你,你写了第一个 D 语言脚本。你可以看到 D 是如何让你按顺序链式调用函数,这让阅读代码的感觉很自然,类似于你在头脑中思考问题的方式。这个[功能让 D 成为我最喜欢的编程语言][8]。
试着再写一个脚本:一个非营利组织的管理员有一个捐款的文本文件,每笔金额都是单独的一行。管理员想把前 10 笔捐款相加,然后打印出金额:
```
// file sum_donations.d
#!/usr/bin/env rdmd
import std;
void main()
{
    double total = 0;
    // open the file
    File("monies.txt")
         // iterate by line
        .byLine
         // pick first 10 lines
        .take(10)
        // remove new line characters (\n)
        .map!(strip)
         // convert each to double
        .map!(to!double)
        // add element to total
        .tee!((x) { total += x; })
        // print each number
        .each!writeln;
    // print total
    writeln("total: ", total);
}
```
`each` 一起使用的 `!` 操作符是[模板参数][9]的语法。
### 2\. D 是快速原型设计的好帮手
D 是灵活的它可以快速地将代码敲打在一起并使其发挥作用。它的标准库中包含了丰富的实用函数用于执行常见的任务如操作数据JSON、CSV、文本等。它还带有一套丰富的通用算法用于迭代、搜索、比较和 mutate 数据。这些巧妙的算法通过定义通用的[基于范围的接口][10]面向处理序列。
上面的脚本显示了 D 中的链式调用函数如何提供顺序处理和操作数据的要领。D 的另一个吸引人的地方是它不断增长的第三方包的生态系统,用于执行普通任务。一个例子是,使用 [Vibe.d][11] web 框架构建一个简单的 web 服务器很容易。下面是一个例子:
```
#!/usr/bin/env dub
/+ dub.sdl:
dependency "vibe-d" version="~>0.8.0"
+/
void main()
{
    import vibe.d;
    listenHTTP(":8080", (req, res) {
        res.writeBody("Hello, World: " ~ req.path);
    });
    runApplication();
}
```
它使用官方的 D 软件包管理器 [Dub][12],从 [D 软件包仓库][13]中获取 vibe.d web 框架。Dub 负责下载 Vibe.d 包,然后在本地主机 8080 端口上编译并启动一个 web 服务器。
### 尝试一下 D 语言
这些只是你可能想用 D 来写脚本的几个原因。
D 是一种非常适合开发的语言。你可以很容易从 D 下载页面安装,因此下载编译器,看看例子,并亲自体验 D 语言。
--------------------------------------------------------------------------------
via: https://opensource.com/article/21/1/d-scripting
作者:[Lawrence Aberba][a]
选题:[lujun9972][b]
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/aberba
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/lenovo-thinkpad-laptop-concentration-focus-windows-office.png?itok=-8E2ihcF (Woman using laptop concentrating)
[2]: https://opensource.com/article/20/3/automating-community-management-python
[3]: https://tour.dlang.org/tour/en/welcome/install-d-locally
[4]: https://tour.dlang.org/tour/en/welcome/run-d-program-locally
[5]: https://run.dlang.io/
[6]: https://en.wikipedia.org/wiki/Shebang_(Unix)
[7]: https://dlang.org/rdmd.html
[8]: https://opensource.com/article/20/7/d-programming
[9]: http://ddili.org/ders/d.en/templates.html
[10]: http://ddili.org/ders/d.en/ranges.html
[11]: https://vibed.org
[12]: https://dub.pm/getting_started
[13]: https://code.dlang.org