How do you find inverse functions in the Elixir API reference?

Hi,

I’m currently learning Elixir on Exercism, and I’m often faced with some exercise where I want to convert data to another type, transform it, and convert the result back to the original type. Unfortunately, the Elixir API reference isn’t particularly supportive in this endeavor.

For example, there is Map.to_list/1. The documentation doesn’t mention an inverse operation. The naming pattern suggests Map.from_list or List.to_map, but neither functions exist. The most likely candidate is Map.new/1, but its documentation doesn’t mention any inverse functions either. An even better example would be String.to_charlist/1, since I can’t find its inverse function.

Am I missing something? Is there a reliable way to find a function’s inverse in the Elixir API reference?

Thank you

Map.new and List.to_string are what you want.

You are right that it does not seem to be a strong naming convention here. But the number of different types is very small, you’ll know them by heart very quickly.

Now if you want to convert a map to a list, and then back to a map, there are chances that you do not need to.

For instance, Map.new can “map” the values, just like Enum.map:

iex(1)> map = %{a: 10, b: 20}
%{a: 10, b: 20}
iex(2)> Map.new(map, fn {k, v} -> {k, v * v} end)
%{a: 100, b: 400}
2 Likes