Generalizing functions to accept inputs with 1 or 2 (or with 2 or 3) dimensions

I am not sure if this is what you want, but this example is, literally, what conforms to that description (here the “best” input is the smallest of the numbers, and the “best” is multiplied by 10 at the end):

julia> function f(args...)
         best = args[1]
         for i in 2:length(args)
           if args[i] < best
             best = args[i]
           end
         end
         return 10*best
       end
f (generic function with 1 method)

julia> f(1)
10

julia> f(1,2)
10

julia> f(0.5,1,2)
5.0


2 Likes