Splat explanation

After reading about Julia at http://learnxinyminutes.com/docs/julia/ , I came across this:

# You can define functions that take a variable number of # positional arguments function varargs(args...) return args # use the keyword return to return anywhere in the function end # => varargs (generic function with 1 method) varargs(1,2,3) # => (1,2,3) # The ... is called a splat. # We just used it in a function definition. # It can also be used in a fuction call, # where it will splat an Array or Tuple contents into the argument list. Set([1,2,3]) # => Set{Array{Int64,1}}([1,2,3]) # produces a Set of Arrays Set([1,2,3]...) # => Set{Int64}(1,2,3) # this is equivalent to Set(1,2,3) x = (1,2,3) # => (1,2,3) Set(x) # => Set{(Int64,Int64,Int64)}((1,2,3)) # a Set of Tuples Set(x...) # => Set{Int64}(2,3,1) 

I am sure this is a great explanation, but I do not understand the main idea / advantages.

From what I understand so far:

  • Using splat in a function definition allows us to indicate that we don’t know how many input arguments will be provided for the function, maybe 1, maybe 1000. I don’t really see the benefits of this, but at least I understand (hopefully) the concept of this.
  • Using splat as an input argument to a function does ... What exactly? And why should I use this? If I had to enter the contents of the array into the argument list, I would use the syntax instead: some_array (:, :) (for 3D arrays, I would use some_array (:,:, :), etc.).

I think part of the reason why I don’t understand this is because I am struggling with the definition of tuples and arrays, are tuples and arrays data types (like Int64 - data type) in Julia? Or is it a data structure and a data structure? When I hear an array, I usually think of a 2D matrix, maybe not the best way to represent arrays in a programming context?

I understand that you could probably write whole books about what a data structure is, and I could say with confidence that this is Google, however I believe that people with a deep understanding of the subject can explain this much more succinctly ( and possibly simplified), then let him say that the Wikipedia article may be, so I ask you guys (and girls).

+5
source share
2 answers

It looks like you get the mechanism and how / what they do, but struggle with what you used it for. I understand.

I find them useful for things where I need to pass an unknown number of arguments and don't want to worry about creating an array first before passing it when working with a function in interactive mode.

eg:

 func geturls(urls::Vector) # some code to retrieve URL from the network end geturls(urls...) = geturls([urls...]) # slightly nicer to type than building up an array first then passing it in. geturls("http://google.com", "http://facebook.com") # when we already have a vector we can pass that in as well since julia has method dispatch geturls(urlvector) 

So a few comments. Splat allows you to turn iterable into an array and vice versa. See Bit [urls...] above? Julia turns this into a vector with a URL extension, which turns out to be much more useful than an argument that breaks itself into my experience.

This is just one example of where they are useful to me. When you use julia, you will come across more.

It is mainly there to help develop api that seem natural to use.

+4
source

Signs require you to know the parameters; an advantage, rather than passing values ​​/ variables for each of the parameters, which you can pass to a single variable that has properties representing each of the required parameters.

The main advantages are less typing and improved readability.

eg. (PowerShell)

 $credentials = get-credential $x = Test-Credential -username $credentials.Username -password $credentials.Password 

against

 $credentials = get-credential $x = Test-Credential @credentials 
-1
source

Source: https://habr.com/ru/post/1207074/


All Articles