Skip to content

Commit 21a7d4d

Browse files
committedApr 22, 2016
Merge pull request #15804 from martinholters/depfunctors
Defunctorize
2 parents 5c9b7a6 + 0429c38 commit 21a7d4d

33 files changed

+332
-603
lines changed
 

‎Makefile

-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ CORE_SRCS := $(addprefix $(JULIAHOME)/, \
193193
base/essentials.jl \
194194
base/generator.jl \
195195
base/expr.jl \
196-
base/functors.jl \
197196
base/hashing.jl \
198197
base/inference.jl \
199198
base/int.jl \

‎base/arraymath.jl

+8-26
Original file line numberDiff line numberDiff line change
@@ -56,67 +56,49 @@ promote_array_type(F, ::Type{Bool}, ::Type{Bool}) = promote_op(F, Bool, Bool)
5656
.^(X::AbstractArray, y::Number ) =
5757
reshape([ x ^ y for x in X ], size(X))
5858

59-
for (f,F) in ((:+, AddFun()),
60-
(:-, SubFun()),
61-
(:div, IDivFun()),
62-
(:mod, ModFun()),
63-
(:&, AndFun()),
64-
(:|, OrFun()),
65-
(:$, XorFun()))
59+
for f in (:+, :-, :div, :mod, :&, :|, :$)
6660
@eval begin
6761
function ($f){S,T}(A::Range{S}, B::Range{T})
68-
F = similar(A, promote_op($F,S,T), promote_shape(size(A),size(B)))
62+
F = similar(A, promote_op($f,S,T), promote_shape(size(A),size(B)))
6963
for (iF, iA, iB) in zip(eachindex(F), eachindex(A), eachindex(B))
7064
@inbounds F[iF] = ($f)(A[iA], B[iB])
7165
end
7266
return F
7367
end
7468
function ($f){S,T}(A::AbstractArray{S}, B::Range{T})
75-
F = similar(A, promote_op($F,S,T), promote_shape(size(A),size(B)))
69+
F = similar(A, promote_op($f,S,T), promote_shape(size(A),size(B)))
7670
for (iF, iA, iB) in zip(eachindex(F), eachindex(A), eachindex(B))
7771
@inbounds F[iF] = ($f)(A[iA], B[iB])
7872
end
7973
return F
8074
end
8175
function ($f){S,T}(A::Range{S}, B::AbstractArray{T})
82-
F = similar(B, promote_op($F,S,T), promote_shape(size(A),size(B)))
76+
F = similar(B, promote_op($f,S,T), promote_shape(size(A),size(B)))
8377
for (iF, iA, iB) in zip(eachindex(F), eachindex(A), eachindex(B))
8478
@inbounds F[iF] = ($f)(A[iA], B[iB])
8579
end
8680
return F
8781
end
8882
function ($f){S,T}(A::AbstractArray{S}, B::AbstractArray{T})
89-
F = similar(A, promote_op($F,S,T), promote_shape(size(A),size(B)))
83+
F = similar(A, promote_op($f,S,T), promote_shape(size(A),size(B)))
9084
for (iF, iA, iB) in zip(eachindex(F), eachindex(A), eachindex(B))
9185
@inbounds F[iF] = ($f)(A[iA], B[iB])
9286
end
9387
return F
9488
end
9589
end
9690
end
97-
for (f,F) in ((:.+, DotAddFun()),
98-
(:.-, DotSubFun()),
99-
(:.*, DotMulFun()),
100-
(:, DotIDivFun()),
101-
(:.%, DotRemFun()),
102-
(:.<<, DotLSFun()),
103-
(:.>>, DotRSFun()),
104-
(:div, IDivFun()),
105-
(:mod, ModFun()),
106-
(:rem, RemFun()),
107-
(:&, AndFun()),
108-
(:|, OrFun()),
109-
(:$, XorFun()))
91+
for f in (:.+, :.-, :.*, :, :.%, :.<<, :.>>, :div, :mod, :rem, :&, :|, :$)
11092
@eval begin
11193
function ($f){T}(A::Number, B::AbstractArray{T})
112-
F = similar(B, promote_array_type($F,typeof(A),T))
94+
F = similar(B, promote_array_type($f,typeof(A),T))
11395
for (iF, iB) in zip(eachindex(F), eachindex(B))
11496
@inbounds F[iF] = ($f)(A, B[iB])
11597
end
11698
return F
11799
end
118100
function ($f){T}(A::AbstractArray{T}, B::Number)
119-
F = similar(A, promote_array_type($F,typeof(B),T))
101+
F = similar(A, promote_array_type($f,typeof(B),T))
120102
for (iF, iA) in zip(eachindex(F), eachindex(A))
121103
@inbounds F[iF] = ($f)(A[iA], B)
122104
end

‎base/base.jl

-2
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ gc_enable(on::Bool) = ccall(:jl_gc_enable, Cint, (Cint,), on)!=0
8484

8585
bytestring(str::ByteString) = str
8686

87-
identity(x) = x
88-
8987
# used by { } syntax
9088
function cell_1d(xs::ANY...)
9189
n = length(xs)

‎base/bitarray.jl

+29-18
Original file line numberDiff line numberDiff line change
@@ -1047,12 +1047,11 @@ for f in (:+, :-)
10471047
return r
10481048
end
10491049
end
1050-
for (f,F) in ((:.+, DotAddFun()),
1051-
(:.-, DotSubFun()))
1050+
for (f) in (:.+, :.-)
10521051
for (arg1, arg2, T, fargs) in ((:(B::BitArray), :(x::Bool) , Int , :(b, x)),
1053-
(:(B::BitArray), :(x::Number) , :(promote_array_type($F, typeof(x), Bool)), :(b, x)),
1052+
(:(B::BitArray), :(x::Number) , :(promote_array_type($f, typeof(x), Bool)), :(b, x)),
10541053
(:(x::Bool) , :(B::BitArray), Int , :(x, b)),
1055-
(:(x::Number) , :(B::BitArray), :(promote_array_type($F, typeof(x), Bool)), :(x, b)))
1054+
(:(x::Number) , :(B::BitArray), :(promote_array_type($f, typeof(x), Bool)), :(x, b)))
10561055
@eval function ($f)($arg1, $arg2)
10571056
r = Array($T, size(B))
10581057
bi = start(B)
@@ -1091,7 +1090,7 @@ function div(x::Bool, B::BitArray)
10911090
end
10921091
function div(x::Number, B::BitArray)
10931092
all(B) || throw(DivideError())
1094-
pt = promote_array_type(IDivFun(), typeof(x), Bool)
1093+
pt = promote_array_type(div, typeof(x), Bool)
10951094
y = div(x, true)
10961095
reshape(pt[ y for i = 1:length(B) ], size(B))
10971096
end
@@ -1112,16 +1111,15 @@ function mod(x::Bool, B::BitArray)
11121111
end
11131112
function mod(x::Number, B::BitArray)
11141113
all(B) || throw(DivideError())
1115-
pt = promote_array_type(ModFun(), typeof(x), Bool)
1114+
pt = promote_array_type(mod, typeof(x), Bool)
11161115
y = mod(x, true)
11171116
reshape(pt[ y for i = 1:length(B) ], size(B))
11181117
end
11191118

