How to programmatically generate different functions?

This can for sure be written in a nicer way.

function testf(a,b,p1,p2,p3)
    @info "call with", (a,b,p1,p2,p3)
end

function genf(i)

    (a,b,p...) ->begin
        p_new = []
        param = collect(p)
        mask = Bool.(digits(i,base=2,pad=3))
        for i in mask
            if i
                push!(p_new,Inf)
            else
                push!(p_new,popfirst!(param))
            end
        end
        testf(a,b,p_new...)
    end

end
julia> for i in 0:7
           genf(i)(1,2,3,4,5)
       end
[ Info: ("call with", (1, 2, 3, 4, 5))
[ Info: ("call with", (1, 2, Inf, 3, 4))
[ Info: ("call with", (1, 2, 3, Inf, 4))
[ Info: ("call with", (1, 2, Inf, Inf, 3))
[ Info: ("call with", (1, 2, 3, 4, Inf))
[ Info: ("call with", (1, 2, Inf, 3, Inf))
[ Info: ("call with", (1, 2, 3, Inf, Inf))
[ Info: ("call with", (1, 2, Inf, Inf, Inf))

julia> genf(1)(1,2,3,4)
[ Info: ("call with", (1, 2, Inf, 3, 4))

julia> genf(3)(1,2,3)
[ Info: ("call with", (1, 2, Inf, Inf, 3))
1 Like