From ee09e5d0656603d51a6215433fad9c20d2d57e64 Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Tue, 15 Oct 2019 22:32:02 -0700 Subject: [PATCH] Function composition for multiple functions (JuliaLang/julia#33568) --- README.md | 4 ++++ src/Compat.jl | 5 +++++ test/runtests.jl | 7 +++++++ 3 files changed, 16 insertions(+) diff --git a/README.md b/README.md index 0b1a801d4..113cee93e 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,9 @@ Currently, the `@compat` macro supports the following syntaxes: ## Other changes +* Function composition now supports multiple functions: `∘(f, g, h) = f ∘ g ∘ h` + and splatting `∘(fs...)` for composing an iterable collection of functions ([#33568]). + ## New types ## Developer tips @@ -127,3 +130,4 @@ includes this fix. Find the minimum version from there. [#29749]: https://github.com/JuliaLang/julia/issues/29749 [#32628]: https://github.com/JuliaLang/julia/issues/32628 [#33129]: https://github.com/JuliaLang/julia/issues/33129 +[#33568]: https://github.com/JuliaLang/julia/pull/33568 diff --git a/src/Compat.jl b/src/Compat.jl index e9d2d2fb6..e4946a6d2 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -88,6 +88,11 @@ if VERSION < v"1.3.0-alpha.8" Base.mod(i::Integer, r::AbstractUnitRange{<:Integer}) = mod(i-first(r), length(r)) + first(r) end +# https://github.com/JuliaLang/julia/pull/33568 +if VERSION < v"1.4.0-DEV.329" + Base.:∘(f, g, h...) = ∘(f ∘ g, h...) +end + include("deprecated.jl") end # module Compat diff --git a/test/runtests.jl b/test/runtests.jl index af6b808ee..6e1f8b77c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -90,4 +90,11 @@ end @test_throws DivideError mod(3, 1:0) end +# https://github.com/JuliaLang/julia/pull/33568 +@testset "function composition" begin + @test ∘(x -> x-2, x -> x-3, x -> x+5)(7) == 7 + fs = [x -> x[1:2], uppercase, lowercase] + @test ∘(fs...)("ABC") == "AB" +end + nothing