How to optimize code without repeating similar part, based on the type of input argument?

I have these 4 functions, which are almost similar. Based on the type of input argument, different Gaussline(i, j) will be given.

Is there any way to shorten this code?

function create_gauss_points(::Type{Bar2})
    gauss_line = GaussLine(2,1)
    gauss_points = [GaussPoint(i...) for i in gauss_line]
    return gauss_points
end

function create_gauss_points(::Type{Bar3})
    gauss_line = GaussLine(3,1)
    gauss_points = [GaussPoint(i...) for i in gauss_line]
    return gauss_points
end

function create_gauss_points(::Type{Quad4})
    gauss_line = GaussLine(2, 2)
    gauss_points = [GaussPoint(i...) for i in gauss_line]
    return gauss_points
end

function create_gauss_points(::Type{Quad8})
    gauss_line = GaussLine(3,2)
    gauss_points = [GaussPoint(i...) for i in gauss_line]
    return gauss_points
end

Thank you for your help!

Make the arguments to GaussLine type parameters?

struct Element{A,B}
  name
  ...
end
function create_gauss_points(::Element{A,B}) where {A,B}
  gauss_line = GaussLine(A,B)
...
end
3 Likes

I really appreciate your reply. This is really helpful!!

1 Like