In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what are the reasons for writing scripts in D language". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it slowly to study and learn "what are the reasons for writing scripts in D language" together!
D is easy to read and write.
As a language similar to C, D should be familiar to most programmers. D is easy for anyone who uses JavaScript, Java, PHP or Python.
If you don't already have D installed, install the D compiler so you can run the D code in this article. You can also use the online D editor.
Here is an example of D-code that reads words from a file called words.txt and prints them on the command line:
opensourceiscool
Write scripts in D:
#!/ usr/bin/env rdmd// file print_words.d // import the D standard libraryimport std; void main(){ // open the file File("./ words.txt") //iterate by line .byLine // print each number .each! writeln;}
This code starts with an explanation that will run it using rdmd, which is a tool that comes with the D compiler to compile and run code. Assuming you are running Unix or Linux, before running this script, you must use the chmod command to make it executable:
chmod u+x print_words.d
Now that the script is executable, you can run it:
./ print_words.d
This will print the following on your command line:
opensourceiscool
Congratulations, you wrote your first D script. You can see how D lets you chain calls to functions in sequence, which makes reading code feel natural, similar to the way you think about problems in your head. This feature makes D my favorite programming language.
Try writing another script: The administrator of a nonprofit organization has a text file of donations, with a separate line for each amount. The administrator wants to add up the first 10 donations and print out the amount:
#!/ usr/bin/env rdmd// file sum_donations.d 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);}
Used with each! The operator is the syntax for template parameters.
D is a good helper for rapid prototyping.
D is flexible, it can quickly hammer code together and make it work. Its standard library contains a wealth of utility functions for performing common tasks such as manipulating data (JSON, CSV, text, etc.). It also comes with a rich set of general algorithms for iterating, searching, comparing, and mutate data. These clever algorithms process in sequence by defining generic scope-based interfaces.
The script above shows how the chained call function in D provides the essentials for sequential processing and manipulation of data. Another attraction 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 web framework. Here is an example:
#!/ 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();}
It uses the official D package manager Dub to get the vibe.d Web framework from the D package repository. Dub is responsible for downloading the Vibe.d package, compiling and starting a web server on port 8080 on the local host
Thank you for reading, the above is "what are the reasons for writing scripts in D language" content, after the study of this article, I believe that we have a deeper understanding of the reasons for writing scripts in D language, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.