次のコードを考えると
import std.datetime: Clock, SysTime, Duration;
SysTime[] times;
const n = 3;
foreach (i; 0..n) times ~= Clock.currTime;
同じ目標を達成するための、より単純で、おそらく機能的で、高次のパターンはありますか?
ボーナスは、可能であれば、おそらくインプレース構築パターンを通じて要素のコピーを最小限に抑えることです。
更新:
これまでの私の試みは次のとおりです。
enum arityMin0(alias fun) = __traits(compiles, fun());
auto apply(alias fun, N)(N n) if (isCallable!fun &&
arityMin0!fun &&
!is(ReturnType!fun == void) &&
isIntegral!N)
{
import std.range: iota, map;
return n.iota.map!(n => fun);
}
たとえば、
import std.datetime: Clock;
auto times = 3.apply!(Clock.currTime).array;
1つの詳細が残っています。制限
arity!fun == 0
に評価falseする
auto times = 3.apply!(Clock.currTime).array;
ここでは、アリティは実際には 0 か 1 のいずれかです。
はデフォルトの引数を取るため、この場合は とarity!fun評価されます。1Clock.currTime
たぶん、私たちもarityMin必要arityMaxですstd.traits。
その場合__traits(compiles、実装に使用する必要がありますarityMinか?