Skip to content

Instantly share code, notes, and snippets.

@tom-tan
Created September 4, 2013 02:40
Show Gist options
  • Save tom-tan/6432208 to your computer and use it in GitHub Desktop.
Save tom-tan/6432208 to your computer and use it in GitHub Desktop.
Ruby の Object#tap をD言語で書いてみた.デバッグ用途にどうぞ.
auto tap(alias fun, Range)(Range r)
{
alias fun_ = unaryFun!fun;
struct ResultType
{
this(Range r) { this.r = r; }
@property auto front()
{
auto ret = r.front;
fun_(ret);
return ret;
}
void popFront() { r.popFront(); }
@property auto empty() { return r.empty; }
private:
Range r;
}
return ResultType(r);
}
unittest
{
{
auto a = iota(3);
auto b = iota(3).tap!(e => e);
assert(equal(a, b));
}
{
auto a = iota(3).map!"2*a";
auto b = iota(3).tap!"3*a".map!"2*a";
assert(equal(a, b));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment