Bug 15480 – std.algorithm.iteration.map not accepting multiple lambdas
Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2015-12-27T14:24:00Z
Last change time
2016-03-19T20:22:36Z
Assigned to
nobody
Creator
tir.karthi
Comments
Comment #0 by tir.karthi — 2015-12-27T14:24:14Z
The map function in std.algorithm.iteration only accepts strings with "a" as the variable name for multiple functions at https://dlang.org/phobos/std_algorithm_iteration.html#.map . The following code doesn't work with two lambdas being passed to map as follows :
Program
import std.algorithm.iteration : map;
import std.algorithm.comparison : equal;
import std.range : chain;
void main() {
int[] arr1 = [ 1, 2, 3, 4 ];
int[] arr2 = [ 5, 6 ];
auto dd = map!(z => z * z, c => c * c * c)(chain(arr1, arr2));
}
Output :
/usr/include/dmd/phobos/std/meta.d(546): Error: template instance F!(__lambda1) cannot use local '__lambda1' 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, __lambda1) error instantiating
/usr/include/dmd/phobos/std/algorithm/iteration.d(447): instantiated from here: staticMap!(AppliedReturnType, __lambda1, __lambda2)
maps_square.d(11): instantiated from here: map!(Result)
/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(553): 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, __lambda1, __lambda2)
maps_square.d(11): instantiated from here: map!(Result)
The same program when replaced with mixins instead of functions with variable name as "a" works
Program :
import std.algorithm.iteration : map;
import std.algorithm.comparison : equal;
import std.range : chain;
void main() {
int[] arr1 = [ 1, 2, 3, 4 ];
int[] arr2 = [ 5, 6 ];
auto dd = map!("a * a", "a * a * a")(chain(arr1, arr2));
}
The relevant discussion at dlang learn forum at http://forum.dlang.org/thread/[email protected]
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. It will be helpful to specify lambdas instead of mixins with "a" variable names as they are more readable and elegant. The workaround was to use tuples in the forum. I am d beginner and any thoughts on confirming this as a bug will be helpful.
System information :
OS : Linux Mint 15 (64 bit)
DMD version : DMD64 D Compiler v2.069.2