www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 17201] New: std.array: invert (convenience method that

https://issues.dlang.org/show_bug.cgi?id=17201

          Issue ID: 17201
           Summary: std.array: invert (convenience method that inverts a
                    hashmap)
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: greeenify gmail.com

This is often used and could potentially be implemented in a more performant
way than the naive solution:

auto invert(A)(A a)
{
import std.algorithm.iteration : map;
import std.array;
import std.typecons : tuple;
auto a = [0:"a", 1:"b", 2:"c"];
return a.byKeyValue.map!(pair => tuple(pair.value, pair.key)).assocArray;
}

--
Feb 19 2017