- D Cookbook
- Adam D. Ruppe
- 166字
- 2021-07-16 11:50:47
Introduction
This chapter goes into the details of an artefact central to the Phobos standard library with a little core language support: ranges. Ranges are user-defined objects used to build iterators over a collection of items. The collection may be pre-existing, such as an array, or it may be generated on the fly by the range object.
Ranges are defined in a way that they can be plugged together like building blocks with generic algorithms and other transformations. Command-line pipelines and range code can be very similar.
The Unix command-line command cat file.txt | sort | uniq
can be expressed similarly in D, using ranges from std.stdio
and std.algorithm
and a helper function from std.range
, as shown in the following code:
foreach(line; File("file.txt").byLine.map!(a=>a.idup).array.sort.uniq) writeln("Unique line: ", line);
Each range feeds into the next, building a system of generic building blocks that can be combined to perform a variety of tasks. In this chapter, we'll look at how to use ranges and how to create our own.
- 深入理解Android(卷I)
- Learning Selenium Testing Tools with Python
- Instant Apache Stanbol
- Xcode 7 Essentials(Second Edition)
- Animate CC二維動畫設計與制作(微課版)
- 微信小程序開發(fā)解析
- 新印象:解構UI界面設計
- 硬件產品設計與開發(fā):從原型到交付
- Redmine Cookbook
- Less Web Development Cookbook
- Instant AppFog
- C語言從入門到精通(第5版)
- Elasticsearch實戰(zhàn)(第2版)
- Mastering VMware vSphere Storage
- 測試架構師修煉之道:從測試工程師到測試架構師(第2版)