Ranges and Algorithms -- Templates, Delegates, or Ranges?

Lutger Blijdestijn lutger.blijdestijn at gmail.com
Tue Feb 22 04:29:09 PST 2011


Mafi wrote:

> Am 22.02.2011 11:29, schrieb %u:
>> Having learned functional programming in Scheme a couple months ago, I
>> tried my hand at using map(), reduce(), and filter() in D:
>>
>>      int addend = 5;
>>      map(delegate int(int x) { return x + addend; }, iota(1, 5));
>>
>> but it didn't work. It turned out that map() actually took the mapper as
>> its _template_ argument, not as a function argument. Not too much of a
>> problem, it probably seemed to be... except that it's a critical problem.
>> It makes map(), reduce(), filter(), etc. to become 10x less useful,
>> because there's no way to change their behavior at runtime, depending on
>> program's state.
>>
> 
> I did never try and so I'm unsure but shouldn't you be able to give a
> delegate variable as template parameter.
> std.algorithms.map's parameter is an alias parameter so it should be
> able alias your variable and use it's runtime value.
> If it does not work this way it's a bug IMO.
> 
> Mafi


yes it works: map!((int x) { return x + addend; })(iota(1, 5));

It's called local template instantiation iirc, and is restricted to global 
templates (which map is).


More information about the Digitalmars-d mailing list