Skip to content

Commit 4d27537

Browse files
authored
Merge pull request #22 from eliascarv/type-stable-functional
Type Stable Functional
2 parents ce70534 + 68047aa commit 4d27537

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

src/transforms/functional.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
88
The transform that applies a `function` elementwise.
99
"""
10-
struct Functional <: Colwise
11-
func::Function
10+
struct Functional{F} <: Colwise
11+
func::F
1212
end
1313

1414
isrevertible(transform::Functional) =
@@ -23,7 +23,7 @@ inverse(::typeof(sin)) = asin
2323
inverse(::typeof(asin)) = sin
2424

2525
# fallback to nothing
26-
inverse(::Function) = nothing
26+
inverse(::Any) = nothing
2727

2828
colcache(::Functional, x) = nothing
2929

test/runtests.jl

+7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ islinux = Sys.islinux()
2020
visualtests = !isCI || (isCI && islinux)
2121
datadir = joinpath(@__DIR__,"data")
2222

23+
# for functor tests in Functional testset
24+
struct Polynomial{T<:Real}
25+
coeffs::Vector{T}
26+
end
27+
Polynomial(args::T...) where {T<:Real} = Polynomial(collect(args))
28+
(p::Polynomial)(x) = sum(a * x^(i-1) for (i, a) in enumerate(p.coeffs))
29+
2330
# list of tests
2431
testfiles = [
2532
"distributions.jl",

test/transforms.jl

+13
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,19 @@
358358
n, c = apply(T, t)
359359
@test t == n
360360
@test isrevertible(T) == false
361+
362+
# functor tests
363+
x = rand(1500)
364+
y = rand(1500)
365+
t = Table(; x, y)
366+
f = Polynomial(1, 2, 3) # f(x) = 1 + 2x + 3x²
367+
T = Functional(f)
368+
n, c = apply(T, t)
369+
@test f.(x) == n.x
370+
@test f.(y) == n.y
371+
@test all((1), n.x)
372+
@test all((1), n.y)
373+
@test isrevertible(T) == false
361374
end
362375

363376
@testset "EigenAnalysis" begin

0 commit comments

Comments
 (0)