Using zip or enumerate within IF?

Hello,
Is there a way to use zip() or enumerate() within the IF statement?
I’m looking for a safter way to run the code below. I’ve created structs to dispatch an appropriate model based off a “category” in a dataframe. Currently it’s only dispatching the custom models correctly because custom_cats and custom_cats_structs are aligned (that is when looping through category: Custom 1, Struct: custom_a is the appropriate choice).

It seems like doing something like zip(custom_cats, custom_cats_structs) would be safer / more preferred way, but I can’t seem to figure out what the right syntax is.

Here’s what I have (which works, but again seems suboptimal):

struct general end
struct custom_a end
struct custom_b end

df = DataFrame(category=["Normal 1", "Normal 2", "Normal 3", "Custom 1", "Custom 2"], X=[5, 4, 8, 9,10], Y =[1000,5000,3000,1200,5000])

cats = ["Normal 1", "Normal 2", "Normal 3", "Custom 1", "Custom 2"]
normal_cats = ["Normal 1", "Normal 2", "Normal 3"]
custom_cats = ["Custom 1", "Custom 2"]
custom_cats_structs = [custom_a(),custom_b()]

function mymodel(x::general)
    println("Genearl Model Hello")
end

function mymodel(x::custom_a)
    println("This is my first custom model")
end

function mymodel(x::custom_b)
    println("This is my second custom model")
end

function run_models(cats,norm,cust,cust_structs)
    for c in cats
        println("working on $c")
        if c in norm
            mymodel(general())
        elseif c in cust
            n = 1
            mymodel(cust_structs[n])
            n+1
        end
    end
end

run_models(cats,normal_cats,custom_cats,custom_cats_structs)

Hope this makes sense. Thanks for your help.

julia> a = [1, 2, 3, 4]; b = [100, 200, 300, 400];

julia> for (ai, bi) in zip(a, b)
           println("ai: ", ai)
           println("bi: ", bi)
       end
ai: 1
bi: 100
ai: 2
bi: 200
ai: 3
bi: 300
ai: 4
bi: 400

Can we ask what the higher level goal is? Like, what end result do you want to get e.g. print something for each item in the data frame? While attempting a solution is good, it can lead to so-called “XY problems”. I also don’t quite understand what the current goal is either.

You essentially have an associative map. The datastructure for this is Dict. Here’s how I would do it:

struct General end
struct CustomA end
struct CustomB end

categories = ["Normal 1", "Normal 2", "Normal 3", "Custom 1", "Custom 2"]
custom_category_selectors = Dict("Custom 1" => CustomA(), "Custom 2" => CustomB())

function mymodel(x::General)
    println("General Model Hello")
end

function mymodel(x::CustomA)
    println("This is my first custom model")
end

function mymodel(x::CustomB)
    println("This is my second custom model")
end

function run_models(categories, custom_selectors)
    for c in categories
        println("working on $c")
        selector = get(custom_selectors, c, General())  # Defaults to General() when c is not a key in custom_selectors
        mymodel(selector)
    end
end

run_models(categories, custom_category_selectors)
# Output
working on Normal 1
General Model Hello
working on Normal 2
General Model Hello
working on Normal 3
General Model Hello
working on Custom 1
This is my first custom model
working on Custom 2
This is my second custom model
1 Like