diff --git a/NEWS.md b/NEWS.md index 4ffe14249e5d7..c2c9039a932f8 100644 --- a/NEWS.md +++ b/NEWS.md @@ -8,9 +8,6 @@ New language features * `import` now allows quoted symbols, e.g. `import Base.:+` ([#33158]). -* Function composition now supports multiple functions: `∘(f, g, h) = f ∘ g ∘ h` -and splatting `∘(fs...)` for composing an iterable collection of functions ([#33568]). - Language changes ---------------- @@ -36,6 +33,9 @@ New library functions * `readdir` output is now guaranteed to be sorted. The `sort` keyword allows opting out of sorting to get names in OS-native order ([#33542]). * The new `only(x)` function returns the one-and-only element of a collection `x`, and throws an `ArgumentError` if `x` contains zero or multiple elements. ([#33129]) * `takewhile` and `dropwhile` have been added to the Iterators submodule ([#33437]). +* Function composition now supports multiple functions: `∘(f, g, h) = f ∘ g ∘ h` + and splatting `∘(fs...)` for composing an iterable collection of functions ([#33568]). +* Function composition `∘` now has an ASCII alias `compose` ([#33573]). Standard library changes diff --git a/base/exports.jl b/base/exports.jl index 49063720c14c4..80fd9d6c76fb1 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -763,6 +763,7 @@ export # misc atexit, atreplinit, + compose, exit, ntuple, diff --git a/base/operators.jl b/base/operators.jl index e6af7dad7eb6d..dc44ce230b72a 100644 --- a/base/operators.jl +++ b/base/operators.jl @@ -823,6 +823,7 @@ julia> [1:5;] |> x->x.^2 |> sum |> inv """ f ∘ g + compose(f, g) Compose functions: i.e. `(f ∘ g)(args...)` means `f(g(args...))`. The `∘` symbol can be entered in the Julia REPL (and most editors, appropriately configured) by typing `\\circ`. @@ -832,7 +833,7 @@ The prefix form supports composition of multiple functions: `∘(f, g, h) = f and splatting `∘(fs...)` for composing an iterable collection of functions. !!!compat "Julia 1.4" - Multiple function composition requires at least Julia 1.4. + Multiple function composition and ASCII alias `compose` require at least Julia 1.4. # Examples ```jldoctest @@ -851,10 +852,14 @@ julia> fs = [ julia> ∘(fs...)(3) 3.0 + +julia> compose(fs...)(3) +3.0 ``` """ ∘(f, g) = (x...)->f(g(x...)) ∘(f, g, h...) = ∘(f ∘ g, h...) +const compose = ∘ """ !f::Function diff --git a/test/operators.jl b/test/operators.jl index 58102d7d65a32..699f98aa388f2 100644 --- a/test/operators.jl +++ b/test/operators.jl @@ -111,6 +111,7 @@ Base.promote_rule(::Type{T19714}, ::Type{Int}) = T19714 @test ∘(x -> x-2, x -> x-3, x -> x+5)(7) == 7 fs = [x -> x[1:2], uppercase, lowercase] @test ∘(fs...)("ABC") == "AB" + @test ∘(fs...) === compose(fs...) end @testset "function negation" begin str = randstring(20)