diff --git a/Project.toml b/Project.toml index f808b5df9..c0acbfdfe 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "Compat" uuid = "34da2185-b29b-5c13-b0c7-acf172513d20" -version = "4.4.0" +version = "4.5.0" [deps] Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" diff --git a/README.md b/README.md index 3d1585ce6..e26ae5d24 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,8 @@ changes in `julia`. ## Supported features +* `Splat(f)` which is equivalent to `args -> f(args...)`. ([#42717]) (since Compat 4.5.0) + * `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) * `div`, `lcm`, `gcd`, `/`, `rem`, and `mod` will `promote` heterogenous `Dates.Period`s ([`@bdf9ead9`]). (since Compat 4.3.0) diff --git a/src/Compat.jl b/src/Compat.jl index 42a883a01..4ed013dd4 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -616,6 +616,44 @@ 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) + 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 include("deprecated.jl") diff --git a/test/runtests.jl b/test/runtests.jl index 7cd9ca40b..7010f5bf6 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -577,3 +577,9 @@ end @test op(xms, yms) == op(xms, ys) == op(xs, yms) end end + +@testset "Splat" begin + @test Splat(+)((1,2,3)) == 6 + @test repr(Splat(+)) == "Splat(+)" + @test repr(MIME"text/plain"(), Splat(+)) == "Splat(+)" +end