Julia Programming Languege
Julia Programming Languege
####################################################
## 1. Primitive Datatypes and Operators
####################################################
# Bitwise Operators
~2 # => -3 # bitwise not
3 & 5 # => 1 # bitwise and
2 | 4 # => 6 # bitwise or
xor(2, 4) # => 6 # bitwise xor
2 >>> 1 # => 1 # logical shift right
2 >> 1 # => 1 # arithmetic shift right
2 << 1 # => 4 # logical/arithmetic shift left
# Boolean operators
!true # => false
!false # => true
1 == 1 # => true
2 == 1 # => false
1 != 1 # => false
2 != 1 # => true
1 < 10 # => true
1 > 10 # => false
2 <= 2 # => true
2 >= 2 # => true
# Comparisons can be chained, like in Python but unlike many other languages
1 < 2 < 3 # => true
2 < 3 < 2 # => false
# Strings are UTF8 encoded, so strings like "π" or "☃" are not directly equivalent
# to an array of single characters.
# Only if they contain only ASCII characters can they be safely indexed.
ascii("This is a string")[1] # => 'T'
# => 'T': ASCII/Unicode U+0054 (category Lu: Letter, uppercase)
# Beware, Julia indexes everything from 1 (like MATLAB), not 0 (like most
languages).
# Otherwise, iterating over strings is recommended (map, for loops, etc).
# Printing is easy
println("I'm Julia. Nice to meet you!") # => I'm Julia. Nice to meet you!
# Another way to format strings is the printf macro from the stdlib Printf.
using Printf # this is how you load (or import) a module
@printf "%d is less than %f\n" 4.5 5.3 # => 5 is less than 5.300000
####################################################
## 2. Variables and Collections
####################################################
# Function names that end in exclamations points indicate that they modify
# their argument.
arr = [5,4,6] # => 3-element Array{Int64,1}: [5,4,6]
sort(arr) # => [4,5,6]
arr # => [5,4,6]
sort!(arr) # => [4,5,6]
arr # => [4,5,6]
# Errors list the line and file they came from, even if it's in the standard
# library. You can look in the folder share/julia inside the julia folder to
# find these files.
# Use the get method to avoid that error by providing a default value
# get(dictionary, key, defaultValue)
get(filledDict, "one", 4) # => 1
get(filledDict, "four", 4) # => 4
# Assignment with `=` attaches a new label to the same value without copying
a = [1, 2, 3]
b = a
# Now `b` and `a` point to the same value, so changing one affects the other:
a[3] = 5
b[3] # => 5
####################################################
## 3. Control Flow
####################################################
for pair in Dict("dog" => "mammal", "cat" => "mammal", "mouse" => "mammal")
from, to = pair
println("$from is a $to")
end
# => mouse is a mammal
# => cat is a mammal
# => dog is a mammal
for (k, v) in Dict("dog" => "mammal", "cat" => "mammal", "mouse" => "mammal")
println("$k is a $v")
end
# => mouse is a mammal
# => cat is a mammal
# => dog is a mammal
####################################################
## 4. Functions
####################################################
add(5, 6)
# => x is 5 and y is 6
# => 11
all_the_args(1, 3, keywordArg=4)
# => normal arg: 1
# => optional arg: 3
# => keyword arg: 4
####################################################
## 5. Types
####################################################
# struct Name
# field::OptionalType
# ...
# end
struct Tiger
taillength::Float64
coatcolor # not including a type annotation is the same as `::Any`
end
# abstract Name
abstract type Cat end # just a name and point in the type hierarchy
# Every type has a super type; use the `supertype` function to get it.
typeof(5) # => Int64
supertype(Int64) # => Signed
supertype(Signed) # => Integer
supertype(Integer) # => Real
supertype(Real) # => Number
supertype(Number) # => Any
supertype(supertype(Signed)) # => Real
supertype(Any) # => Any
# All of these type, except for Int64, are abstract.
typeof("fire") # => String
supertype(String) # => AbstractString
# Likewise here with String
supertype(SubString) # => AbstractString
####################################################
## 6. Multiple-Dispatch
####################################################
function meow(animal::Panther)
"grrr"
end
function meow(animal::Tiger)
"rawwwr"
end
# Let's define a function with more arguments, so we can see the difference
function fight(t::Tiger, c::Cat)
println("The $(t.coatcolor) tiger wins!")
end
# => fight (generic function with 1 method)
# This warning is because it's unclear which fight will be called in:
try
fight(Lion("RAR"), Lion("brown", "rarrr"))
# => ERROR: MethodError: fight(::Lion, ::Lion) is ambiguous. Candidates:
# => fight(c::Cat, l::Lion) in Main at ...
# => fight(l::Lion, c::Cat) in Main at ...
# => Possible fix, define
# => fight(::Lion, ::Lion)
# => ...
catch e
println(e)
end
# The result may be different in other versions of Julia
square_area(5) # => 25
# Note that julia will use floating point instructions if any of the
# arguments are floats.
# Let's calculate the area of a circle
circle_area(r) = pi * r * r # circle_area (generic function with 1 method)
circle_area(5) # 78.53981633974483