1120-
for (f,F) in ((:div, IDivFun()),
1121-
(:mod, ModFun()))
1119+
for f in (:div, :mod)
11221120
@eval begin
11231121
function ($f)(B::BitArray, x::Number)
1124-
F = Array(promote_array_type($F, typeof(x), Bool), size(B))
1122+
F = Array(promote_array_type($f, typeof(x), Bool), size(B))
11251123
for i = 1:length(F)
11261124
F[i] = ($f)(B[i], x)
11271125
end
@@ -1676,7 +1674,7 @@ end
16761674

16771675
## Reductions ##
16781676

1679-
sum(A::BitArray, region) = reducedim(AddFun(), A, region)
1677+
sum(A::BitArray, region) = reducedim(+, A, region)
16801678
sum(B::BitArray) = countnz(B)
16811679

16821680
function all(B::BitArray)
@@ -1711,19 +1709,32 @@ maximum(B::BitArray) = isempty(B) ? throw(ArgumentError("argument must be non-em
17111709
# arrays since there can be a 64x speedup by working at the level of Int64
17121710
# instead of looping bit-by-bit.
17131711

1714-
map(f::Function, A::BitArray) = map(specialized_bitwise_unary(f), A)
1715-
map(f::Function, A::BitArray, B::BitArray) = map(specialized_bitwise_binary(f), A, B)
1716-
map(f::BitFunctorUnary, A::BitArray) = map!(f, similar(A), A)
1717-
map(f::BitFunctorBinary, A::BitArray, B::BitArray) = map!(f, similar(A), A, B)
1712+
map(f::Function, A::BitArray) = map!(f, similar(A), A)
1713+
map(f::Function, A::BitArray, B::BitArray) = map!(f, similar(A), A, B)
17181714

17191715
map!(f, A::BitArray) = map!(f, A, A)
1720-
map!(f::Function, dest::BitArray, A::BitArray) = map!(specialized_bitwise_unary(f), dest, A)
1721-
map!(f::Function, dest::BitArray, A::BitArray, B::BitArray) = map!(specialized_bitwise_binary(f), dest, A, B)
1716+
map!(f::typeof(!), dest::BitArray, A::BitArray) = map!(~, dest, A)
1717+
map!(f::typeof(zero), dest::BitArray, A::BitArray) = fill!(dest, false)
1718+
map!(f::typeof(one), dest::BitArray, A::BitArray) = fill!(dest, true)
1719+
1720+
immutable BitChunkFunctor{F<:Function}
1721+
f::F
1722+
end
1723+
(f::BitChunkFunctor)(x, y) = f.f(x,y)
1724+
1725+
map!(f::Union{typeof(*), typeof(min)}, dest::BitArray, A::BitArray, B::BitArray) = map!(&, dest, A, B)
1726+
map!(f::typeof(max), dest::BitArray, A::BitArray, B::BitArray) = map!(|, dest, A, B)
1727+
map!(f::typeof(!=), dest::BitArray, A::BitArray, B::BitArray) = map!($, dest, A, B)
1728+
map!(f::Union{typeof(>=), typeof(^)}, dest::BitArray, A::BitArray, B::BitArray) = map!(BitChunkFunctor((p, q) -> p | ~q), dest, A, B)
1729+
map!(f::typeof(<=), dest::BitArray, A::BitArray, B::BitArray) = map!(BitChunkFunctor((p, q) -> ~p | q), dest, A, B)
1730+
map!(f::typeof(==), dest::BitArray, A::BitArray, B::BitArray) = map!(BitChunkFunctor((p, q) -> ~(p $ q)), dest, A, B)
1731+
map!(f::typeof(<), dest::BitArray, A::BitArray, B::BitArray) = map!(BitChunkFunctor((p, q) -> ~p & q), dest, A, B)
1732+
map!(f::typeof(>), dest::BitArray, A::BitArray, B::BitArray) = map!(BitChunkFunctor((p, q) -> p & ~q), dest, A, B)
17221733

17231734
# If we were able to specialize the function to a known bitwise operation,
17241735
# map across the chunks. Otherwise, fall-back to the AbstractArray method that
17251736
# iterates bit-by-bit.
1726-
function map!(f::BitFunctorUnary, dest::BitArray, A::BitArray)
1737+
function map!(f::Union{typeof(identity), typeof(~)}, dest::BitArray, A::BitArray)
17271738
size(A) == size(dest) || throw(DimensionMismatch("sizes of dest and A must match"))
17281739
isempty(A) && return dest
17291740
for i=1:length(A.chunks)-1
@@ -1732,7 +1743,7 @@ function map!(f::BitFunctorUnary, dest::BitArray, A::BitArray)
17321743
dest.chunks[end] = f(A.chunks[end]) & _msk_end(A)
17331744
dest
17341745
end
1735-
function map!(f::BitFunctorBinary, dest::BitArray, A::BitArray, B::BitArray)
1746+
function map!(f::Union{BitChunkFunctor, typeof(&), typeof(|), typeof($)}, dest::BitArray, A::BitArray, B::BitArray)
17361747
size(A) == size(B) == size(dest) || throw(DimensionMismatch("sizes of dest, A, and B must all match"))
17371748
isempty(A) && return dest
17381749
for i=1:length(A.chunks)-1

‎base/bool.jl

+1
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,4 @@ rem(x::Bool, y::Bool) = y ? false : throw(DivideError())
6161
mod(x::Bool, y::Bool) = rem(x,y)
6262

6363
promote_op(op, ::Type{Bool}, ::Type{Bool}) = typeof(op(true, true))
64+
promote_op{T<:Integer}(::typeof(^), ::Type{Bool}, ::Type{T}) = Bool

‎base/broadcast.jl

+6-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ module Broadcast
44

55
using ..Cartesian
66
using Base: promote_op, promote_eltype, promote_eltype_op, @get!, _msk_end, unsafe_bitgetindex
7-
using Base: AddFun, SubFun, MulFun, LDivFun, RDivFun, PowFun
87
import Base: .+, .-, .*, ./, .\, .//, .==, .<, .!=, .<=, , .%, .<<, .>>, .^
98
export broadcast, broadcast!, broadcast_function, broadcast!_function, bitbroadcast
109
export broadcast_getindex, broadcast_setindex!
@@ -277,24 +276,24 @@ end
277276
.<<(A::AbstractArray, B::AbstractArray) = broadcast(<<, A, B)
278277
.>>(A::AbstractArray, B::AbstractArray) = broadcast(>>, A, B)
279278

280-
eltype_plus(As::AbstractArray...) = promote_eltype_op(AddFun(), As...)
279+
eltype_plus(As::AbstractArray...) = promote_eltype_op(+, As...)
281280

282281
.+(As::AbstractArray...) = broadcast!(+, Array(eltype_plus(As...), broadcast_shape(As...)), As...)
283282

284283
function .-(A::AbstractArray, B::AbstractArray)
285-
broadcast!(-, Array(promote_op(SubFun(), eltype(A), eltype(B)), broadcast_shape(A,B)), A, B)
284+
broadcast!(-, Array(promote_op(-, eltype(A), eltype(B)), broadcast_shape(A,B)), A, B)
286285
end
287286

288-
eltype_mul(As::AbstractArray...) = promote_eltype_op(MulFun(), As...)
287+
eltype_mul(As::AbstractArray...) = promote_eltype_op(*, As...)
289288

290289
.*(As::AbstractArray...) = broadcast!(*, Array(eltype_mul(As...), broadcast_shape(As...)), As...)
291290

292291
function ./(A::AbstractArray, B::AbstractArray)
293-
broadcast!(/, Array(promote_op(RDivFun(), eltype(A), eltype(B)), broadcast_shape(A, B)), A, B)
292+
broadcast!(/, Array(promote_op(/, eltype(A), eltype(B)), broadcast_shape(A, B)), A, B)
294293
end
295294

296295
function .\(A::AbstractArray, B::AbstractArray)
297-
broadcast!(\, Array(promote_op(LDivFun(), eltype(A), eltype(B)), broadcast_shape(A, B)), A, B)
296+
broadcast!(\, Array(promote_op(\, eltype(A), eltype(B)), broadcast_shape(A, B)), A, B)
298297
end
299298

300299
typealias RatIntT{T<:Integer} Union{Type{Rational{T}},Type{T}}
@@ -308,7 +307,7 @@ function .//(A::AbstractArray, B::AbstractArray)
308307
end
309308

310309
function .^(A::AbstractArray, B::AbstractArray)
311-
broadcast!(^, Array(promote_op(PowFun(), eltype(A), eltype(B)), broadcast_shape(A, B)), A, B)
310+
broadcast!(^, Array(promote_op(^, eltype(A), eltype(B)), broadcast_shape(A, B)), A, B)
312311
end
313312

314313
## element-wise comparison operators returning BitArray ##

‎base/char.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ length(c::Char) = 1
1818
endof(c::Char) = 1
1919
getindex(c::Char) = c
2020
getindex(c::Char, i::Integer) = i == 1 ? c : throw(BoundsError())
21-
getindex(c::Char, I::Integer...) = all(EqX(1), I) ? c : throw(BoundsError())
21+
getindex(c::Char, I::Integer...) = all(Predicate(x -> x == 1), I) ? c : throw(BoundsError())
2222
first(c::Char) = c
2323
last(c::Char) = c
2424
eltype(::Type{Char}) = Char
@@ -42,9 +42,9 @@ isless(x::Integer, y::Char) = isless(x, UInt32(y))
4242
+(x::Char, y::Integer) = Char(Int32(x) + Int32(y))
4343
+(x::Integer, y::Char) = y + x
4444

45-
Base.promote_op{I<:Integer}(::Base.SubFun, ::Type{Char}, ::Type{I}) = Char
46-
Base.promote_op{I<:Integer}(::Base.AddFun, ::Type{Char}, ::Type{I}) = Char
47-
Base.promote_op{I<:Integer}(::Base.AddFun, ::Type{I}, ::Type{Char}) = Char
45+
Base.promote_op{I<:Integer}(::typeof(-), ::Type{Char}, ::Type{I}) = Char
46+
Base.promote_op{I<:Integer}(::typeof(+), ::Type{Char}, ::Type{I}) = Char
47+
Base.promote_op{I<:Integer}(::typeof(+), ::Type{I}, ::Type{Char}) = Char
4848

4949
bswap(x::Char) = Char(bswap(UInt32(x)))
5050

‎base/coreimg.jl

-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ include("nofloat_hashing.jl")
5656
macro simd(forloop)
5757
esc(forloop)
5858
end
59-
include("functors.jl")
6059
include("reduce.jl")
6160

6261
## core structures

‎base/dates/arithmetic.jl

+8-16
Original file line numberDiff line numberDiff line change
@@ -97,26 +97,18 @@ end
9797

9898
# promotion rules
9999

100-
for (op,F) in ((:+, Base.AddFun),
101-
(:-, Base.SubFun),
102-
(:.+, Base.DotAddFun),
103-
(:.-, Base.DotSubFun))
100+
for op in (:+, :-, :.+, :.-)
104101
@eval begin
105-
Base.promote_op{P<:Period}(::$F, ::Type{P}, ::Type{P}) = P
106-
Base.promote_op{P1<:Period,P2<:Period}(::$F, ::Type{P1}, ::Type{P2}) = CompoundPeriod
107-
Base.promote_op{D<:Date}(::$F, ::Type{D}, ::Type{D}) = Day
108-
Base.promote_op{D<:DateTime}(::$F, ::Type{D}, ::Type{D}) = Millisecond
102+
Base.promote_op{P<:Period}(::typeof($op), ::Type{P}, ::Type{P}) = P
103+
Base.promote_op{P1<:Period,P2<:Period}(::typeof($op), ::Type{P1}, ::Type{P2}) = CompoundPeriod
104+
Base.promote_op{D<:Date}(::typeof($op), ::Type{D}, ::Type{D}) = Day
105+
Base.promote_op{D<:DateTime}(::typeof($op), ::Type{D}, ::Type{D}) = Millisecond
109106
end
110107
end
111108

112-
for (op,F) in ((:/, Base.RDivFun),
113-
(:%, Base.RemFun),
114-
(:div, Base.IDivFun),
115-
(:mod, Base.ModFun),
116-
(:./, Base.DotRDivFun),
117-
(:.%, Base.DotRemFun))
109+
for op in (:/, :%, :div, :mod, :./, :.%)
118110
@eval begin
119-
Base.promote_op{P<:Period}(::$F, ::Type{P}, ::Type{P}) = typeof($op(1,1))
120-
Base.promote_op{P<:Period,R<:Real}(::$F, ::Type{P}, ::Type{R}) = P
111+
Base.promote_op{P<:Period}(::typeof($op), ::Type{P}, ::Type{P}) = typeof($op(1,1))
112+
Base.promote_op{P<:Period,R<:Real}(::typeof($op), ::Type{P}, ::Type{R}) = P
121113
end
122114
end

‎base/deprecated.jl

+50
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,56 @@ function pmap(f, c...; err_retry=nothing, err_stop=nothing, pids=nothing)
10201020
return pmap(p, f, c...)
10211021
end
10221022

1023+
# 15692
1024+
typealias Func{N} Function
1025+
deprecate(:Func)
1026+
for (Fun, func) in [(:IdFun, :identity),
1027+
(:AbsFun, :abs),
1028+
(:Abs2Fun, :abs2),
1029+
(:ExpFun, :exp),
1030+
(:LogFun, :log),
1031+
(:ConjFun, :conj),
1032+
(:AndFun, :&),
1033+
(:OrFun, :|),
1034+
(:XorFun, :$),
1035+
(:AddFun, :+),
1036+
(:DotAddFun, :.+),
1037+
(:SubFun, :-),
1038+
(:DotSubFun, :.-),
1039+
(:MulFun, :*),
1040+
(:DotMulFun, :.*),
1041+
(:RDivFun, :/),
1042+
(:DotRDivFun, :./),
1043+
(:LDivFun, :\),
1044+
(:IDivFun, :div),
1045+
(:DotIDivFun, :),
1046+
(:ModFun, :mod),
1047+
(:RemFun, :rem),
1048+
(:DotRemFun, :.%),
1049+
(:PowFun, :^),
1050+
(:MaxFun, :scalarmax),
1051+
(:MinFun, :scalarmin),
1052+
(:LessFun, :<),
1053+
(:MoreFun, :>),
1054+
(:DotLSFun, :.<<),
1055+
(:DotRSFun, :.>>),
1056+
(:ElementwiseMaxFun, :max),
1057+
(:ElementwiseMinFun, :min),
1058+
(:ComplexFun, :complex),
1059+
(:DotFun, :dot),
1060+
]
1061+
@eval begin
1062+
@deprecate_binding $(Fun) typeof($(func))
1063+
(::Type{typeof($(func))})() = $(func)
1064+
end
1065+
end
1066+
@deprecate_binding CentralizedAbs2Fun typeof(centralizedabs2fun(0)).name.primary
1067+
(::Type{typeof(centralizedabs2fun(0)).name.primary})(m::Number) = centralizedabs2fun(m)
1068+
@deprecate specialized_unary(f::Function) f
1069+
@deprecate specialized_binary(f::Function) f
1070+
@deprecate specialized_bitwise_unary(f::Function) f
1071+
@deprecate specialized_bitwise_binary(f::Function) f
1072+
10231073

10241074
# During the 0.5 development cycle, do not add any deprecations below this line
10251075
# To be deprecated in 0.6

0 commit comments

Comments
 (0)
Please sign in to comment.