From 4cc20ad21179dbf61b078741cacf2d30de8d5d4c Mon Sep 17 00:00:00 2001 From: Neven Sajko Date: Mon, 27 Nov 2023 22:46:39 +0100 Subject: [PATCH] Add an `operate` method for `abs` (#243) --- src/interface.jl | 4 ++++ test/interface.jl | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/interface.jl b/src/interface.jl index 5b3452e..c825ddb 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -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(+), diff --git a/test/interface.jl b/test/interface.jl index 0f3c4e3..9e3e654 100644 --- a/test/interface.jl +++ b/test/interface.jl @@ -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