How to instantiate a map with multiple functions

karthikeyan via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Dec 26 11:46:26 PST 2015


On Saturday, 26 December 2015 at 19:38:16 UTC, Fusxfaranto wrote:
> On Saturday, 26 December 2015 at 19:30:24 UTC, karthikeyan 
> wrote:
>> How to instantiate a map with multiple functions. I looked 
>> into the docs at 
>> http://dlang.org/phobos/std_algorithm_iteration.html#map. They 
>> contain a string which I suppose is a mixin and when I change 
>> "a" to some other name it results in an error for me. Are 
>> there any ways to use lambda functions directly instead of 
>> strings and any explanation of the strings used in the map 
>> example and why a is used will be helpful.
>>
>> I tried reading the source 
>> https://github.com/D-Programming-Language/phobos/blob/master/std/algorithm/iteration.d#L520 .Some hint that "a" should be used at https://github.com/D-Programming-Language/phobos/blob/master/std/functional.d#L101 but how do I change that since map doesn't allow me any params to specify the parameter name. Also how can I map an array of tuples with two or more elements with a function of two or more params like unpack the tuple into a function like that. I am D beginner so any will insights will be very helpful for me.
>>
>> I am using dmd version 2.069 on Linux Mint 15 - 64bit
>
> You should be able to just use any function (including lambdas) 
> as a template argument to map.  The string version is from 
> before the more concise "=>" lambda syntax was developed, and 
> generally isn't what you want to use nowadays.

Thanks but the following returns an error for me

   import std.algorithm.comparison : equal;
   import std.range : chain;
   int[] arr1 = [ 1, 2, 3, 4 ];
   int[] arr2 = [ 5, 6 ];
   auto dd = map!(z => z * z, c => c * c * c)(chain(arr1, arr2));
   writeln(dd);

Error :

/usr/include/dmd/phobos/std/meta.d(546): Error: template instance 
F!(__lambda2) cannot use local '__lambda2' as parameter to 
non-global template AppliedReturnType(alias f)
/usr/include/dmd/phobos/std/meta.d(552): Error: template instance 
maps_square.main.staticMap!(AppliedReturnType, __lambda2) error 
instantiating
/usr/include/dmd/phobos/std/algorithm/iteration.d(447):        
instantiated from here: staticMap!(AppliedReturnType, __lambda2, 
__lambda3)
maps_square.d(71):        instantiated from here: map!(Result)
/usr/include/dmd/phobos/std/meta.d(546): Error: template instance 
F!(__lambda3) cannot use local '__lambda3' as parameter to 
non-global template AppliedReturnType(alias f)
/usr/include/dmd/phobos/std/meta.d(553): Error: template instance 
maps_square.main.staticMap!(AppliedReturnType, __lambda3) error 
instantiating
/usr/include/dmd/phobos/std/algorithm/iteration.d(447):        
instantiated from here: staticMap!(AppliedReturnType, __lambda2, 
__lambda3)
maps_square.d(71):        instantiated from here: map!(Result)

Am I missing something here over how the lambda notation can be 
used? I personally prefer the lambda notation to be clear than 
passing strings as arguments. Kindly help me on this.


More information about the Digitalmars-d-learn mailing list