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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Compat"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "3.16.0"
version = "3.17.0"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ changes in `julia`.

## Supported features

* The composition operator `∘` now returns a `Compat.ComposedFunction` instead of an anonymous function ([#37517]). (since Compat 3.17)

* New function `addenv` for adding environment mappings into a `Cmd` object, returning the new `Cmd` object ([#37244]). (since Compat 3.16)

* `contains(haystack, needle)` and its one argument partially applied form `contains(haystack)` have been added, it acts like `occursin(needle, haystack)` ([#35132]). (since Compat 3.15)
Expand Down Expand Up @@ -200,3 +202,4 @@ Note that you should specify the correct minimum version for `Compat` in the
[#35132]: https://github.com/JuliaLang/julia/pull/35132
[#35052]: https://github.com/JuliaLang/julia/pull/35052
[#37244]: https://github.com/JuliaLang/julia/pull/37244
[#37517]: https://github.com/JuliaLang/julia/pull/37517
31 changes: 31 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,37 @@ 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-DEV.1037"
export ComposedFunction
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added export since Base now exports ComposedFunction: JuliaLang/julia#37517 (comment)

# 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, g) = ComposedFunction{Core.Typeof(f),Core.Typeof(g)}(f, g)
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

# https://github.com/JuliaLang/julia/pull/37244
if VERSION < v"1.6.0-DEV.873" # 18198b1bf85125de6cec266eac404d31ccc2e65c
export addenv
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-DEV.1037"
@test c.f === sin
@test c.g === cos
@test propertynames(c) == (:f, :g, :outer, :inner)
else
@test propertynames(c) == (:outer, :inner)
end
end

# From spawn.jl
shcmd = `sh`
havebb = false
Expand Down