On Wednesday, 19 October 2016 at 13:59:29 UTC, Nordlöw wrote:
Is there a generic way to do


typeof(fn(E.init))[] map1(alias fn, E, size_t n)(E[n] container)
{
  import std.algorithm.iteration : map;
  import std.array : array;
  return container[].map!fn.array;
}

@safe pure nothrow unittest
{
  int[42] c;
  auto result = map1!(_ => _^2)(c);
}


but with propagation of length of return value of `f` to a static array instead, without having to specialize each range separately?

One less elegant way would be to replace the call to `array` with an overload of `std.container.util.make` for static arrays, that checks at run-time that the length of the output static array matches the length of the input range?

Couldn't you do this? Mind you I'm not able to test the code right now, but I think it can work with some tweaking.

typeof(fn(E.init))[n] map1(alias fn, E, size_t n)(E[n] container)
{
    import std.algorithm.iteration : map;
    import std.array : array;

    typeof(return) sres;
    return container[].map!fn.copy(sres[]);
}

@safe pure nothrow unittest
{
   int[42] c;
   //result should be a static array of length 42
   auto result = map1!(_ => _^2)(c);
}

Reply via email to