@@ -11,7 +11,7 @@ using .Base.Cartesian
1111using . Base: Indices, OneTo, tail, to_shape, isoperator, promote_typejoin,
1212 _msk_end, unsafe_bitgetindex, bitcache_chunks, bitcache_size, dumpbitcache, unalias
1313import . Base: copy, copyto!, axes
14- export broadcast, broadcast!, BroadcastStyle, broadcast_axes, broadcastable, dotview, @__dot__ , broadcast_preserving_zero_d
14+ export broadcast, broadcast!, BroadcastStyle, broadcast_axes, broadcastable, dotview, @__dot__ , broadcast_preserving_zero_d, BroadcastFunction
1515
1616# # Computing the result's axes: deprecated name
1717const broadcast_axes = axes
@@ -1196,7 +1196,7 @@ function __dot__(x::Expr)
11961196 Meta. isexpr (x. args[1 ], :call ) # function or macro definition
11971197 Expr (x. head, x. args[1 ], dotargs[2 ])
11981198 elseif x. head === :(< :) || x. head === :(> :)
1199- tmp = x. head === :(< :) ? :( .< :) : :( .> :)
1199+ tmp = x. head === :(< :) ? :.< : : :.> :
12001200 Expr (:call , tmp, dotargs... )
12011201 else
12021202 if x. head === :&& || x. head === :||
@@ -1264,4 +1264,44 @@ end
12641264end
12651265@inline broadcasted (:: S , f, args... ) where S<: BroadcastStyle = Broadcasted {S} (f, args)
12661266
1267+ """
1268+ BroadcastFunction{F} <: Function
1269+
1270+ Represents the "dotted" version of an operator, which broadcasts the operator over its
1271+ arguments, so `BroadcastFunction(op)` is functionally equivalent to `(x...) -> (op).(x...)`.
1272+
1273+ Can be created by just passing an operator preceded by a dot to a higher-order function.
1274+
1275+ # Examples
1276+ ```jldoctest
1277+ julia> a = [[1 3; 2 4], [5 7; 6 8]];
1278+
1279+ julia> b = [[9 11; 10 12], [13 15; 14 16]];
1280+
1281+ julia> map(.*, a, b)
1282+ 2-element Vector{Matrix{Int64}}:
1283+ [9 33; 20 48]
1284+ [65 105; 84 128]
1285+
1286+ julia> Base.BroadcastFunction(+)(a, b) == a .+ b
1287+ true
1288+ ```
1289+
1290+ !!! compat "Julia 1.6"
1291+ `BroadcastFunction` and the standalone `.op` syntax are available as of Julia 1.6.
1292+ """
1293+ struct BroadcastFunction{F} <: Function
1294+ f:: F
1295+ end
1296+
1297+ @inline (op:: BroadcastFunction )(x... ; kwargs... ) = op. f .(x... ; kwargs... )
1298+
1299+ function Base. show (io:: IO , op:: BroadcastFunction )
1300+ print (io, BroadcastFunction, ' (' )
1301+ show (io, op. f)
1302+ print (io, ' )' )
1303+ nothing
1304+ end
1305+ Base. show (io:: IO , :: MIME"text/plain" , op:: BroadcastFunction ) = show (io, op)
1306+
12671307end # module
0 commit comments