Skip to content

Commit 6f1c10e

Browse files
committed
factor out ERR_NAN_FOUND
1 parent 308fd08 commit 6f1c10e

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

src/methods.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,15 +194,15 @@ function Dist.modes(d::UnivariateFinite{S,V,R,P}) where {S,V,R,P}
194194
return d.decoder(M)
195195
end
196196

197+
const ERR_NAN_FOUND = DomainError(
198+
NaN,
199+
"`mode(s)` is invalid for a `UnivariateFinite` distribution "*
200+
"with `pdf` containing `NaN`s"
201+
)
202+
197203
function throw_nan_error_if_needed(x)
198204
if isnan(x)
199-
throw(
200-
DomainError(
201-
NaN,
202-
"`mode(s)` is invalid for a `UnivariateFinite` distribution "*
203-
"with `pdf` containing `NaN`s"
204-
)
205-
)
205+
throw(ERR_NAN_FOUND)
206206
end
207207
end
208208

test/arrays.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Random
1010
using Missings
1111
using ScientificTypes
1212

13-
import CategoricalDistributions: classes
13+
import CategoricalDistributions: classes, ERR_NAN_FOUND
1414
import CategoricalArrays.unwrap
1515

1616
rng = StableRNG(111)
@@ -220,7 +220,7 @@ end
220220
],
221221
pool=missing
222222
)
223-
@test_throws DomainError mode.(unf_arr)
223+
@test_throws ERR_NAN_FOUND mode.(unf_arr)
224224
end
225225

226226
@testset "broadcasting modes" begin
@@ -261,7 +261,7 @@ end
261261
],
262262
pool=missing
263263
)
264-
@test_throws DomainError modes.(unf_arr)
264+
@test_throws ERR_NAN_FOUND modes.(unf_arr)
265265
end
266266

267267
@testset "cat for UnivariateFiniteArray" begin

test/methods.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Random
99
rng = StableRNG(123)
1010
using ScientificTypes
1111

12-
import CategoricalDistributions: classes
12+
import CategoricalDistributions: classes, ERR_NAN_FOUND
1313

1414
v = categorical(collect("asqfasqffqsaaaa"), ordered=true)
1515
V = categorical(collect("asqfasqffqsaaaa"))
@@ -181,7 +181,7 @@ end
181181

182182
# `mode` of `Univariate` objects containing `NaN` in probs.
183183
unf = UnivariateFinite([0.1, 0.2, NaN, 0.1, NaN], pool=missing)
184-
@test_throws DomainError mode(unf)
184+
@test_throws ERR_NAN_FOUND mode(unf)
185185
end
186186

187187
@testset "Univariate modes, bimodal" begin
@@ -195,7 +195,7 @@ end
195195

196196
# `mode` of `Univariate` objects containing `NaN` in probs.
197197
unf = UnivariateFinite([0.1, 0.2, NaN, 0.1, NaN], pool=missing)
198-
@test_throws DomainError modes(unf)
198+
@test_throws ERR_NAN_FOUND modes(unf)
199199
end
200200

201201
@testset "UnivariateFinite methods" begin

0 commit comments

Comments
 (0)