Skip to content

Commit 300616e

Browse files
committed
Update ds/union to not throw error when values are not present
1 parent da28402 commit 300616e

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

lib/algorithms/disjoint_set.ex

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,20 +123,18 @@ defmodule AdventOfCode.Algorithms.DisjointSet do
123123
iex> DisjointSet.union(set, 3, 1) == set
124124
true
125125
126-
iex> DisjointSet.new(1) |> DisjointSet.union(100, 200)
127-
:error
128-
129-
iex> DisjointSet.union(:error, 100, 200)
130-
:error
126+
iex> ds = DisjointSet.new(1)
127+
iex> ds == DisjointSet.union(ds, 100, 200)
128+
true
131129
132130
"""
133-
@spec union(t() | :error, value(), value()) :: t() | :error
131+
@spec union(t(), value(), value()) :: t()
134132
def union(%__MODULE__{} = disjoint_set, a, b) do
135133
with {root_a, disjoint_set} <- find(disjoint_set, a),
136134
{root_b, disjoint_set} <- find(disjoint_set, b) do
137135
union_by_rank(disjoint_set, root_a, root_b)
138136
else
139-
_ -> :error
137+
_ -> disjoint_set
140138
end
141139
end
142140

@@ -155,10 +153,10 @@ defmodule AdventOfCode.Algorithms.DisjointSet do
155153
...> |> DisjointSet.components()
156154
[MapSet.new([{0, 0}, {0, 1}, {0, 2}]), MapSet.new([{10, 11}, {10, 12}]), MapSet.new([{100, 200}])]
157155
158-
iex> DisjointSet.new(10)
156+
iex> DisjointSet.new(3)
159157
...> |> DisjointSet.union(20, 30)
160158
...> |> DisjointSet.components()
161-
:error
159+
[MapSet.new([0]), MapSet.new([1]), MapSet.new([2])]
162160
163161
"""
164162
@spec components(t() | :error) :: [[term()]]

0 commit comments

Comments
 (0)