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
9 changes: 6 additions & 3 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,7 @@ julia> [1:5;] |> x->x.^2 |> sum |> inv
"""
f ∘ g

Compose functions: i.e. `(f ∘ g)(args...)` means `f(g(args...))`. The `∘` symbol can be
Compose functions: i.e. `(f ∘ g)(args...; kwargs...)` means `f(g(args...; kwargs...))`. The `∘` symbol can be
entered in the Julia REPL (and most editors, appropriately configured) by typing `\\circ<tab>`.

Function composition also works in prefix form: `∘(f, g)` is the same as `f ∘ g`.
Expand All @@ -911,7 +911,10 @@ and splatting `∘(fs...)` for composing an iterable collection of functions.
Multiple function composition requires at least Julia 1.4.

!!! compat "Julia 1.5"
Composition of one function ∘(f) requires at least Julia 1.5.
Composition of one function ∘(f) requires at least Julia 1.5.

!!! compat "Julia 1.7"
Using keyword arguments requires at least Julia 1.7.

# Examples
```jldoctest
Expand Down Expand Up @@ -973,7 +976,7 @@ struct ComposedFunction{O,I} <: Function
ComposedFunction(outer, inner) = new{Core.Typeof(outer),Core.Typeof(inner)}(outer, inner)
end

(c::ComposedFunction)(x...) = c.outer(c.inner(x...))
(c::ComposedFunction)(x...; kw...) = c.outer(c.inner(x...; kw...))

∘(f) = f
∘(f, g) = ComposedFunction(f, g)
Expand Down
5 changes: 5 additions & 0 deletions test/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ Base.promote_rule(::Type{T19714}, ::Type{Int}) = T19714

@test repr(uppercase ∘ first) == "uppercase ∘ first"
@test sprint(show, "text/plain", uppercase ∘ first) == "uppercase ∘ first"

# test keyword ags in composition
function kwf(a;b,c); a + b + c; end
@test (abs2 ∘ kwf)(1,b=2,c=3) == 36

end

@testset "function negation" begin
Expand Down