mirror of
https://github.com/dlang/phobos.git
synced 2025-05-11 14:41:08 +03:00
Merge pull request #5961 from wilzbach/std-range-nullSink
std.range.nullSink - allow convenient use of the NullSink
This commit is contained in:
commit
9d7b13b95b
2 changed files with 55 additions and 3 deletions
20
changelog/std-range-nullsink.dd
Normal file
20
changelog/std-range-nullsink.dd
Normal 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");
|
||||||
|
---
|
|
@ -117,7 +117,7 @@ $(BOOKTABLE ,
|
||||||
loop. Similar to $(D zip), except that $(D lockstep) is designed
|
loop. Similar to $(D zip), except that $(D lockstep) is designed
|
||||||
especially for $(D foreach) loops.
|
especially for $(D foreach) loops.
|
||||||
))
|
))
|
||||||
$(TR $(TD $(LREF NullSink))
|
$(TR $(TD $(LREF nullSink))
|
||||||
$(TD An output _range that discards the data it receives.
|
$(TD An output _range that discards the data it receives.
|
||||||
))
|
))
|
||||||
$(TR $(TD $(LREF only))
|
$(TR $(TD $(LREF only))
|
||||||
|
@ -11677,14 +11677,46 @@ struct NullSink
|
||||||
void put(E)(E){}
|
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.iteration : map;
|
||||||
import std.algorithm.mutation : copy;
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
/++
|
/++
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue