Hi,

I think an invert() method on Map would be convenient to use.  It is only a 
handful of lines of code to add to a custom program, but why should users 
need to do that?  In my opinion this basic functionality should be 
implemented by the Map module. 

The invert() method would operate the same way it does for Ruby:
https://ruby-doc.org/core-2.2.2/Hash.html#method-i-invert

Recently I wanted to invert a map in one of my Elixir programs (details 
below), and I googled and found the below solution by Chris McCord in h
ttps://stackoverflow.com/questions/26614682/how-to-change-all-the-values-in-an-elixir-map
:

def invert(map) when is_map(map) do
    for {k, v} <- map, into: %{}, do: {v, k}
  end

I would basically like the above function to be added to the Elixir.Map 
module.

Here is the scenario for which I wanted to use this the invert function:

*My Elixir program is synchronizing Robot Framework source code files with 
TestRail.  Robot Framework defines automated test cases.  TestRail is a 
web-based Test Case Management system.   The goal is to synchronize 
TestRail to contain all the tests that are defined in the Robot Framework 
files, and in the same directory structure.*

*Originally the Elixir program was specified to only create test cases in 
TestRail.  When you are creating a test case, the TestRail API 
<http://docs.gurock.com/testrail-api2/reference-cases#add_case> requires 
you to specify the ID of the directory (aka "section") where the case is 
written.  For this I use an Elixir Map to map paths to section IDs.  When I 
want to create a file in directory "/path/to/test.robot", then I Lookup the 
section ID from my map & then call the "add_case" TestRail API function.*

*That version of the program was a success.  My boss was impressed and next 
asked for enhancements to the program.  The new functionality being 
requested is to have existing TestRail Test Cases get updated with the 
latest test steps defined in Robot Framework.  Part of that update is to 
verify that the path to the test case is the same in the Robot Framework 
source code and in the TestRail.  In this case we first call the TestRail 
API to lookup a test case, at which point we know the section ID that it 
belongs to.  But we need to translate that to a directory path and confirm 
that this path is the same location as where the test file exists in the 
Robot Framework source code.  *

*For updating, it makes sense to also be able to use an Elixir Map to map 
from the Section ID back to the Path -- the inverse of the original map.  *

*It is true that I could iterate through the original map until I found the 
section ID in the value section, and then return the map index.  But that 
seems awfully inefficient when you consider that there are hundreds of test 
files and thousands of tests.  So I decided an inverse map would provide 
faster lookup, at the cost of more memory being used.  Also note that in my 
scenario both the indexes and the values are unique; it is a 1-to-1 
mapping.*


This is just one example scenario.  I imagine other examples exist, or else 
the invert() function would never have been created in the Ruby community.

-- 
You received this message because you are subscribed to the Google Groups 
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elixir-lang-core+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/b2c7e58a-ad99-4458-a45e-9f7d5f7fce15%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to