Skip to content

Commit

Permalink
Add splat
Browse files Browse the repository at this point in the history
  • Loading branch information
martinholters committed Jan 18, 2023
1 parent 295c146 commit 24e3081
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 44 deletions.
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.5.0"
version = "4.6.0"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -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
55 changes: 19 additions & 36 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")

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

0 comments on commit 24e3081

Please sign in to comment.