@@ -123,20 +123,18 @@ defmodule AdventOfCode.Algorithms.DisjointSet do
123
123
iex> DisjointSet.union(set, 3, 1) == set
124
124
true
125
125
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
131
129
132
130
"""
133
- @ spec union ( t ( ) | :error , value ( ) , value ( ) ) :: t ( ) | :error
131
+ @ spec union ( t ( ) , value ( ) , value ( ) ) :: t ( )
134
132
def union ( % __MODULE__ { } = disjoint_set , a , b ) do
135
133
with { root_a , disjoint_set } <- find ( disjoint_set , a ) ,
136
134
{ root_b , disjoint_set } <- find ( disjoint_set , b ) do
137
135
union_by_rank ( disjoint_set , root_a , root_b )
138
136
else
139
- _ -> :error
137
+ _ -> disjoint_set
140
138
end
141
139
end
142
140
@@ -155,10 +153,10 @@ defmodule AdventOfCode.Algorithms.DisjointSet do
155
153
...> |> DisjointSet.components()
156
154
[MapSet.new([{0, 0}, {0, 1}, {0, 2}]), MapSet.new([{10, 11}, {10, 12}]), MapSet.new([{100, 200}])]
157
155
158
- iex> DisjointSet.new(10 )
156
+ iex> DisjointSet.new(3 )
159
157
...> |> DisjointSet.union(20, 30)
160
158
...> |> DisjointSet.components()
161
- :error
159
+ [MapSet.new([0]), MapSet.new([1]), MapSet.new([2])]
162
160
163
161
"""
164
162
@ spec components ( t ( ) | :error ) :: [ [ term ( ) ] ]
0 commit comments