File tree Expand file tree Collapse file tree 2 files changed +32
-0
lines changed
Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change 1+ defmodule Together.Map do
2+ @ moduledoc """
3+ Helper functions for dealing with maps.
4+
5+ Would be great to have them in elixir core, but for now they aren't wanted.
6+ """
7+
8+ @ doc """
9+ Inverts a map, exchanging keys and values.
10+
11+ ## Examples
12+
13+ iex> invert(%{a: 1, b: 2})
14+ %{1 => :a, 2 => :b}
15+
16+ # this one depends a bit on ordering of the map inside the BEAM, but wanted to show that
17+ # no values get aggregated.
18+ iex> invert(%{a: 1, b: 1})
19+ %{1 => :b}
20+
21+ iex> invert(%{})
22+ %{}
23+ """
24+ @ spec invert ( map ) :: map ( )
25+ def invert ( map ) do
26+ Map . new ( map , fn { key , value } -> { value , key } end )
27+ end
28+ end
Original file line number Diff line number Diff line change 1+ defmodule Together.MapTest do
2+ use ExUnit.Case , async: true
3+ doctest Together.Map , import: true
4+ end
You can’t perform that action at this time.
0 commit comments