Saturday, August 26, 2017

Julia - Language - Variable Number of Arguments using Ellipses / Splat (...)

Consider a scenario in which we want to create a function in which we do not know how many arguments we will pass to it in advance. How do we do this?
And to do this, we are going to use these three dots (...) with our argument name.

... - These 3 dots are called splat or ellipsis. Ellipsis indicate that we are passing zero, one or more than one arguments. 

Example - function with ellipis:

julia> function how_many_args(myarguments...)
         println("the number of arguments passed are $(length(myarguments))")
       end
how_many_args (generic function with 1 method)

julia> how_many_args()
the number of arguments passed are 0

julia> how_many_args(1,2,3)
the number of arguments passed are 3

julia> 


julia> a = 5
5

julia> b = 6
6

julia> c="sdf"
"sdf"

julia> how_many_args(a,b,c)
the number of arguments passed are 3

julia> 


Let us see some other areas where this facility to handle unknown number of arguments in a function is useful.

Example: Usage of Join Function.

We will now write a function which accepts a array of strings and gives the output as a meaningful string.

julia> function join_string(input_array)
       string_elements = join(input_array," , "," and ")
       println("The elements in the string are: $(string_elements) \!")
       end
join_string (generic function with 1 method)

For details on usage of Join function Refer: https://docs.julialang.org/en/stable/stdlib/strings/#Base.join

julia> join_string(["Steve Waugh","Mark Waugh","Ricky Ponting"])
The elements in the string are: Steve Waugh , Mark Waugh and Ricky Ponting !

julia> join_string(["Steve Waugh","Mark Waugh"])
The elements in the string are: Steve Waugh and Mark Waugh !

julia> 

Let us see, what happens when you pass a single string as input to this function: join_string

julia> join_string("Ramesh")
The elements in the string are: R , a , m , e , s and h !

julia> 


The single string input has got split into mulitiple little strings. To overcome this problem, let us create one more function using ellipsis or splat:

julia> function join_string_splat(input_string...)
              string_elements = join(input_string," , "," and ")
              println("The elements in the string are: $(string_elements) \!")
              end
join_string_splat (generic function with 1 method)

julia>

julia> join_string_splat("Ramesh")
The elements in the string are: Ramesh !

julia> join_string_splat("Steve Waugh","Mark Waugh","Ricky Ponting")
The elements in the string are: Steve Waugh , Mark Waugh and Ricky Ponting !

julia>

Note: This time, we have not passed the input as a list of strings, but as separate strings. 

To make our understanding better let us try one more example of multiple argments passed as ellipses / splat (...). 

julia> function multiple_args(a,b,c...)
       println("The argument list is $a , $b, $c")
       end
multiple_args (generic function with 1 method)

julia> multiple_args(1,2,3,4,5,6,7,8,9,10,"sachin")
The argument list is 1 , 2, (3, 4, 5, 6, 7, 8, 9, 10, "sachin")


In the above example, the variable c has accepted the unlimited multiple arguments due to the presence of ellipses.

Let us now check if it is possible to combine keyword argument with splat arguments:

julia> function keyword_splat_arg(;a...)
             a
             end
keyword_splat_arg (generic function with 1 method)

julia> keyword_splat_arg(var1=1,var2=2,var3=3)
3-element Array{Any,1}:
 (:var1, 1)
 (:var2, 2)
 (:var3, 3)

julia> 


We can see that, the return vallue is an array of tuples with Key-Value pairs. The Key is the name we gave the keyword argument. Moreover, it is a symbol as there is a colon preceding it.

No comments:

Post a Comment