Merge pull request #5961 from wilzbach/std-range-nullSink

std.range.nullSink - allow convenient use of the NullSink
This commit is contained in:
Andrei Alexandrescu 2018-01-26 18:41:56 -05:00 committed by GitHub
commit 9d7b13b95b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 3 deletions

View file

@ -0,0 +1,20 @@
`nullSink` was added to `std.range`
$(REF nullSink, std, range) is a convenience wrapper for $(REF NullSink, std,range)
and creates an output range that discards the data it receives. It's the range analog
of `/dev/null`.
---
import std.csv : csvNextToken;
string line = "a,b,c";
// ignore the first column
line.csvNextToken(nullSink, ',', '"');
line.popFront;
// look at the second column
Appender!string app;
line.csvNextToken(app, ',', '"');
assert(app.data == "b");
---

View file

@ -117,7 +117,7 @@ $(BOOKTABLE ,
loop. Similar to $(D zip), except that $(D lockstep) is designed
especially for $(D foreach) loops.
))
$(TR $(TD $(LREF NullSink))
$(TR $(TD $(LREF nullSink))
$(TD An output _range that discards the data it receives.
))
$(TR $(TD $(LREF only))
@ -11677,14 +11677,46 @@ struct NullSink
void put(E)(E){}
}
/// ditto
auto ref nullSink()
{
static NullSink sink;
return sink;
}
///
@safe unittest
@safe nothrow unittest
{
import std.algorithm.iteration : map;
import std.algorithm.mutation : copy;
[4, 5, 6].map!(x => x * 2).copy(NullSink()); // data is discarded
[4, 5, 6].map!(x => x * 2).copy(nullSink); // data is discarded
}
///
@safe unittest
{
import std.csv : csvNextToken;
string line = "a,b,c";
// ignore the first column
line.csvNextToken(nullSink, ',', '"');
line.popFront;
// look at the second column
Appender!string app;
line.csvNextToken(app, ',', '"');
assert(app.data == "b");
}
@safe unittest
{
auto r = 10.iota
.tee(nullSink)
.dropOne;
assert(r.front == 1);
}
/++