Created
September 4, 2013 02:40
-
-
Save tom-tan/6432208 to your computer and use it in GitHub Desktop.
Ruby の Object#tap をD言語で書いてみた.デバッグ用途にどうぞ.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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