mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-05-23 06:40:58 +00:00
one more quick run over the code
This commit is contained in:
parent
c8ad0d0809
commit
9fab30a26a
@ -30,7 +30,7 @@ This is based on Julia 1.0.0
|
|||||||
3 # => 3 (Int64)
|
3 # => 3 (Int64)
|
||||||
3.2 # => 3.2 (Float64)
|
3.2 # => 3.2 (Float64)
|
||||||
2 + 1im # => 2 + 1im (Complex{Int64})
|
2 + 1im # => 2 + 1im (Complex{Int64})
|
||||||
2 // 3 # => 2//3 (Rational{Int64})
|
2 // 3 # => 2 // 3 (Rational{Int64})
|
||||||
|
|
||||||
# All of the normal infix operators are available.
|
# All of the normal infix operators are available.
|
||||||
1 + 1 # => 2
|
1 + 1 # => 2
|
||||||
@ -81,29 +81,18 @@ false
|
|||||||
2 < 3 < 2 # => false
|
2 < 3 < 2 # => false
|
||||||
|
|
||||||
# Strings are created with "
|
# Strings are created with "
|
||||||
try
|
"This is a string."
|
||||||
"This is a string."
|
|
||||||
catch ; end
|
|
||||||
|
|
||||||
# Julia has several types of strings, including ASCIIString and UTF8String.
|
|
||||||
# More on this in the Types section.
|
|
||||||
|
|
||||||
# Character literals are written with '
|
# Character literals are written with '
|
||||||
try
|
'a'
|
||||||
'a'
|
|
||||||
catch ; end
|
|
||||||
|
|
||||||
# Some strings can be indexed like an array of characters
|
# Strings are UTF8 encoded. Only if they contain only ASCII characters can
|
||||||
try
|
# they be safely indexed.
|
||||||
"This is a string"[1] # => 'T' # Julia indexes from 1
|
ascii("This is a string")[1] # => 'T' # Julia indexes from 1
|
||||||
catch ; end
|
# Otherwise, iterating over strings is recommended (map, for loops, etc).
|
||||||
# However, this is will not work well for UTF8 strings,
|
|
||||||
# so iterating over strings is recommended (map, for loops, etc).
|
|
||||||
|
|
||||||
# $ can be used for string interpolation:
|
# $ can be used for string interpolation:
|
||||||
try
|
"2 + 2 = $(2 + 2)" # => "2 + 2 = 4"
|
||||||
"2 + 2 = $(2 + 2)" # => "2 + 2 = 4"
|
|
||||||
catch ; end
|
|
||||||
# You can put any Julia expression inside the parentheses.
|
# You can put any Julia expression inside the parentheses.
|
||||||
|
|
||||||
# Another way to format strings is the printf macro from the stdlib Printf.
|
# Another way to format strings is the printf macro from the stdlib Printf.
|
||||||
@ -168,7 +157,7 @@ b[end] # => 6
|
|||||||
# 2-dimensional arrays use space-separated values and semicolon-separated rows.
|
# 2-dimensional arrays use space-separated values and semicolon-separated rows.
|
||||||
matrix = [1 2; 3 4] # => 2x2 Int64 Array: [1 2; 3 4]
|
matrix = [1 2; 3 4] # => 2x2 Int64 Array: [1 2; 3 4]
|
||||||
|
|
||||||
# Arrays of a particular Type
|
# Arrays of a particular type
|
||||||
b = Int8[4, 5, 6] # => 3-element Int8 Array: [4, 5, 6]
|
b = Int8[4, 5, 6] # => 3-element Int8 Array: [4, 5, 6]
|
||||||
|
|
||||||
# Add stuff to the end of a list with push! and append!
|
# Add stuff to the end of a list with push! and append!
|
||||||
@ -202,15 +191,17 @@ sort!(arr) # => [4,5,6]; arr is now [4,5,6]
|
|||||||
|
|
||||||
# Looking out of bounds is a BoundsError
|
# Looking out of bounds is a BoundsError
|
||||||
try
|
try
|
||||||
a[0] # => ERROR: BoundsError() in getindex at array.jl:270
|
a[0]
|
||||||
a[end + 1] # => ERROR: BoundsError() in getindex at array.jl:270
|
# => BoundsError: attempt to access 7-element Array{Int64,1} at index [0]
|
||||||
|
a[end + 1]
|
||||||
|
# => BoundsError: attempt to access 7-element Array{Int64,1} at index [8]
|
||||||
catch e
|
catch e
|
||||||
println(e)
|
println(e)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Errors list the line and file they came from, even if it's in the standard
|
# Errors list the line and file they came from, even if it's in the standard
|
||||||
# library. If you built Julia from source, you can look in the folder base
|
# library. You can look in the folder share/julia inside the julia folder to
|
||||||
# inside the julia folder to find these files.
|
# find these files.
|
||||||
|
|
||||||
# You can initialize arrays from ranges
|
# You can initialize arrays from ranges
|
||||||
a = [1:5;] # => 5-element Int64 Array: [1,2,3,4,5]
|
a = [1:5;] # => 5-element Int64 Array: [1,2,3,4,5]
|
||||||
@ -242,7 +233,7 @@ catch e
|
|||||||
println(e)
|
println(e)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Many list functions also work on tuples
|
# Many array functions also work on tuples
|
||||||
length(tup) # => 3
|
length(tup) # => 3
|
||||||
tup[1:2] # => (1,2)
|
tup[1:2] # => (1,2)
|
||||||
in(2, tup) # => true
|
in(2, tup) # => true
|
||||||
@ -266,19 +257,20 @@ empty_dict = Dict() # => Dict{Any,Any}()
|
|||||||
|
|
||||||
# You can create a dictionary using a literal
|
# You can create a dictionary using a literal
|
||||||
filled_dict = Dict("one" => 1, "two" => 2, "three" => 3)
|
filled_dict = Dict("one" => 1, "two" => 2, "three" => 3)
|
||||||
# => Dict{ASCIIString,Int64}
|
# => Dict{String,Int64}
|
||||||
|
|
||||||
# Look up values with []
|
# Look up values with []
|
||||||
filled_dict["one"] # => 1
|
filled_dict["one"] # => 1
|
||||||
|
|
||||||
# Get all keys
|
# Get all keys
|
||||||
keys(filled_dict)
|
keys(filled_dict)
|
||||||
# => KeyIterator{Dict{ASCIIString,Int64}}(["three"=>3,"one"=>1,"two"=>2])
|
# => Base.KeySet for a Dict{String,Int64} with 3 entries. Keys:
|
||||||
|
# "two", "one", "three"
|
||||||
# Note - dictionary keys are not sorted or in the order you inserted them.
|
# Note - dictionary keys are not sorted or in the order you inserted them.
|
||||||
|
|
||||||
# Get all values
|
# Get all values
|
||||||
values(filled_dict)
|
values(filled_dict)
|
||||||
# => ValueIterator{Dict{ASCIIString,Int64}}(["three"=>3,"one"=>1,"two"=>2])
|
# => Base.ValueIterator{Dict{String,Int64}} with 3 entries. Values: 2, 1, 3
|
||||||
# Note - Same as above regarding key ordering.
|
# Note - Same as above regarding key ordering.
|
||||||
|
|
||||||
# Check for existence of keys in a dictionary with in, haskey
|
# Check for existence of keys in a dictionary with in, haskey
|
||||||
@ -289,33 +281,33 @@ haskey(filled_dict, 1) # => false
|
|||||||
|
|
||||||
# Trying to look up a non-existent key will raise an error
|
# Trying to look up a non-existent key will raise an error
|
||||||
try
|
try
|
||||||
filled_dict["four"] # => ERROR: key not found: four in getindex at dict.jl:489
|
filled_dict["four"] # => KeyError: key "four" not found
|
||||||
catch e
|
catch e
|
||||||
println(e)
|
println(e)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Use the get method to avoid that error by providing a default value
|
# Use the get method to avoid that error by providing a default value
|
||||||
# get(dictionary,key,default_value)
|
# get(dictionary, key, default_value)
|
||||||
get(filled_dict, "one", 4) # => 1
|
get(filled_dict, "one", 4) # => 1
|
||||||
get(filled_dict, "four", 4) # => 4
|
get(filled_dict, "four", 4) # => 4
|
||||||
|
|
||||||
# Use Sets to represent collections of unordered, unique values
|
# Use Sets to represent collections of unordered, unique values
|
||||||
empty_set = Set() # => Set{Any}()
|
empty_set = Set() # => Set{Any}()
|
||||||
# Initialize a set with values
|
# Initialize a set with values
|
||||||
filled_set = Set([1,2,2,3,4]) # => Set{Int64}(1,2,3,4)
|
filled_set = Set([1, 2, 2, 3, 4]) # => Set([4, 2, 3, 1])
|
||||||
|
|
||||||
# Add more values to a set
|
# Add more values to a set
|
||||||
push!(filled_set, 5) # => Set{Int64}(5,4,2,3,1)
|
push!(filled_set, 5) # => Set([4, 2, 3, 5, 1])
|
||||||
|
|
||||||
# Check if the values are in the set
|
# Check if the values are in the set
|
||||||
in(2, filled_set) # => true
|
in(2, filled_set) # => true
|
||||||
in(10, filled_set) # => false
|
in(10, filled_set) # => false
|
||||||
|
|
||||||
# There are functions for set intersection, union, and difference.
|
# There are functions for set intersection, union, and difference.
|
||||||
other_set = Set([3, 4, 5, 6]) # => Set{Int64}(6,4,5,3)
|
other_set = Set([3, 4, 5, 6]) # => Set([4, 3, 5, 6])
|
||||||
intersect(filled_set, other_set) # => Set{Int64}(3,4,5)
|
intersect(filled_set, other_set) # => Set([4, 3, 5])
|
||||||
union(filled_set, other_set) # => Set{Int64}(1,2,3,4,5,6)
|
union(filled_set, other_set) # => Set([4, 2, 3, 5, 6, 1])
|
||||||
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([4, 1])
|
||||||
|
|
||||||
|
|
||||||
####################################################
|
####################################################
|
||||||
@ -356,8 +348,9 @@ end
|
|||||||
# cat is a mammal
|
# cat is a mammal
|
||||||
# mouse is a mammal
|
# mouse is a mammal
|
||||||
|
|
||||||
for a in Dict("dog" => "mammal", "cat" => "mammal", "mouse" => "mammal")
|
for pair in Dict("dog" => "mammal", "cat" => "mammal", "mouse" => "mammal")
|
||||||
println("$(a[1]) is a $(a[2])")
|
from, to = pair
|
||||||
|
println("$from is a $to")
|
||||||
end
|
end
|
||||||
# prints:
|
# prints:
|
||||||
# dog is a mammal
|
# dog is a mammal
|
||||||
@ -705,7 +698,7 @@ fight(Lion("RAR"), Lion("brown", "rarrr")) # => prints The lions come to a tie
|
|||||||
|
|
||||||
square_area(l) = l * l # square_area (generic function with 1 method)
|
square_area(l) = l * l # square_area (generic function with 1 method)
|
||||||
|
|
||||||
square_area(5) #25
|
square_area(5) # => 25
|
||||||
|
|
||||||
# What happens when we feed square_area an integer?
|
# What happens when we feed square_area an integer?
|
||||||
code_native(square_area, (Int32,))
|
code_native(square_area, (Int32,))
|
||||||
|
Loading…
Reference in New Issue
Block a user