One liner for creating an array filled by a factory method

Mengu mengukagan at gmail.com
Fri Dec 22 23:33:55 UTC 2017


On Thursday, 21 December 2017 at 21:11:58 UTC, Steven 
Schveighoffer wrote:
> On 12/21/17 4:00 PM, kerdemdemir wrote:
>> I have a case like :
>> 
>> http://rextester.com/NFS28102
>> 
>> I have a factory method, I am creating some instances given 
>> some enums.
>> My question is about :
>> 
>> 
>> void PushIntoVector( BaseEnum[] baseEnumList )
>> {
>>      Base[] baseList;
>>      foreach ( tempEnum; baseEnumList )
>>      {
>>         baseList ~= Factory(tempEnum);
>>      }
>> }
>> 
>> I don't want to use "foreach" loop. Is there any cool std 
>> function that I can call ?
>> 
>> Something like baseEnumList.CoolStdFunc!( a=> Factory(a) 
>> )(baseList);
>> 
>
> https://dlang.org/phobos/std_algorithm_iteration.html#map
>
> -Steve

so basically it becomes:

Base[] baseList = baseEnumList.map!(el => Factory(el));

there's also a parallel version of map [0] if you ever need to 
map the list concurrently.

[0] https://dlang.org/phobos/std_parallelism.html#.TaskPool.map


More information about the Digitalmars-d-learn mailing list