From bc4e8ce32c9289ba936b1e1d6a54b72cbb238080 Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Tue, 15 Oct 2019 21:58:52 -0700 Subject: [PATCH] =?UTF-8?q?Add=20ASCII=20alias=20`compose`=20of=20`?= =?UTF-8?q?=E2=88=98`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NEWS.md | 2 ++ base/exports.jl | 1 + base/operators.jl | 7 ++++++- test/operators.jl | 1 + 4 files changed, 10 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index f7afd2de594345..b8f07fe06135d6 100644 --- a/NEWS.md +++ b/NEWS.md @@ -11,6 +11,8 @@ New language features * 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`. + Language changes ---------------- diff --git a/base/exports.jl b/base/exports.jl index 49063720c14c47..80fd9d6c76fb10 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 232f6407ed6f5d..61913fd50bc66e 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> ∘(fs...) === compose(fs...) +true ``` """ ∘(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 58102d7d65a328..699f98aa388f28 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)