www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 6256] New: [patch] std.algorithm.map does not support static arrays and has 'length' for narrow strings.

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6256

           Summary: [patch] std.algorithm.map does not support static
                    arrays and has 'length' for narrow strings.
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Keywords: patch
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: sandford jhu.edu


--- Comment #0 from Rob Jacques <sandford jhu.edu> 2011-07-05 17:12:18 PDT ---
std.algorithm.map doesn't support static arrays. Two minor changes to map, and
one to std.functional.unaryFunImpl (listed below) were required to make this
work.

-    auto map(Range)(Range r) if (isInputRange!(Unqual!Range))
+    auto map(Range)(auto ref Range r)
+        if (isInputRange!(Unqual!Range)  || isArray!Range )

-            alias Unqual!Range R;
+            static if(is(Unqual!Range E:E[]) ) {
+                alias E[] R;
+            } else {
+                alias Unqual!Range R;
+            }

also, map adds a length member for narrow strings. This can be fixed by
changing the appropriate static if statement.

-            static if (hasLength!R || isSomeString!R)
+            static if (hasLength!R)

Also, I found that a patch to unaryFunImpl similar to the patch in Issue 5406
for binaryFunImpl was needed to compile a simple test case:

void main(string[] args) {
    uint[5] testing = [1,2,3,4,5];
    writeln( map!"a"("testing") );
    return;
}

correctly. That patch is below:

template unaryFunImpl(alias fun, bool byRef, string parmName = "a")
{
    static if (is(typeof(fun) : string)) {
        enum testAsExp = "{ ElementType "~parmName ~"; return ("~fun~"); }()";
        static if (byRef) {
            typeof(mixin(testAsExp))
            result(ElementType)(ref ElementType __a)
                if(__traits(compiles, mixin(testAsExp)))
            {
                mixin("alias __a "~parmName~";");
                mixin("return (" ~ fun ~ ");");
            }
        } else {
            typeof(mixin(testAsExp))
            result(ElementType)(ElementType __a)
                if(__traits(compiles, mixin(testAsExp)))
            {
                mixin("alias __a "~parmName~";");
                mixin("return (" ~ fun ~ ");");
            }
        }
    } else {
        alias fun result;
    }
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 05 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6256


hsteoh quickfur.ath.cx changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |hsteoh quickfur.ath.cx


--- Comment #1 from hsteoh quickfur.ath.cx 2013-08-19 20:01:49 PDT ---
FWIW, map has been fixed in git HEAD to not export length with narrow strings:

import std.algorithm, std.range;
void main() {
        string narrow = "abcdef";
        auto m = map!(a => a)(narrow);
        static assert(!hasLength!(typeof(m)));
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 19 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6256



--- Comment #2 from hsteoh quickfur.ath.cx 2013-08-19 20:03:36 PDT ---
As for iterating over static arrays, one workaround is to slice it:

import std.algorithm, std.range, std.stdio;
void main() {
        uint[4] test = [1,2,3,4];
        writeln(map!(a=>a+10)(test[]));
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 19 2013