How to expand an expression along with a parameter tuple?
Ali Çehreli
acehreli at yahoo.com
Mon Jun 17 09:13:37 PDT 2013
On 06/17/2013 02:32 AM, TommiT wrote:
> On Monday, 17 June 2013 at 07:20:23 UTC, Ali Çehreli wrote:
>>
>> The following does not answer the question of expanding but at least
>> foo() receives [30, 70, 110] :)
>>
>> import std.stdio;
>> import std.algorithm;
>> import std.array;
>> import std.range;
>>
>> int[] arr = [ 1, 3, 5, 7, 11 ];
>>
>> void foo(T)(T[] values...)
>> {
>> writeln(values);
>> }
>>
>> void bar(T)(T[] values...)
>> {
>> foo(arr
>> .indexed(values)
>> .map!(a => a * 10)
>> .array);
>> }
>>
>> void main()
>> {
>> bar(1, 3, 4);
>> }
>>
>> Ali
>
> Yeah, that would work. I'd hate the overhead though.
There is no inherent overhead though. I called .array only because I
thought that the C++ version was eager. If we stay lazy and D-like, we
can make foo() take a range:
import std.stdio;
import std.algorithm;
import std.range;
int[] arr = [ 1, 3, 5, 7, 11 ];
void foo(R)(R range) // <-- now takes range
{
writeln(range);
}
void bar(T)(T[] values...)
{
foo(arr
.indexed(values)
.map!(a => a * 10)); // <-- no .array anymore
}
void main()
{
bar(1, 3, 4);
}
I hear that ldc (and perhaps gdc) perform pretty magical compiler
optimizations. There is no reason for the code above to be slower than
the hand-written equivalent.
Ali
More information about the Digitalmars-d-learn
mailing list