digitalmars.D.learn - One liner for creating an array filled by a factory method
- kerdemdemir (18/18) Dec 21 2017 I have a case like :
- Steven Schveighoffer (3/25) Dec 21 2017 https://dlang.org/phobos/std_algorithm_iteration.html#map
- Mengu (7/34) Dec 22 2017 so basically it becomes:
- kerdemdemir (5/41) Dec 23 2017 Yeah that was very easy and I used to use map for this purposed a
- Mengu (10/55) Dec 23 2017 it totally depends on the type of resulting element. if you
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); Erdem
Dec 21 2017
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
Dec 21 2017
On Thursday, 21 December 2017 at 21:11:58 UTC, Steven Schveighoffer wrote:On 12/21/17 4:00 PM, kerdemdemir wrote: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.mapI 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
Dec 22 2017
On Friday, 22 December 2017 at 23:33:55 UTC, Mengu wrote:On Thursday, 21 December 2017 at 21:11:58 UTC, Steven Schveighoffer wrote:Yeah that was very easy and I used to use map for this purposed a lot already. I don't know why I get confused. Thanks guys for correction. I began to think like map could transform but it can't create vector of elements and this confused me.On 12/21/17 4:00 PM, kerdemdemir wrote: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.mapI 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
Dec 23 2017
On Saturday, 23 December 2017 at 08:57:18 UTC, kerdemdemir wrote:On Friday, 22 December 2017 at 23:33:55 UTC, Mengu wrote:it totally depends on the type of resulting element. if you expect Base[], then your map should transform your range / array elements into a Base. import std.range, std.algorithm; auto a = iota(1, 10); int[] b = a.map!(el => el + 1).array; int[][] c = a.map!(el => [el, el + 1]).array; writeln(b); writeln(c);On Thursday, 21 December 2017 at 21:11:58 UTC, Steven Schveighoffer wrote:Yeah that was very easy and I used to use map for this purposed a lot already. I don't know why I get confused. Thanks guys for correction. I began to think like map could transform but it can't create vector of elements and this confused me.On 12/21/17 4:00 PM, kerdemdemir wrote: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.mapI 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
Dec 23 2017