Skip to content

Commit a28e553

Browse files
authored
Remove single-argument methods for map, foreach, Iterators.map (#52631)
In the case of `map` and `foreach`, this removes awkward functionality. `Iterators.map(::Any)`, on the other hand, already threw.
1 parent 8c49e5e commit a28e553

File tree

8 files changed

+25
-17
lines changed

8 files changed

+25
-17
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ Standard library changes
171171
Deprecated or removed
172172
---------------------
173173

174+
* `Base.map`, `Iterators.map`, and `foreach` lost their single-argument methods ([#52631]).
175+
174176

175177
External dependencies
176178
---------------------

base/abstractarray.jl

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3108,9 +3108,8 @@ julia> foreach((x, y) -> println(x, " with ", y), tri, 'a':'z')
31083108
7 with c
31093109
```
31103110
"""
3111-
foreach(f) = (f(); nothing)
31123111
foreach(f, itr) = (for x in itr; f(x); end; nothing)
3113-
foreach(f, itrs...) = (for z in zip(itrs...); f(z...); end; nothing)
3112+
foreach(f, itr, itrs...) = (for z in zip(itr, itrs...); f(z...); end; nothing)
31143113

31153114
## map over arrays ##
31163115

@@ -3282,10 +3281,6 @@ end
32823281
concatenate_setindex!(R, v, I...) = (R[I...] .= (v,); R)
32833282
concatenate_setindex!(R, X::AbstractArray, I...) = (R[I...] = X)
32843283

3285-
## 0 arguments
3286-
3287-
map(f) = f()
3288-
32893284
## 1 argument
32903285

32913286
function map!(f::F, dest::AbstractArray, A::AbstractArray) where F
@@ -3421,7 +3416,7 @@ julia> map(+, [1 2; 3 4], [1,10,100,1000], zeros(3,1)) # iterates until 3rd is
34213416
102.0
34223417
```
34233418
"""
3424-
map(f, iters...) = collect(Generator(f, iters...))
3419+
map(f, it, iters...) = collect(Generator(f, it, iters...))
34253420

34263421
# multi-item push!, pushfirst! (built on top of type-specific 1-item version)
34273422
# (note: must not cause a dispatch loop when 1-item case is not defined)

base/iterators.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ julia> collect(Iterators.map(x -> x^2, 1:3))
5959
9
6060
```
6161
"""
62-
map(f, args...) = Base.Generator(f, args...)
62+
map(f, arg, args...) = Base.Generator(f, arg, args...)
6363

6464
tail_if_any(::Tuple{}) = ()
6565
tail_if_any(x::Tuple) = tail(x)

base/tuple.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ end
321321
# n argument function
322322
heads(ts::Tuple...) = map(t -> t[1], ts)
323323
tails(ts::Tuple...) = map(tail, ts)
324-
map(f, ::Tuple{}...) = ()
324+
map(f, ::Tuple{}, ::Tuple{}...) = ()
325325
anyempty(x::Tuple{}, xs...) = true
326326
anyempty(x::Tuple, xs...) = anyempty(xs...)
327327
anyempty() = false
@@ -615,4 +615,4 @@ Return an empty tuple, `()`.
615615
empty(@nospecialize x::Tuple) = ()
616616

617617
foreach(f, itr::Tuple) = foldl((_, x) -> (f(x); nothing), itr, init=nothing)
618-
foreach(f, itrs::Tuple...) = foldl((_, xs) -> (f(xs...); nothing), zip(itrs...), init=nothing)
618+
foreach(f, itr::Tuple, itrs::Tuple...) = foldl((_, xs) -> (f(xs...); nothing), zip(itr, itrs...), init=nothing)

stdlib/LinearAlgebra/src/adjtrans.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,14 @@ hcat(tvs::Transpose{T,Vector{T}}...) where {T} = _transpose_hcat(tvs...)
396396
#
397397
# note that the caller's operation f operates in the domain of the wrapped vectors' entries.
398398
# hence the adjoint->f->adjoint shenanigans applied to the parent vectors' entries.
399-
map(f, avs::AdjointAbsVec...) = adjoint(map((xs...) -> adjoint(f(adjoint.(xs)...)), parent.(avs)...))
400-
map(f, tvs::TransposeAbsVec...) = transpose(map((xs...) -> transpose(f(transpose.(xs)...)), parent.(tvs)...))
399+
function map(f, av::AdjointAbsVec, avs::AdjointAbsVec...)
400+
s = (av, avs...)
401+
adjoint(map((xs...) -> adjoint(f(adjoint.(xs)...)), parent.(s)...))
402+
end
403+
function map(f, tv::TransposeAbsVec, tvs::TransposeAbsVec...)
404+
s = (tv, tvs...)
405+
transpose(map((xs...) -> transpose(f(transpose.(xs)...)), parent.(s)...))
406+
end
401407
quasiparentt(x) = parent(x); quasiparentt(x::Number) = x # to handle numbers in the defs below
402408
quasiparenta(x) = parent(x); quasiparenta(x::Number) = conj(x) # to handle numbers in the defs below
403409
quasiparentc(x) = parent(parent(x)); quasiparentc(x::Number) = conj(x) # to handle numbers in the defs below

test/abstractarray.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -822,9 +822,8 @@ Base.getindex(A::TSlowNIndexes{T,2}, i::Int, j::Int) where {T} = A.data[i,j]
822822
@test isa(map(Set, Array[[1,2],[3,4]]), Vector{Set{Int}})
823823
end
824824

825-
@testset "mapping over scalars and empty arguments:" begin
825+
@testset "mapping over scalars" begin
826826
@test map(sin, 1) === sin(1)
827-
@test map(()->1234) === 1234
828827
end
829828

830829
function test_UInt_indexing(::Type{TestAbstractArray})

test/functional.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ end
5252

5353
# foreach
5454
let a = []
55-
foreach(()->push!(a,0))
56-
@test a == [0]
57-
a = []
5855
foreach(x->push!(a,x), [1,5,10])
5956
@test a == [1,5,10]
6057
a = []

test/iterators.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,15 @@ end
10101010
@test collect(Iterators.partition(lstrip("01111", '0'), 2)) == ["11", "11"]
10111011
end
10121012

1013+
@testset "no single-argument map methods" begin
1014+
maps = (tuple, Returns(nothing), (() -> nothing))
1015+
mappers = (Iterators.map, map, foreach)
1016+
for f maps, m mappers
1017+
@test !applicable(m, f)
1018+
@test !hasmethod(m, Tuple{typeof(f)})
1019+
end
1020+
end
1021+
10131022
@testset "Iterators docstrings" begin
10141023
@test isempty(Docs.undocumented_names(Iterators))
10151024
end

0 commit comments

Comments
 (0)