Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Splat #785

Merged
merged 8 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
38 changes: 38 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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