Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport ComposedFunction #720

Merged
merged 12 commits into from
Sep 25, 2020
30 changes: 30 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,36 @@ if VERSION < v"1.5.0-DEV.438" # 0a43c0f1d21ce9c647c49111d93927369cd20f85
Base.startswith(s) = Base.Fix2(startswith, s)
end

# https://github.com/JuliaLang/julia/pull/37517
if VERSION < v"1.6.0" # TODO: specify the version when JuliaLang/julia#37517 is merged
tkf marked this conversation as resolved.
Show resolved Hide resolved
# https://github.com/JuliaLang/julia/pull/35980
if VERSION < v"1.6.0-DEV.85"
const ComposedFunction = let h = identity ∘ convert
Base.typename(typeof(h)).wrapper
end
@eval ComposedFunction{F,G}(f, g) where {F,G} =
$(Expr(:new, :(ComposedFunction{F,G}), :f, :g))
ComposedFunction(f::F, g::G) where {F,G} = ComposedFunction{F,G}(f, g)
tkf marked this conversation as resolved.
Show resolved Hide resolved
tkf marked this conversation as resolved.
Show resolved Hide resolved
else
using Base: ComposedFunction
end
function Base.getproperty(c::ComposedFunction, p::Symbol)
if p === :f
return getfield(c, :f)
elseif p === :g
return getfield(c, :g)
elseif p === :outer
return getfield(c, :f)
elseif p === :inner
return getfield(c, :g)
end
error("type ComposedFunction has no property ", p)
end
Base.propertynames(c::ComposedFunction) = (:f, :g, :outer, :inner)
else
using Base: ComposedFunction
end

include("iterators.jl")
include("deprecated.jl")

Expand Down
16 changes: 16 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,22 @@ end
@test endswith("d")("abcd")
end

# https://github.com/JuliaLang/julia/pull/37517
@testset "ComposedFunction" begin
@test sin ∘ cos isa Compat.ComposedFunction
@test sin ∘ cos === Compat.ComposedFunction(sin, cos)
c = sin ∘ cos
@test c.outer === sin
@test c.inner === cos
if VERSION < v"1.6.0" # TODO: specify the version when JuliaLang/julia#37517 is merged
tkf marked this conversation as resolved.
Show resolved Hide resolved
@test c.f === sin
@test c.g === cos
@test propertynames(c) == (:f, :g, :outer, :inner)
else
@test propertynames(c) == (:outer, :inner)
end
end

include("iterators.jl")

nothing