mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-06 14:56:54 +02:00
one more quick run over the code
This commit is contained in:
@@ -81,29 +81,18 @@ false
|
||||
2 < 3 < 2 # => false
|
||||
|
||||
# Strings are created with "
|
||||
try
|
||||
"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 '
|
||||
try
|
||||
'a'
|
||||
catch ; end
|
||||
|
||||
# Some strings can be indexed like an array of characters
|
||||
try
|
||||
"This is a string"[1] # => 'T' # Julia indexes from 1
|
||||
catch ; end
|
||||
# However, this is will not work well for UTF8 strings,
|
||||
# so iterating over strings is recommended (map, for loops, etc).
|
||||
# Strings are UTF8 encoded. Only if they contain only ASCII characters can
|
||||
# they be safely indexed.
|
||||
ascii("This is a string")[1] # => 'T' # Julia indexes from 1
|
||||
# Otherwise, iterating over strings is recommended (map, for loops, etc).
|
||||
|
||||
# $ can be used for string interpolation:
|
||||
try
|
||||
"2 + 2 = $(2 + 2)" # => "2 + 2 = 4"
|
||||
catch ; end
|
||||
# You can put any Julia expression inside the parentheses.
|
||||
|
||||
# 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.
|
||||
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]
|
||||
|
||||
# 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
|
||||
try
|
||||
a[0] # => ERROR: BoundsError() in getindex at array.jl:270
|
||||
a[end + 1] # => ERROR: BoundsError() in getindex at array.jl:270
|
||||
a[0]
|
||||
# => 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
|
||||
println(e)
|
||||
end
|
||||
|
||||
# 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
|
||||
# inside the julia folder to find these files.
|
||||
# library. You can look in the folder share/julia inside the julia folder to
|
||||
# find these files.
|
||||
|
||||
# You can initialize arrays from ranges
|
||||
a = [1:5;] # => 5-element Int64 Array: [1,2,3,4,5]
|
||||
@@ -242,7 +233,7 @@ catch e
|
||||
println(e)
|
||||
end
|
||||
|
||||
# Many list functions also work on tuples
|
||||
# Many array functions also work on tuples
|
||||
length(tup) # => 3
|
||||
tup[1:2] # => (1,2)
|
||||
in(2, tup) # => true
|
||||
@@ -266,19 +257,20 @@ empty_dict = Dict() # => Dict{Any,Any}()
|
||||
|
||||
# You can create a dictionary using a literal
|
||||
filled_dict = Dict("one" => 1, "two" => 2, "three" => 3)
|
||||
# => Dict{ASCIIString,Int64}
|
||||
# => Dict{String,Int64}
|
||||
|
||||
# Look up values with []
|
||||
filled_dict["one"] # => 1
|
||||
|
||||
# Get all keys
|
||||
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.
|
||||
|
||||
# Get all values
|
||||
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.
|
||||
|
||||
# Check for existence of keys in a dictionary with in, haskey
|
||||
@@ -289,7 +281,7 @@ haskey(filled_dict, 1) # => false
|
||||
|
||||
# Trying to look up a non-existent key will raise an error
|
||||
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
|
||||
println(e)
|
||||
end
|
||||
@@ -302,20 +294,20 @@ get(filled_dict, "four", 4) # => 4
|
||||
# Use Sets to represent collections of unordered, unique values
|
||||
empty_set = Set() # => Set{Any}()
|
||||
# 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
|
||||
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
|
||||
in(2, filled_set) # => true
|
||||
in(10, filled_set) # => false
|
||||
|
||||
# There are functions for set intersection, union, and difference.
|
||||
other_set = Set([3, 4, 5, 6]) # => Set{Int64}(6,4,5,3)
|
||||
intersect(filled_set, other_set) # => Set{Int64}(3,4,5)
|
||||
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)
|
||||
other_set = Set([3, 4, 5, 6]) # => Set([4, 3, 5, 6])
|
||||
intersect(filled_set, other_set) # => Set([4, 3, 5])
|
||||
union(filled_set, other_set) # => Set([4, 2, 3, 5, 6, 1])
|
||||
setdiff(Set([1,2,3,4]), Set([2,3,5])) # => Set([4, 1])
|
||||
|
||||
|
||||
####################################################
|
||||
@@ -356,8 +348,9 @@ end
|
||||
# cat is a mammal
|
||||
# mouse is a mammal
|
||||
|
||||
for a in Dict("dog" => "mammal", "cat" => "mammal", "mouse" => "mammal")
|
||||
println("$(a[1]) is a $(a[2])")
|
||||
for pair in Dict("dog" => "mammal", "cat" => "mammal", "mouse" => "mammal")
|
||||
from, to = pair
|
||||
println("$from is a $to")
|
||||
end
|
||||
# prints:
|
||||
# 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(5) #25
|
||||
square_area(5) # => 25
|
||||
|
||||
# What happens when we feed square_area an integer?
|
||||
code_native(square_area, (Int32,))
|
||||
|
Reference in New Issue
Block a user