Question

Given the following code

import std.datetime: Clock, SysTime, Duration;
SysTime[] times;
const n = 3;
foreach (i; 0..n) times ~= Clock.currTime;

is there a simpler, perhaps functional, higher order pattern with which to achieve the same goal?

A bonus would to be, when possible, minimize copyings of the elements perhaps through some in-place construction pattern.

See also: http://forum.dlang.org/thread/yofbijaejfyftpcjdcvd@forum.dlang.org#post-yofbijaejfyftpcjdcvd:40forum.dlang.org

Update:

Ok here's my try so far:

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);
}

called as, for instance,

import std.datetime: Clock;
auto times = 3.apply!(Clock.currTime).array;

One detail left. The restriction

arity!fun == 0

evaluate to false in

auto times = 3.apply!(Clock.currTime).array;

because arity is actually either 0 and 1 here.

So arity!fun evaluates to 1 in this case because Clock.currTime takes a defaulted argument.

Maybe we need arityMin and arityMax in std.traits aswell.

In that case should I use __traits(compiles to implement arityMin?

Was it helpful?

Solution

Evaluating currTime thrice:

auto times = 3.iota.map!(n => Clock.currTime).array();

Evaluating currTime once:

auto times = Clock.currTime.repeat(3).array();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top