Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -873,10 +873,26 @@ julia> ∘(fs...)(3)
```
"""
function ∘ end

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)
end

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

∘(f) = f
∘(f, g) = (x...)->f(g(x...))
∘(f, g) = ComposedFunction(f, g)
∘(f, g, h...) = ∘(f ∘ g, h...)

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

"""
!f::Function

Expand Down
2 changes: 2 additions & 0 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ function show(io::IO, ::MIME"text/plain", f::Function)
end
end

show(io::IO, ::MIME"text/plain", c::ComposedFunction) = show(io, c)

function show(io::IO, ::MIME"text/plain", iter::Union{KeySet,ValueIterator})
isempty(iter) && get(io, :compact, false) && return show(io, iter)
summary(io, iter)
Expand Down
2 changes: 2 additions & 0 deletions base/some.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ struct Some{T}
value::T
end

Some(::Type{T}) where {T} = Some{Type{T}}(T)

promote_rule(::Type{Some{T}}, ::Type{Some{S}}) where {T, S<:T} = Some{T}

nonnothingtype(::Type{T}) where {T} = Core.Compiler.typesubtract(T, Nothing)
Expand Down
2 changes: 2 additions & 0 deletions test/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ Base.promote_rule(::Type{T19714}, ::Type{Int}) = T19714
@test ∘(FreeMagma(1), FreeMagma(2)) === FreeMagma((1,2))
@test ∘(FreeMagma(1), FreeMagma(2), FreeMagma(3)) === FreeMagma(((1,2), 3))
@test ∘(FreeMagma(1), FreeMagma(2), FreeMagma(3), FreeMagma(4)) === FreeMagma((((1,2), 3), 4))

@test fieldtypes(typeof(Float64 ∘ Int)) == (Type{Float64}, Type{Int})
end

@testset "function negation" begin
Expand Down
3 changes: 3 additions & 0 deletions test/some.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,6 @@ using Base: notnothing
# isnothing()
@test !isnothing(1)
@test isnothing(nothing)

# type stability
@test fieldtype(typeof(Some(Int)), 1) === Type{Int}