I am writing a library and trying to figure out what would be a good way to define a lot of similar functions. So for example if I have code like this, but I have to do this thing basically for hundreds of functions. Just have to figure out what variables are being passed and pass them on to the main function based off of that.
def first(base, x, y) do
main(base, "first", [x, y])
end
def first(base, list) when is_list(list) do
main(base, "first", list)
end
def first(list) when is_list(list) do
main(Base.new(), "first", list)
end
def first(x, y) do
case Base.is_base(x) do
false -> main(Base.new(), "first", [x, y])
true -> main(x, "first", [y])
end
end
def second(base, x, y) do
main(base, "second", [x, y])
end
def second(x, y) do
case Base.is_base(x) do
false -> main(Base.new(), "second", [x, y])
true -> main(x, "second", [y])
end
end
Only idea how to simplify this was to generate functions using a macro, but that way I would lose IDE completions and usability for anyone who would implement this. So I am thinking that is not the best design for library? Is there a way to keep the explicitness while also automating this process somehow? other thing than macro I could think of would be actually generating code.