What do triple points (...) do in julia array? Why do they change the type signature?

Given the data types below, the following understanding gives two Array{Any,1} of Player s:

 [[team.players for team in [big_team_1, big_team_2]]] 

This following understanding, however, gives the desired 12-element Array{Player,1} result:

 [[team.players for team in [big_team_1, big_team_2]]...] 

What exactly does ... ? Where is this documented?


Data:

 type Player ranking::Int end type Team players::Array{Player} end team_1 = Team([Player(10_000), Player(11_000), Player(9_000), Player(8_500), Player(20_000), Player(10_500)]) team_2 = Team([Player(i.ranking + 3000) for i in team_1.players]) 
+5
source share
1 answer

args... and ; kwargs... ; kwargs... is a splat operator, if you know Python, it is the same as *args and **kwargs :

You can find the documentation here: What does the splat ... operator ... ?

When in a method signature (collects arguments).

 julia> function foo(pos_1, pos_2, opt_1 = :opt_1, args...; opt_kw1 = :opt_kw1, opt_kw2 = :opt_kw2, kwargs...) [pos_1, pos_2, opt_1, args, opt_kw1, opt_kw2, (kwargs,);] end foo (generic function with 2 methods) 

This signature means:

  • pos_1 is the first required positional argument.
  • pos_2 is the second required positional argument.
  • opt_1 is an optional positional argument.
  • args... are all of the following positional arguments collected in the tuple.

Note how sem colon ; separates positional arguments from keyword arguments (order is not important in keyword arguments):

  • opt_kw1 is an optional keyword argument.
  • opt_kw2 is an optional keyword argument.
  • kwargs... - all of the following keyword arguments collected in an array of element pairs (key, value).

 julia> methods(foo) # 2 methods for generic function "foo": foo(pos_1, pos_2) at none:3 foo(pos_1, pos_2, opt_1, args...) at none:3 

foo can be called like this:

Required Arguments:
 julia> foo(:pos_1, :pos_2) 7-element Array{Any,1}: :pos_1 # provided value :pos_2 # provided value :opt_1 # default value () # empty tuple :opt_kw1 # default value :opt_kw2 # default value (Any[],) # there are no kwargs 
Optional positional and keyword arguments:
 julia> foo(:pos_1, :pos_2, :OPT_1, :a, :b, :c, opt_kw2 = :OPT_KW2, kwarg1 = true, opt_kw1 = :OPT_KW1, kwarg2 = false) 7-element Array{Any,1}: :pos_1 :pos_2 :OPT_1 (:a,:b,:c) :OPT_KW1 :OPT_KW2 (Any[(:kwarg1,true),(:kwarg2,false)],) 

Note that the order in the keyword arguments does not matter, also a semicolon ; not needed when calling a function.


When calling a function (spreads).

Using collections for positional arguments and associations of symbolic key associations for keyword arguments:

 julia> x, y, z = 1, 2, 3; julia> sum(x, y, z) ERROR: MethodError: `sum` has no method matching sum(::Int64, ::Int64, ::Int64) Closest candidates are: sum(::Union{Base.Func{1},Function}, ::AbstractArray{T,N}, ::Any) sum(::Union{Base.Func{1},DataType,Function}, ::Any) sum(::BitArray{N}, ::Any) ... julia> sum sum (generic function with 12 methods) julia> Base.sum(args...) = sum(args) sum (generic function with 13 methods) julia> sum(x, y, z) 6 julia> foo(x, y, z) = sum(x, y, z) foo (generic function with 1 method) julia> foo(x, y, z) 6 julia> foo([x, y, z]) ERROR: MethodError: `foo` has no method matching foo(::Array{Int64,1}) Closest candidates are: foo(::Any, ::Any, ::Any) julia> foo([x, y, z]...) 6 

 julia> foo(; x = 0, y = 0, z = 0) = sum(x, y, z) foo (generic function with 2 methods) julia> foo() 0 julia> foo(z = 3, x = 1, y = 2) 6 julia> foo(; Dict(:z => 3, :y => 2, :x => 1)) ERROR: TypeError: anonymous: in typeassert, expected Symbol, got Pair{Symbol,Int64} in anonymous at no file julia> foo(; Dict(:z => 3, :y => 2, :x => 1)...) 6 
+10
source

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


All Articles