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

Expose ComposedFunction as a public API #37517

Merged
merged 15 commits into from
Sep 24, 2020
Prev Previous commit
Next Next commit
Give more descriptive names to the fields of ComposedFunction
  • Loading branch information
jw3126 committed Sep 10, 2020
commit d7195b81174f207996f69d6c7e467174ea950346
24 changes: 12 additions & 12 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -876,9 +876,9 @@ See also [Base.ComposedFunction](@ref).
function ∘ end

"""
Base.ComposedFunction{F,G} <: Function
Base.ComposedFunction{Outer,Inner} <: Function

Represents the composition of two callable objects `f::F` and `g::G`.
Represents the composition of two callable objects `f::Outer` and `g::Inner`.
```jldoctest
julia> sin ∘ cos === Base.ComposedFunction(sin, cos)
true
Expand All @@ -891,34 +891,34 @@ The composed pieces are stored in the fields of `ComposedFunction` and can be re
julia> composition = sin ∘ cos
sin ∘ cos

julia> composition.f === sin
julia> composition.outer === sin
true

julia> composition.g === cos
julia> composition.inner === cos
true
```
!!! compat "Julia 1.6"
ComposedFunction requires at least Julia 1.6. In earlier version `∘` returns an anonymous function instead.

See also [`∘`](@ref).
"""
struct ComposedFunction{F,G} <: Function
f::F
g::G
ComposedFunction{F, G}(f, g) where {F, G} = new{F, G}(f, g)
ComposedFunction(f, g) = new{Core.Typeof(f),Core.Typeof(g)}(f, g)
struct ComposedFunction{O,I} <: Function
Copy link
Contributor

Choose a reason for hiding this comment

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

Hi, This is coming very late.

Just wanted to find out if it is necessary to restrict O and I to be subtypes of Base.Callable as in

struct ComposedFunction{O<:Base.Callable, I<:Base.Callable} <: Function

Copy link
Member

Choose a reason for hiding this comment

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

There is no reason to restrict to Callable here. There are lots of callable objects that aren't a subtype of Callable and this would break those cases.

outer::O
inner::I
ComposedFunction{O, I}(outer, inner) where {O, I} = new{O, I}(outer, inner)
ComposedFunction(outer, inner) = new{Core.Typeof(outer),Core.Typeof(inner)}(outer, inner)
end

(c::ComposedFunction)(x...) = c.f(c.g(x...))
(c::ComposedFunction)(x...) = c.outer(c.inner(x...))

∘(f) = f
∘(f, g) = ComposedFunction(f, g)
∘(f, g, h...) = ∘(f ∘ g, h...)

function show(io::IO, c::ComposedFunction)
show(io, c.f)
show(io, c.outer)
print(io, " ∘ ")
show(io, c.g)
show(io, c.inner)
end

"""
Expand Down