Survey.jl
Survey.jl copied to clipboard
Simplifying structs
Structures in the package have giant if and else ladders. This is because a user should be allowed to pass probs, weights or popsize, and they all can be vectors as well as symbols, and they can be derived from one another.
The if and else ladder is hard to read and develop upon. How about the following design:
struct PopSize
popsize
end
function PopSize(data::DataFrame, x::Vector)
return x
end
function PopSize(data::DataFrame, x::Symbol)
return data[!, x]
end
struct Weights
wts
end
function Weights(data::DataFrame, x::Vector)
return x
end
function Weights(data::DataFrame, x::Symbol)
return data[!, x]
end
struct SurveyDesign
data
weights,
popsize
end
function SurveyDesign(data, popsize::PopSize)
SurveyDesign(data, nrow(data)./popsize.popsize, popsize.popsize)
end
function SurveyDesign(data, weights::Weights)
SurveyDesign(data, nrow(data)./popsize.popsize, sum(weights.weights))
end
User will do:
mydesign = SurveyDesign(apisrs, PopSize(apisrs, :fpc)
Doesn't look nice? Perhaps:
mydesign = @surveydesign (data = apisrs, popsize = :fpc)
The macro will turn the expression (data = apisrs, popsize = :fpc)
into SurveyDesign(apisrs, PopSize(apisrs, :fpc)
We can look into the macro way? or perhaps later when more functionality related pending tasks done in main? Once more certainty on how many variables needed inside class, then can look into syntactic sugar like this?
@iuliadmtru can you see if such a macro is possible?
Your suggestion definitely feels nicer than the if-elses we have now. Once the other high-priority tasks are done, I will look into this.