1
0
mirror of https://github.com/adambard/learnxinyminutes-docs.git synced 2025-07-31 20:10:49 +02:00

Update Dict and Set Syntax (#2296)

This commit is contained in:
Edmilson Lima
2016-07-02 11:02:38 -03:00
committed by ven
parent a7eed36c1d
commit 5dee671bfe

View File

@@ -255,7 +255,7 @@ e, d = d, e # => (5,4) # d is now 5 and e is now 4
empty_dict = Dict() # => Dict{Any,Any}() empty_dict = Dict() # => Dict{Any,Any}()
# You can create a dictionary using a literal # You can create a dictionary using a literal
filled_dict = ["one"=> 1, "two"=> 2, "three"=> 3] filled_dict = Dict("one"=> 1, "two"=> 2, "three"=> 3)
# => Dict{ASCIIString,Int64} # => Dict{ASCIIString,Int64}
# Look up values with [] # Look up values with []
@@ -305,7 +305,7 @@ in(10, filled_set) # => false
other_set = Set([3, 4, 5, 6]) # => Set{Int64}(6,4,5,3) other_set = Set([3, 4, 5, 6]) # => Set{Int64}(6,4,5,3)
intersect(filled_set, other_set) # => Set{Int64}(3,4,5) intersect(filled_set, other_set) # => Set{Int64}(3,4,5)
union(filled_set, other_set) # => Set{Int64}(1,2,3,4,5,6) union(filled_set, other_set) # => Set{Int64}(1,2,3,4,5,6)
setdiff(Set(1,2,3,4),Set(2,3,5)) # => Set{Int64}(1,4) setdiff(Set([1,2,3,4]),Set([2,3,5])) # => Set{Int64}(1,4)
#################################################### ####################################################
@@ -346,7 +346,7 @@ end
# cat is a mammal # cat is a mammal
# mouse is a mammal # mouse is a mammal
for a in ["dog"=>"mammal","cat"=>"mammal","mouse"=>"mammal"] for a in Dict("dog"=>"mammal","cat"=>"mammal","mouse"=>"mammal")
println("$(a[1]) is a $(a[2])") println("$(a[1]) is a $(a[2])")
end end
# prints: # prints:
@@ -354,7 +354,7 @@ end
# cat is a mammal # cat is a mammal
# mouse is a mammal # mouse is a mammal
for (k,v) in ["dog"=>"mammal","cat"=>"mammal","mouse"=>"mammal"] for (k,v) in Dict("dog"=>"mammal","cat"=>"mammal","mouse"=>"mammal")
println("$k is a $v") println("$k is a $v")
end end
# prints: # prints:
@@ -445,7 +445,7 @@ end
# You can define functions that take keyword arguments # You can define functions that take keyword arguments
function keyword_args(;k1=4,name2="hello") # note the ; function keyword_args(;k1=4,name2="hello") # note the ;
return ["k1"=>k1,"name2"=>name2] return Dict("k1"=>k1,"name2"=>name2)
end end
keyword_args(name2="ness") # => ["name2"=>"ness","k1"=>4] keyword_args(name2="ness") # => ["name2"=>"ness","k1"=>4]