Broadcast and Map on Set have inconsistent behaviors

x,y,z=1,2,3
a=Set()
b=Set()
push!(a,:x,:y,:z)
push!(b,1,2,3)

log.(b) # error
map(log,b) # work

map(string,a) # work, gives Set(String["x", "z", "y"])
string.(a) # no error, but gives a string "Set(Any[:y, :z, :x])"

I find it rather confusing and not fit to common expectations. i am testing on win_x64 v0.6.2.

You should not, in general, expect broadcast and map to have the same behavior, they are named differently for a reason.

The basic philosophy (as I understand it) is that map is for general iterable collections while broadcast is for objects with an AbstractArray-like behavior. In light of this, the reason you shouldn’t necessarily expect broadcast to work for Set is because sets are collections with no ordering.

As an example:

x = [1, 2, 3]
y = [4, 5, 6]
x .* y # returns [4, 10, 18]

In this operation ordering was clearly very important: it only makes sense because x and y have a well-defined order. Meanwhile, b .* y (with b the set you defined above) would be undefined because the ordering of b is not defined.

3 Likes

Broadcasting over sets is deprecated on 0.7 for precisely this reason.

4 Likes

Thanks for the explaination @ExpandingMan.