Skip to content

Commit

Permalink
Add an operate method for abs (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
nsajko authored Nov 27, 2023
1 parent ac7a8a9 commit 4cc20ad
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ function operate end
# custom `operate` method.
operate(::typeof(-), x) = -x

# This is not efficient, as it may make unnecessary intermediate copies,
# but seems like the best way to write a generic implementation.
operate(::typeof(abs), x) = abs(copy_if_mutable(x))

function operate(
op::Union{
typeof(+),
Expand Down
2 changes: 1 addition & 1 deletion test/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ end
@testset "operate" begin
@testset "$T" for T in (Int, BigInt, Rational{Int})
x = T(7)
@testset "1-ary $op" for op in [+, *, gcd, lcm]
@testset "1-ary $op" for op in [+, *, gcd, lcm, abs]
a = op(x)
b = MA.operate(op, x)
@test a == b
Expand Down

0 comments on commit 4cc20ad

Please sign in to comment.