Skip to content

Commit

Permalink
add Splat (#785)
Browse files Browse the repository at this point in the history
  • Loading branch information
MasonProtter authored Nov 29, 2022
1 parent abce98e commit 295c146
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
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

2 comments on commit 295c146

@martinholters
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/73064

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v4.5.0 -m "<description of version>" 295c146528063385a0d89bc2be12a7f534052d82
git push origin v4.5.0

Please sign in to comment.