mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-11-23 06:03:07 +03:00
fix julia 0.7 deprecation warnings
This commit is contained in:
parent
912a51c8ba
commit
876e413558
@ -56,10 +56,10 @@ xor(2, 4) # => 6 # bitwise xor
|
|||||||
2 >> 1 # => 1 # arithmetic shift right
|
2 >> 1 # => 1 # arithmetic shift right
|
||||||
2 << 1 # => 4 # logical/arithmetic shift left
|
2 << 1 # => 4 # logical/arithmetic shift left
|
||||||
|
|
||||||
# You can use the bits function to see the binary representation of a number.
|
# You can use the bitstring function to see the binary representation of a number.
|
||||||
bits(12345)
|
bitstring(12345)
|
||||||
# => "0000000000000000000000000000000000000000000000000011000000111001"
|
# => "0000000000000000000000000000000000000000000000000011000000111001"
|
||||||
bits(12345.0)
|
bitstring(12345.0)
|
||||||
# => "0100000011001000000111001000000000000000000000000000000000000000"
|
# => "0100000011001000000111001000000000000000000000000000000000000000"
|
||||||
|
|
||||||
# Boolean values are primitives
|
# Boolean values are primitives
|
||||||
@ -107,8 +107,9 @@ try
|
|||||||
catch ; end
|
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.
|
# Another way to format strings is the printf macro from the stdlib Printf.
|
||||||
@printf "%d is less than %f" 4.5 5.3 # 4 is less than 5.300000
|
using Printf
|
||||||
|
@printf "%d is less than %f\n" 4.5 5.3 # => 5 is less than 5.300000
|
||||||
|
|
||||||
# Printing is easy
|
# Printing is easy
|
||||||
println("I'm Julia. Nice to meet you!")
|
println("I'm Julia. Nice to meet you!")
|
||||||
@ -128,7 +129,7 @@ some_var # => 5
|
|||||||
|
|
||||||
# Accessing a previously unassigned variable is an error
|
# Accessing a previously unassigned variable is an error
|
||||||
try
|
try
|
||||||
some_other_var # => ERROR: some_other_var not defined
|
some_other_var # => ERROR: UndefVarError: some_other_var not defined
|
||||||
catch e
|
catch e
|
||||||
println(e)
|
println(e)
|
||||||
end
|
end
|
||||||
@ -190,9 +191,9 @@ a[1] # => 1 # remember that Julia indexes from 1, not 0!
|
|||||||
# indexing expression
|
# indexing expression
|
||||||
a[end] # => 6
|
a[end] # => 6
|
||||||
|
|
||||||
# we also have shift and unshift
|
# we also have popfirst! and pushfirst!
|
||||||
shift!(a) # => 1 and a is now [2,4,3,4,5,6]
|
popfirst!(a) # => 1 and a is now [2,4,3,4,5,6]
|
||||||
unshift!(a, 7) # => [7,2,4,3,4,5,6]
|
pushfirst!(a, 7) # => [7,2,4,3,4,5,6]
|
||||||
|
|
||||||
# Function names that end in exclamations points indicate that they modify
|
# Function names that end in exclamations points indicate that they modify
|
||||||
# their argument.
|
# their argument.
|
||||||
@ -236,7 +237,7 @@ length(a) # => 8
|
|||||||
# Tuples are immutable.
|
# Tuples are immutable.
|
||||||
tup = (1, 2, 3) # => (1,2,3) # an (Int64,Int64,Int64) tuple.
|
tup = (1, 2, 3) # => (1,2,3) # an (Int64,Int64,Int64) tuple.
|
||||||
tup[1] # => 1
|
tup[1] # => 1
|
||||||
try:
|
try
|
||||||
tup[1] = 3 # => ERROR: no method setindex!((Int64,Int64,Int64),Int64,Int64)
|
tup[1] = 3 # => ERROR: no method setindex!((Int64,Int64,Int64),Int64,Int64)
|
||||||
catch e
|
catch e
|
||||||
println(e)
|
println(e)
|
||||||
@ -373,10 +374,11 @@ end
|
|||||||
# mouse is a mammal
|
# mouse is a mammal
|
||||||
|
|
||||||
# While loops loop while a condition is true
|
# While loops loop while a condition is true
|
||||||
x = 0
|
let x = 0
|
||||||
while x < 4
|
while x < 4
|
||||||
println(x)
|
println(x)
|
||||||
x += 1 # Shorthand for x = x + 1
|
x += 1 # Shorthand for x = x + 1
|
||||||
|
end
|
||||||
end
|
end
|
||||||
# prints:
|
# prints:
|
||||||
# 0
|
# 0
|
||||||
@ -530,13 +532,13 @@ typeof(DataType) # => DataType
|
|||||||
|
|
||||||
# Users can define types
|
# Users can define types
|
||||||
# They are like records or structs in other languages.
|
# They are like records or structs in other languages.
|
||||||
# New types are defined using the `type` keyword.
|
# New types are defined using the `struct` keyword.
|
||||||
|
|
||||||
# type Name
|
# struct Name
|
||||||
# field::OptionalType
|
# field::OptionalType
|
||||||
# ...
|
# ...
|
||||||
# end
|
# end
|
||||||
type Tiger
|
struct Tiger
|
||||||
taillength::Float64
|
taillength::Float64
|
||||||
coatcolor # not including a type annotation is the same as `::Any`
|
coatcolor # not including a type annotation is the same as `::Any`
|
||||||
end
|
end
|
||||||
@ -556,6 +558,7 @@ sherekhan = typeof(tigger)(5.6, "fire") # => Tiger(5.6,"fire")
|
|||||||
abstract type Cat end # just a name and point in the type hierarchy
|
abstract type Cat end # just a name and point in the type hierarchy
|
||||||
|
|
||||||
# Abstract types cannot be instantiated, but can have subtypes.
|
# Abstract types cannot be instantiated, but can have subtypes.
|
||||||
|
using InteractiveUtils # defines the subtype and supertype function
|
||||||
# For example, Number is an abstract type
|
# For example, Number is an abstract type
|
||||||
subtypes(Number) # => 2-element Array{Any,1}:
|
subtypes(Number) # => 2-element Array{Any,1}:
|
||||||
# Complex{T<:Real}
|
# Complex{T<:Real}
|
||||||
@ -563,13 +566,11 @@ subtypes(Number) # => 2-element Array{Any,1}:
|
|||||||
subtypes(Cat) # => 0-element Array{Any,1}
|
subtypes(Cat) # => 0-element Array{Any,1}
|
||||||
|
|
||||||
# AbstractString, as the name implies, is also an abstract type
|
# AbstractString, as the name implies, is also an abstract type
|
||||||
subtypes(AbstractString) # 6-element Array{Union{DataType, UnionAll},1}:
|
subtypes(AbstractString) # 4-element Array{Any,1}:
|
||||||
# Base.SubstitutionString
|
# String
|
||||||
# Base.Test.GenericString
|
# SubString
|
||||||
# DirectIndexString
|
# SubstitutionString
|
||||||
# RevString
|
# Test.GenericString
|
||||||
# String
|
|
||||||
# SubString
|
|
||||||
|
|
||||||
# Every type has a super type; use the `supertype` function to get it.
|
# Every type has a super type; use the `supertype` function to get it.
|
||||||
typeof(5) # => Int64
|
typeof(5) # => Int64
|
||||||
@ -584,10 +585,10 @@ supertype(Any) # => Any
|
|||||||
typeof("fire") # => String
|
typeof("fire") # => String
|
||||||
supertype(String) # => AbstractString
|
supertype(String) # => AbstractString
|
||||||
# Likewise here with String
|
# Likewise here with String
|
||||||
supertype(DirectIndexString) # => AbstractString
|
supertype(SubString) # => AbstractString
|
||||||
|
|
||||||
# <: is the subtyping operator
|
# <: is the subtyping operator
|
||||||
type Lion <: Cat # Lion is a subtype of Cat
|
struct Lion <: Cat # Lion is a subtype of Cat
|
||||||
mane_color
|
mane_color
|
||||||
roar::AbstractString
|
roar::AbstractString
|
||||||
end
|
end
|
||||||
@ -598,10 +599,10 @@ end
|
|||||||
Lion(roar::AbstractString) = Lion("green", roar)
|
Lion(roar::AbstractString) = Lion("green", roar)
|
||||||
# This is an outer constructor because it's outside the type definition
|
# This is an outer constructor because it's outside the type definition
|
||||||
|
|
||||||
type Panther <: Cat # Panther is also a subtype of Cat
|
struct Panther <: Cat # Panther is also a subtype of Cat
|
||||||
eye_color
|
eye_color
|
||||||
Panther() = new("green")
|
Panther() = new("green")
|
||||||
# Panthers will only have this constructor, and no default constructor.
|
# Panthers will only have this constructor, and no default constructor.
|
||||||
end
|
end
|
||||||
# Using inner constructors, like Panther does, gives you control
|
# Using inner constructors, like Panther does, gives you control
|
||||||
# over how values of the type can be created.
|
# over how values of the type can be created.
|
||||||
@ -636,9 +637,9 @@ meow(Lion("brown", "ROAAR")) # => "ROAAR"
|
|||||||
meow(Panther()) # => "grrr"
|
meow(Panther()) # => "grrr"
|
||||||
|
|
||||||
# Review the local type hierarchy
|
# Review the local type hierarchy
|
||||||
issubtype(Tiger, Cat) # => false
|
Tiger <: Cat # => false
|
||||||
issubtype(Lion, Cat) # => true
|
Lion <: Cat # => true
|
||||||
issubtype(Panther, Cat) # => true
|
Panther <: Cat # => true
|
||||||
|
|
||||||
# Defining a function that takes Cats
|
# Defining a function that takes Cats
|
||||||
function pet_cat(cat::Cat)
|
function pet_cat(cat::Cat)
|
||||||
|
Loading…
Reference in New Issue
Block a user