A Small Contribution to Phobos
irritate
irritate at gmail.com
Sun Jun 16 06:39:33 PDT 2013
On Monday, 3 June 2013 at 06:58:00 UTC, monarch_dodra wrote:
> I disagree. One thing a user could expect out of tee is to
Hello.
I have created a pull request for tee:
https://github.com/D-Programming-Language/phobos/pull/1348
(I did this based on discussion in Issue 9882, and was not aware
of this present forum thread until monarch_dodra asked me to
present my approach here).
My approach is to create an InputRange wrapper called TeeRange,
which will call the user provided function with each element of
the wrapped range during iteration.
One concept with this is that the user can pass a flag to specify
whether the function should be called on popFront (default) or on
front. The following unittest from my change illustrates that
distinction:
---
unittest
{
// Manually stride to test different pipe behavior.
void testRange(Range)(Range r)
{
const int strideLen = 3;
int i = 0;
typeof(Range.front) elem;
while (!r.empty)
{
if (i % strideLen == 0)
{
elem = r.front();
}
r.popFront();
i++;
}
}
string txt = "abcdefghijklmnopqrstuvwxyz";
int popCount = 0;
auto pipeOnPop = tee!(a => popCount++)(txt);
testRange(pipeOnPop);
assert(popCount == 26);
int frontCount = 0;
auto pipeOnFront = tee!(a => frontCount++, false)(txt);
testRange(pipeOnFront);
assert(frontCount == 9);
}
---
Thanks,
irritate
More information about the Digitalmars-d
mailing list