# Flexible subseting

**URL:** <https://discourse.julialang.org/t/flexible-subseting/2948>\
**Category:** General Usage\
**Created:** [March 29, 2017, 12:41pm UTC](https://discourse.julialang.org/t/flexible-subseting/2948 "2017-03-29T12:41:32Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [March 29, 2017, 12:41pm UTC](https://discourse.julialang.org/t/flexible-subseting/2948/1 "2017-03-29T12:41:32Z")

</div>

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:

```julia

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)

```
