From 21b2df95a6407d85bb8d025ced1117a4c57e95b2 Mon Sep 17 00:00:00 2001 From: Martin Holters Date: Wed, 18 Jan 2023 14:21:20 +0100 Subject: [PATCH] Add `splat` --- Project.toml | 2 +- README.md | 9 +++++--- src/Compat.jl | 55 +++++++++++++++++------------------------------- test/runtests.jl | 16 ++++++++++---- 4 files changed, 38 insertions(+), 44 deletions(-) diff --git a/Project.toml b/Project.toml index c0acbfdfe..b5a31709f 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "Compat" uuid = "34da2185-b29b-5c13-b0c7-acf172513d20" -version = "4.5.0" +version = "4.6.0" [deps] Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" diff --git a/README.md b/README.md index e26ae5d24..acd57b55e 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ changes in `julia`. ## Supported features -* `Splat(f)` which is equivalent to `args -> f(args...)`. ([#42717]) (since Compat 4.5.0) +* `splat(f)` which is equivalent to `args -> f(args...)`. ([#42717], [#48038]) (since Compat 4.6.0) (Note: for historical reasons, `Compat` on Julia before v1.9 also exports `Splat`; its usage is discouraged, however.) * `Compat.@assume_effects setting... ex` overrides the compiler's effect modeling for the method definition `ex` on Julia versions that support this feature. Julia version without support just pass back `ex`. ([#43852]) (since Compat 4.4.0) @@ -142,6 +142,7 @@ is at least this version by `VERSION >= v"X.Y.Z-aaa+NNNN"`. Note that you should specify the correct minimum version for `Compat` in the `[compat]` section of your `Project.toml`, as given in above list. +[`@bdf9ead9`]: https://github.com/JuliaLang/julia/commit/bdf9ead91e5a8dfd91643a17c1626032faada329 [#29901]: https://github.com/JuliaLang/julia/issues/29901 [#35316]: https://github.com/JuliaLang/julia/issues/35316 [#36229]: https://github.com/JuliaLang/julia/issues/36229 @@ -157,6 +158,8 @@ Note that you should specify the correct minimum version for `Compat` in the [#41312]: https://github.com/JuliaLang/julia/issues/41312 [#42125]: https://github.com/JuliaLang/julia/issues/42125 [#42351]: https://github.com/JuliaLang/julia/issues/42351 -[#43354]: https://github.com/JuliaLang/julia/issues/43354 +[#42717]: https://github.com/JuliaLang/julia/issues/42717 [#43334]: https://github.com/JuliaLang/julia/issues/43334 -[`@bdf9ead9`]: https://github.com/JuliaLang/julia/commit/bdf9ead91e5a8dfd91643a17c1626032faada329 +[#43354]: https://github.com/JuliaLang/julia/issues/43354 +[#43852]: https://github.com/JuliaLang/julia/issues/43852 +[#48038]: https://github.com/JuliaLang/julia/issues/48038 diff --git a/src/Compat.jl b/src/Compat.jl index 4ed013dd4..0c8b1a3e6 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -616,44 +616,27 @@ if VERSION < v"1.9.0-DEV.1163" _empty_stack(_...) = throw(ArgumentError("`stack` on an empty collection is not allowed")) end - -@static if VERSION < v"1.9.0-DEV.513" - # https://github.com/JuliaLang/julia/pull/42717 - export Splat - - struct Splat{F} <: Function - f::F - Splat(f) = new{Core.Typeof(f)}(f) + +if v"1.10.0-" <= VERSION < v"1.10.0-DEV.360" || VERSION < v"1.9.0-beta3" + if VERSION < v"1.9.0-DEV.513" + # https://github.com/JuliaLang/julia/pull/42717 + export Splat # Base does not export this, but we have to keep it for compatibility + + struct Splat{F} <: Function + f::F + Splat(f) = new{Core.Typeof(f)}(f) + end + + (s::Splat)(args) = s.f(args...) + Base.print(io::IO, s::Splat) = print(io, "splat(", s.f, ')') + Base.show(io::IO, s::Splat) = print(io, s) + Base.show(io::IO, ::MIME"text/plain", s::Splat) = show(io, s) end - (s::Splat)(args) = s.f(args...) - Base.print(io::IO, s::Splat) = print(io, "Splat(", s.f, ')') - Base.show(io::IO, s::Splat) = print(io, s) - Base.show(io::IO, ::MIME"text/plain", s::Splat) = show(io, s) - @doc """ - Splat(f) -Equivalent to -```julia - my_splat(f) = args->f(args...) -``` -i.e. given a function returns a new function that takes one argument and splats -its argument into the original function. This is useful as an adaptor to pass -a multi-argument function in a context that expects a single argument, but -passes a tuple as that single argument. Additionally has pretty printing. -# Example usage: -```jldoctest -julia> map(Base.Splat(+), zip(1:3,4:6)) -3-element Vector{Int64}: - 5 - 7 - 9 -julia> my_add = Base.Splat(+) -Splat(+) -julia> my_add((1,2,3)) -6 -``` -""" Splat -end + # https://github.com/JuliaLang/julia/pull/48038 + export splat + splat(f) = Splat(f) +end include("deprecated.jl") diff --git a/test/runtests.jl b/test/runtests.jl index 7010f5bf6..ce25e5920 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -578,8 +578,16 @@ end end end -@testset "Splat" begin - @test Splat(+)((1,2,3)) == 6 - @test repr(Splat(+)) == "Splat(+)" - @test repr(MIME"text/plain"(), Splat(+)) == "Splat(+)" +@testset "splat" begin + @test splat(+)((1,2,3)) == 6 + + if v"1.10.0-" <= VERSION < v"1.10.0-DEV.360" || + v"1.9.0-DEV.513" <= VERSION < v"1.9.0-beta3" + # these versions of Base export Splat (which we use) but pretty-print with capital `S` + @test repr(splat(+)) == "Splat(+)" + @test repr(MIME"text/plain"(), splat(+)) == "Splat(+)" + else + @test repr(splat(+)) == "splat(+)" + @test repr(MIME"text/plain"(), splat(+)) == "splat(+)" + end end