Hi all-
I’m trying to figure out a flexible way to subset an array of objects based on attributes. If I have 4 attributes, I could have as many as 15 specific functions. What I was hoping to do is reduce that down to as few as possible. What I have devised so far reduces it to 4 using multiple dispatch. I was wondering if there is a more elegant approach. Here is an example of what I have:
function F(a1,v1,VectorMytype)
return filter(x->getfield(x,a1)==v1,VectorMytype)
end
function F(a1,v1,a2,v2,VectorMytype)
return filter(x->(getfield(x,a1)==v1) & (getfield(x,a2)==v2),VectorMytype)
end
#The same for 3 and 4 attributes
type myType
Attr1::Int
Attr2::Int
Attr3::Int
Attr4::Int
myType(a1,a2,a3,a4) = new(a1,a2,a3,a4)
end
VectorMytype = [myType(rand(1:4,4)...) for i = 1:10]
#An example of subsetting based on one attribute.
F(:Attr1,2,VectorMytype)