# Contruct filter expression within loop

**URL:** <https://discourse.julialang.org/t/contruct-filter-expression-within-loop/104003>\
**Category:** New to Julia\
**Tags:** macros\
**Created:** [September 18, 2023, 9:45pm UTC](https://discourse.julialang.org/t/contruct-filter-expression-within-loop/104003 "2023-09-18T21:45:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chedy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chedy/32/52838_2.png) [@chedy](https://discourse.julialang.org/u/chedy)\
**Post date:** [September 18, 2023, 9:45pm UTC](https://discourse.julialang.org/t/contruct-filter-expression-within-loop/104003/1 "2023-09-18T21:45:21Z")

</div>

Hi all,

I’m trying to simplify this code using a macro, but I’m pretty new to meta-programming and not sure what I need to do. I have an array `bigrams` of structs that have two fields (among others): w1, and w2 (both integers).

I want to write a loop that does the following:

```julia
# First iteration:
rows_with = filter(x -> x.w1 == 1, bigrams)
rows_without = filter(x -> x.w2 != 2, rows_with)

# Second iteration:
rows_with = filter(x -> x.w2 == 2, bigrams)
rows_without = filter(x -> x.w1 != 1, rows_with)

```

How can I dynamically write something like `x -> x.w1 == 1` and `x -> x.w2 != 2` within the loop? Is it good style? And would this work if it’s wrapped within a function (so no global scope for variables)?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 18, 2023, 11:22pm UTC](https://discourse.julialang.org/t/contruct-filter-expression-within-loop/104003/2 "2023-09-18T23:22:11Z")

</div>

It is not clear what you want there. Probably you don’t need macros, but it is also unclear what’s wrong about the solution you are providing.

Assuming that what you are asking is how do iterate over sets of functions, you could do something like this in this example:

```julia
    for (f1,f2) in [ # vector of tuples of functions
        (x -> x.w1 == 1, x -> x.w2 != 2), 
        (x -> x.w2 == 2, x -> x.w1 != 1)
    ]
        rows_with = filter(f1, biggrams)
        rows_without = filter(f2, rows_with)
    end

```

Depending on the purpose, that might be fine. Maybe more details are needed to really understand what you need, but in principle I don’t see why use macros (you’ll almost never need to).

---

<div class="post-metadata">

**Author:** ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)\
**Post date:** [September 19, 2023, 7:11am UTC](https://discourse.julialang.org/t/contruct-filter-expression-within-loop/104003/3 "2023-09-19T07:11:48Z")

</div>

As @lmiq said, more information would be helpful.

Perhaps `getfield` could be useful in your case? It allows you to get a field value programmatically with `Symbol`s, without the need to hard-code the field names:

```julia
struct Foo
  w1::Int
  w2::Int
end

obj = Foo(1,2)

for fname in [:w1, :w2]
  getfield(obj, fname) |> println
end

```

EDIT: By hard-coding, I just mean writing `obj.w1` which has the field baked in. `getfield(obj, fname)` can just use a variable with the field name.
