Files
progcode/docs/rebuild-v2/raw/outbound/c11441b4e03c663d.txt

589 lines
20 KiB
Plaintext
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<head>
<link href="https://fonts.googleapis.com/css?family=Roboto+Slab:300,400" rel="stylesheet" type="text/css"/>
<link rel="stylesheet" href="/static/css/common.css"/>
<link rel="shortcut icon" href="/static/img/favicon.ico"/>
<link rel="stylesheet" type="text/css" href="/static/css/menu.css"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=0.1, maximum-scale=10.0"/>
<title>Ranges - Dlang Tour</title>
<link rel="stylesheet" href="/static/css/tour.css"/>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css"/>
<link rel="stylesheet" href="/static/lib/hotkeys.min.css"/>
<link rel="stylesheet" href="/static/lib/codemirror/lib/codemirror.css"/>
<link rel="stylesheet" href="/static/lib/codemirror/addon/lint/lint.css"/>
<link rel="stylesheet" href="/static/lib/codemirror/theme/elegant.css"/>
<link rel="stylesheet" href="/static/lib/grid12.min.css"/>
<link rel="stylesheet" href="/static/css/ansi.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
</head>
<body ng-app="DlangTourApp" class="ng-cloak">
<div id="top">
<div class="helper">
<div class="helper expand-container active">
<div class="logo">
<a href="https://dlang.org">
<img id="logo" alt="DLang Logo" src="/static/img/dlogo.svg"/>
</a>
<a href="../../../" id="title">
<span>DLang Tour</span>
</a>
</div>
<a href="#" title="Menu" class="hamburger expand-toggle">
<span>Menu</span>
</a>
<div id="cssmenu">
<ul>
<li class="expand-container">
<a class="expand-toggle" href="#">
<span>Welcome</span>
</a>
<ul class="expand-content">
<li class="">
<a href="/tour/en/welcome/welcome-to-d">
<span>Welcome to D</span>
</a>
</li>
<li class="">
<a href="/tour/en/welcome/languages">
<span>Read in your language</span>
</a>
</li>
<li class="">
<a href="/tour/en/welcome/install-d-locally">
<span>Install D locally</span>
</a>
</li>
<li class="">
<a href="/tour/en/welcome/run-d-program-locally">
<span>Run D program locally</span>
</a>
</li>
</ul>
</li>
<li class="expand-container active">
<a class="expand-toggle" href="#">
<span>D's Basics</span>
</a>
<ul class="expand-content">
<li class="active">
<a href="/tour/en/basics/imports-and-modules">
<span>Imports and modules</span>
</a>
</li>
<li class="active">
<a href="/tour/en/basics/basic-types">
<span>Basic types</span>
</a>
</li>
<li class="active">
<a href="/tour/en/basics/memory">
<span>Memory</span>
</a>
</li>
<li class="active">
<a href="/tour/en/basics/type-qualifiers">
<span>Mutability</span>
</a>
</li>
<li class="active">
<a href="/tour/en/basics/controlling-flow">
<span>Control flow</span>
</a>
</li>
<li class="active">
<a href="/tour/en/basics/functions">
<span>Functions</span>
</a>
</li>
<li class="active">
<a href="/tour/en/basics/structs">
<span>Structs</span>
</a>
</li>
<li class="active">
<a href="/tour/en/basics/arrays">
<span>Arrays</span>
</a>
</li>
<li class="active">
<a href="/tour/en/basics/slices">
<span>Slices</span>
</a>
</li>
<li class="active">
<a href="/tour/en/basics/alias-strings">
<span>Alias &amp; Strings</span>
</a>
</li>
<li class="active">
<a href="/tour/en/basics/loops">
<span>Loops</span>
</a>
</li>
<li class="active">
<a href="/tour/en/basics/foreach">
<span>Foreach</span>
</a>
</li>
<li class="active">
<a href="/tour/en/basics/ranges">
<span>Ranges</span>
</a>
</li>
<li class="active">
<a href="/tour/en/basics/associative-arrays">
<span>Associative Arrays</span>
</a>
</li>
<li class="active">
<a href="/tour/en/basics/classes">
<span>Classes</span>
</a>
</li>
<li class="active">
<a href="/tour/en/basics/interfaces">
<span>Interfaces</span>
</a>
</li>
<li class="active">
<a href="/tour/en/basics/templates">
<span>Templates</span>
</a>
</li>
<li class="active">
<a href="/tour/en/basics/delegates">
<span>Delegates</span>
</a>
</li>
<li class="active">
<a href="/tour/en/basics/exceptions">
<span>Exceptions</span>
</a>
</li>
<li class="active">
<a href="/tour/en/basics/further-reading">
<span>Further Reading</span>
</a>
</li>
</ul>
</li>
<li class="expand-container">
<a class="expand-toggle" href="#">
<span>D's Gems</span>
</a>
<ul class="expand-content">
<li class="">
<a href="/tour/en/gems/uniform-function-call-syntax-ufcs">
<span>Uniform Function Call Syntax (UFCS)</span>
</a>
</li>
<li class="">
<a href="/tour/en/gems/scope-guards">
<span>Scope guards</span>
</a>
</li>
<li class="">
<a href="/tour/en/gems/range-algorithms">
<span>Range algorithms</span>
</a>
</li>
<li class="">
<a href="/tour/en/gems/unittesting">
<span>Unit Testing</span>
</a>
</li>
<li class="">
<a href="/tour/en/gems/string-mixins">
<span>String Mixins</span>
</a>
</li>
<li class="">
<a href="/tour/en/gems/compile-time-function-evaluation-ctfe">
<span>Compile Time Function Evaluation (CTFE)</span>
</a>
</li>
<li class="">
<a href="/tour/en/gems/functional-programming">
<span>Functional programming</span>
</a>
</li>
<li class="">
<a href="/tour/en/gems/contract-programming">
<span>Contract programming</span>
</a>
</li>
<li class="">
<a href="/tour/en/gems/subtyping">
<span>Subtyping</span>
</a>
</li>
<li class="">
<a href="/tour/en/gems/attributes">
<span>Attributes</span>
</a>
</li>
<li class="">
<a href="/tour/en/gems/opdispatch-opapply">
<span>opDispatch &amp; opApply</span>
</a>
</li>
<li class="">
<a href="/tour/en/gems/documentation">
<span>Documentation</span>
</a>
</li>
<li class="">
<a href="/tour/en/gems/traits">
<span>Traits</span>
</a>
</li>
<li class="">
<a href="/tour/en/gems/template-meta-programming">
<span>Template meta programming</span>
</a>
</li>
<li class="">
<a href="/tour/en/gems/bit-manipulation">
<span>Bit manipulation</span>
</a>
</li>
<li class="">
<a href="/tour/en/gems/unicode">
<span>Unicode in D</span>
</a>
</li>
</ul>
</li>
<li class="expand-container">
<a class="expand-toggle" href="#">
<span>Multithreading</span>
</a>
<ul class="expand-content">
<li class="">
<a href="/tour/en/multithreading/thread-local-storage">
<span>Thread local storage</span>
</a>
</li>
<li class="">
<a href="/tour/en/multithreading/message-passing">
<span>Message Passing</span>
</a>
</li>
<li class="">
<a href="/tour/en/multithreading/synchronization-sharing">
<span>Synchronization &amp; Sharing</span>
</a>
</li>
<li class="">
<a href="/tour/en/multithreading/std-parallelism">
<span>std.parallelism</span>
</a>
</li>
<li class="">
<a href="/tour/en/multithreading/fibers">
<span>Fibers</span>
</a>
</li>
</ul>
</li>
<li class="expand-container">
<a class="expand-toggle" href="#">
<span>Vibe.d</span>
</a>
<ul class="expand-content">
<li class="">
<a href="/tour/en/vibed/vibe-d-web-framework">
<span>Vibe.d web framework</span>
</a>
</li>
<li class="">
<a href="/tour/en/vibed/basics-asynchronous-i-o">
<span>Basics &amp; Asynchronous I/O</span>
</a>
</li>
<li class="">
<a href="/tour/en/vibed/web-server">
<span>Web server</span>
</a>
</li>
<li class="">
<a href="/tour/en/vibed/diet-templates">
<span>DIET Templates</span>
</a>
</li>
<li class="">
<a href="/tour/en/vibed/json-rest-interface">
<span>JSON REST Interface</span>
</a>
</li>
<li class="">
<a href="/tour/en/vibed/database-connectivity">
<span>Database connectivity</span>
</a>
</li>
<li class="">
<a href="/tour/en/vibed/deploy-on-heroku">
<span>Deploy on Heroku</span>
</a>
</li>
</ul>
</li>
<li class="expand-container">
<a class="expand-toggle" href="#">
<span>D by Examples</span>
</a>
<ul class="expand-content">
<li class="">
<a href="/tour/en/byexample/code-generation-parser">
<span>Code generation (Parser)</span>
</a>
</li>
</ul>
</li>
<li class="expand-container">
<a class="expand-toggle" href="#">
<span>DUB packages</span>
</a>
<ul class="expand-content">
<li class="">
<a href="/tour/en/dub/mir-algorithm">
<span>Mir Algorithm</span>
</a>
</li>
<li class="">
<a href="/tour/en/dub/mir-random">
<span>Mir Random</span>
</a>
</li>
<li class="">
<a href="/tour/en/dub/mir">
<span>Mir</span>
</a>
</li>
<li class="">
<a href="/tour/en/dub/emsi_containers">
<span>EMSI Containers</span>
</a>
</li>
<li class="">
<a href="/tour/en/dub/vibe-d">
<span>Vibe-d</span>
</a>
</li>
<li class="">
<a href="/tour/en/dub/libdparse">
<span>libdparse</span>
</a>
</li>
<li class="">
<a href="/tour/en/dub/pegged">
<span>Pegged</span>
</a>
</li>
<li class="">
<a href="/tour/en/dub/lubeck">
<span>Lubeck</span>
</a>
</li>
</ul>
</li>
</ul>
</div>
<div id="github_avatar">
<a href="https://github.com/dlang-tour/core">
<i class="fa fa-github" aria-hidden="true"></i>
</a>
<a ng-href="https://github.com/dlang-tour/core/issues/new">
<i class="fa fa-question-circle" aria-hidden="true"></i>
</a>
</div>
</div>
</div>
</div>
<div class="row" ng-controller="DlangTourAppCtrl as ctrl" ng-init="initTour(&#39;en&#39;, &#39;dlang-tour/english&#39;, &#39;basics&#39;, &#39;ranges&#39;, true, &#39;/tour/en/basics/foreach&#39;, &#39;/tour/en/basics/associative-arrays&#39;)">
<div id="tour-content" ng-show="showContent" ng-class="{&#39;col-md-12&#39;: !showSourceCode, &#39;col-md-7 col-sm-12&#39;: showSourceCode}">
<div ng-hide="showProgramOutput">
<div class="content-command-box">
<button class="btn btn-default" ng-click="editOnGithub()">
<div class="fa fa-edit"></div>
</button>
<span class="hidden-xs hidden-sm">
<button class="btn btn-default btn-sm" ng-click="showSourceCode = true" ng-hide="showSourceCode">
<div class="fa fa-eye"></div>
</button>
</span>
</div>
<h1 id="ranges"> Ranges</h1>
<p>If a <code class="prettyprint">foreach</code> is encountered by the compiler
</p>
<pre class="prettyprint"><code>foreach (element; range)
{
  // Loop body...
}
</code></pre>
<p>it's internally rewritten similar to the following:
</p>
<pre class="prettyprint"><code>for (auto __rangeCopy = range;
    !__rangeCopy.empty;
    __rangeCopy.popFront())
{
   auto element = __rangeCopy.front;
   // Loop body...
}
</code></pre>
<p>Any object which fulfills the following interface is called a <strong>range</strong>
(or more specific <code class="prettyprint">InputRange</code>) and is thus a type that can be iterated over:
</p>
<pre class="prettyprint"><code>    interface InputRange(E)
{
bool empty();
E front();
void popFront();
}
</code></pre>
<p>Have a look at the example on the right to inspect the implementation and usage
of an input range closer.
</p>
<h2 id="laziness"> Laziness</h2>
<p>Ranges are <strong>lazy</strong>. They won't be evaluated until requested.
Hence, a range from an infinite range can be taken:
</p>
<pre class="prettyprint"><code>42.repeat.take(3).writeln; // [42, 42, 42]
</code></pre>
<h2 id="value-vs-reference-types"> Value vs. Reference types</h2>
<p>If the range object is a value type, then range will be copied and only the copy
will be consumed:
</p>
<pre class="prettyprint"><code>auto r = 5.iota;
r.drop(5).writeln; // []
r.writeln; // [0, 1, 2, 3, 4]
</code></pre>
<p>If the range object is a reference type (e.g. <code class="prettyprint">class</code> or <a href="https://dlang.org/phobos/std_range.html#refRange"><code class="prettyprint">std.range.refRange</code></a>),
then the range will be consumed and won't be reset:
</p>
<pre class="prettyprint"><code>auto r = 5.iota;
auto r2 = refRange(&amp;r);
r2.drop(5).writeln; // []
r2.writeln; // []
</code></pre>
<h3 id="copyable-inputranges-are-forwardranges"> Copyable <code class="prettyprint">InputRanges</code> are <code class="prettyprint">ForwardRanges</code></h3>
<p>Most of the ranges in the standard library are structs and so <code class="prettyprint">foreach</code>
iteration is usually non-destructive, though not guaranteed. If this
guarantee is important, a specialization of an <code class="prettyprint">InputRange</code> can be used—
<strong>forward</strong> ranges with a <code class="prettyprint">.save</code> method:
</p>
<pre class="prettyprint"><code>interface ForwardRange(E) : InputRange!E
{
typeof(this) save();
}
</code></pre>
<pre class="prettyprint"><code>// by value (Structs)
auto r = 5.iota;
auto r2 = refRange(&amp;r);
r2.save.drop(5).writeln; // []
r2.writeln; // [0, 1, 2, 3, 4]
</code></pre>
<h3 id="forwardranges-can-be-extended-to-bidirectional-ranges-random-access-ranges"> <code class="prettyprint">ForwardRanges</code> can be extended to Bidirectional ranges + random access ranges</h3>
<p>There are two extensions of the copyable <code class="prettyprint">ForwardRange</code>: (1) a bidirectional range
and (2) a random access range.
A bidirectional range allows iteration from the back:
</p>
<pre class="prettyprint"><code>interface BidirectionalRange(E) : ForwardRange!E
{
E back();
void popBack();
}
</code></pre>
<pre class="prettyprint"><code>5.iota.retro.writeln; // [4, 3, 2, 1, 0]
</code></pre>
<p>A random access range has a known <code class="prettyprint">length</code> and each element can be directly accessed.
</p>
<pre class="prettyprint"><code>interface RandomAccessRange(E) : ForwardRange!E
{
E opIndex(size_t i);
size_t length();
}
</code></pre>
<p>The best known random access range is D's array:
</p>
<pre class="prettyprint"><code>auto r = [4, 5, 6];
r[1].writeln; // 5
</code></pre>
<h3 id="lazy-range-algorithms"> Lazy range algorithms</h3>
<p>The functions in <a href="http://dlang.org/phobos/std_range.html"><code class="prettyprint">std.range</code></a> and
<a href="http://dlang.org/phobos/std_algorithm.html"><code class="prettyprint">std.algorithm</code></a> provide
building blocks that make use of this interface. Ranges enable the
composition of complex algorithms behind an object that
can be iterated with ease. Furthermore, ranges enable the creation of <strong>lazy</strong>
objects that only perform a calculation when it's really needed
in an iteration e.g. when the next range's element is accessed.
Special range algorithms will be presented later in the
<a href="/tour/en/gems/range-algorithms">D's Gems</a> section.
</p>
<h3 id="in-depth"> In-depth</h3>
<ul>
<li><a href="http://dlang.org/phobos/std_algorithm.html"><code class="prettyprint">std.algorithm</code></a>
</li>
<li><a href="http://dlang.org/phobos/std_range.html"><code class="prettyprint">std.range</code></a>
</li>
</ul>
</div>
<div ng-show="showProgramOutput">
<div class="content-command-box">
<button class="btn btn-danger btn-sm" ng-click="showProgramOutput = !showProgramOutput">
<span class="fa fa-close"></span>
</button>
</div>
<h2 class="program-output-title">rdmd playground.d</h2><div id="nanobar" ng-show="inProgress"></div><pre id="program-output" ng-bind-html="programOutput"></pre></div>
</div>
<div ng-class="{&#39;col-md-5 col-sm-12&#39;: showContent, &#39;col-md-12&#39;: !showContent}" ng-show="showSourceCode" style="padding-left: 0px; padding-right: 0px">
<div id="command-box" class="text-right">
<button class="btn btn-default" ng-click="showContent = !showContent">
<i class="fa fa-expand" ng-show="showContent" aria-hidden="true"></i><i class="fa fa-compress" ng-hide="showContent" aria-hidden="true"></i>
</button>
<button class="btn btn-default btn-sm hidden-xs hidden-sm" ng-click="showSourceCode = false">
<div class="fa fa-eye-slash"></div>
</button>
<button class="btn btn-primary" ng-click="run()">
<span>Run</span><i class="fa fa-play" aria-hidden="true"></i>
</button>
<button class="btn btn-default" ng-click="format()">
<span>Format</span><i class="fa fa-magic" aria-hidden="true"></i>
</button>
<button class="btn btn-default" ng-click="reset()">
<span>Reset</span><i class="fa fa-undo" aria-hidden="true"></i>
</button>
<button class="btn btn-default" ng-click="export()">
<span>Export</span><i class="fa fa-share" aria-hidden="true"></i>
</button>
</div>
<ui-codemirror ui-codemirror-opts="editorOptions" ui-codemirror="{ onLoad : codemirrorLoaded }" ng-model="sourceCode"></ui-codemirror>
</div>
</div>
<nav class="navbar navbar-bottom">
<div class="container text-center h1">
<a href="/tour/en/basics/foreach" ng-show="&#39;&#39; != &#39;/tour/en/basics/foreach&#39;" target="_self">
<
<span class="h4">Foreach</span>
</a>
13 /
<span class="small">20</span>
<a href="/tour/en/basics/associative-arrays" ng-show="&#39;&#39; != &#39;/tour/en/basics/associative-arrays&#39;" target="_self">
<span class="h4">Associative Arrays</span>>
</a>
</div>
<div class="container hidden-xs hidden-sm">
<p class="text-muted text-center">
<kbd>?</kbd>Keyboard Shortcuts
</p>
</div>
</nav>
<script type="application/javascript" src="/static/js/menu.js"></script><script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', '', 'auto');
ga('send', 'pageview');
</script><script src="/static/js/tour-controller.js"></script><script src="/static/js/swipe.js"></script><script src="/static/lib/codemirror/lib/codemirror.js"></script><script src="/static/lib/codemirror/mode/d/d.js"></script><script src="/static/lib/codemirror/addon/lint/lint.js"></script><script src="/static/lib/codemirror/addon/runmode/runmode.js"></script><script src="/static/lib/ui-codemirror.min.js"></script><script src="/static/lib/hotkeys.min.js"></script><script src="/static/lib/nanobar.min.js"></script><script src="/static/lib/ansi_up.min.js"></script>
</body>