Skip to content

Instantly share code, notes, and snippets.

@tomschenkjr
Created November 18, 2014 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomschenkjr/5532c664828df16138ca to your computer and use it in GitHub Desktop.
Save tomschenkjr/5532c664828df16138ca to your computer and use it in GitHub Desktop.
Notes from Julia Workshop: From Installed to Productive
# Notes from Julia Workshop: From Installed to Productive
# 2014-11-15
# http://www.meetup.com/JuliaChicago/events/216950712/
println("hello world") # => hello world
x = 5
## IF statements
if x == 4
println("x is four")
else
println("x is not four")
end
if x == 4
println("x is four")
elseif x == 5
println("x is not five")
end
x == 4 ? x : x + 2
# If x = 4, then
y = x == 4 ? x : x + 2
ans ## points to the previous output, unless prior output was error
julia> typeof(ans)
Nothing (constructor with 1 method) ## Nothing is a type
## FOR loops
for x = 1:5
println(x)
end
# This line block is the same as the following block
for x=1:5, y=6:10
println(x,",",y)
end
# This is the same as the preceeding line
for x=1:5,
y=6:10
println(x,",",y)
end
x % 3
x % 5
# FizzBuzz exercise
1, 2, Fizz, 4, Buzz, 6, 7, 8, ..., 14, FizzBuzz, 16, ..., 100
for x = 1:100
if x % 3 == 0 && x % 5 == 0
println("FizzBuzz")
elseif x % 3 == 0
println("Fizz")
elseif x % 5 == 0
println("Buzz")
else
println(x)
end
end
# Array
[1,2,3]
# Scalar addition
[1,2,3] + 2
# Element addition
[1,2,3] .+ 2
# Creates 2 x 3 matrix
[1 2 3; 4 5 6]
[1 2 3; 4 5 6] .* 2
# WHILE loops
while false
println("hi")
end
while true
println("hi")
break
end
# Anonymous function
## Create anonymous function
function (x,y)
x + y
end
## Calls previous function and creates an answer
ans(5,6)
# Generic function
##
x = + # Creates a function for 'x'
x(4,5) # Calls function x
# Dispatch functions
function foo(x,y)
x+y
end
function foo(x)
x + 2
end
foo(3,4) # Calls the former foo() function
foo(3) # Calls the latter foo() function
# Can redefine positional arguments
function barr(y; n=10)
return y + n
end
barr(3)
barr(3, n=10)
barr(3, n=11)
if false
function rainbow()
println("False")
end
else
function rainbow(x)
println("True")
end
end
methods(rainbow)
# User input
print("""Welcome to Guessing Game!
Please enter an integer:""")
readline()
parseint("55\n")
int("55\n") # Same as above
parseint("lkj\n") # Will break
try
int("55\n")
catch
println("Please enter a number")
end
# Take
guess = 55
# Logic to tell if a guess was correct
if guess < correct
"Your guess was too low"
elseif guess > correct
"Your guess was too high"
else
"You win!"
end
try
readline()
catch
println("Please enter a number")
end
# ask_user_for_guess() # => integer
# ask_user_to_play_again() # => bool
# give_user_feedback(guess,correct) # => nothing
# Create a function to let users guess a number and keep playing
function ask_user_to_play_again()
print("Would you like to play another game (y/n):")
answer = readline()
answer = lowercase(strip(answer))[1] # changes to lowercase, removes white space/newline, and takes first letter of answer
if answer == 'y'
return true
elseif answer == 'n'
return false
end
end
function ask_user_for_guess()
print("Please enter your guess:")
answer = parseint(readline())
end
function play_game()
keep_playing = true
while keep_playing
println("Welcome to Guessing Game!")
correct = 42
guess = ask_user_for_guess()
while guess != correct
if guess < correct
println("Your guess $guess was too low!") # Interpolates the value into the string
elseif guess > correct
println("Your guess $(guess) was too high!") # When using parens, you can further evaluate the interpolated values, i.e., $(guess + 2)
end
guess = ask_user_for_guess()
end
println("Yay! You won")
keep_playing = ask_user_to_play_again()
end
# Create a function to let users guess a random number and keep playing
function ask_user_to_play_again()
print("Would you like to play another game (y/n):")
answer = readline()
answer = lowercase(strip(answer))[1] # changes to lowercase, removes white space/newline, and takes first letter of answer
if answer == 'y'
return true
elseif answer == 'n'
return false
end
end
function ask_user_for_guess()
print("Please enter your guess between 1 and 100:")
answer = parseint(readline())
end
function play_game()
keep_playing = true
while keep_playing
println("Welcome to Guessing Game!")
correct = rand(1:100)
guess = ask_user_for_guess()
while guess != correct
if guess < correct
println("Your guess $guess was too low!") # Interpolates the value into the string
elseif guess > correct
println("Your guess $(guess) was too high!") # When using parens, you can further evaluate the interpolated values, i.e., $(guess + 2)
end
guess = ask_user_for_guess()
end
println("Yay! You won")
keep_playing = ask_user_to_play_again()
end
end
# Countdown from 10 to 1
for x=10:-1:1
print(x,",")
end
# Can look at names
x = 1:5
names(x)
type Foo
x
y
end
Foo(4,5)
f = Foo(4,5)
f.x
f.y
function Foo(x)
Foo(x,42000)
end
# immutable data types
immutable RGBColor
r
g
b
end
r = RGBColor(2,3,4)
r.r # => 2
r.b # => 3
r.g # => 4
r.r = "red" # => type RBGColor is immutable
r = RGBColor("red",3,4)
# Link list
[1,2,3] # Array
[1,2,3][1] # First Element
[1,2,3][end] # => 3
for x in [1,2,3]
println(x)
end
# Concept of link list: (1,->(2,->(3,\)))
type Node
# Elements within Node
data
next
end
Node(1,nothing)
Node(1,nothing).data
Node(1,nothing).next
Node(1,Node(2,Node(3,nothing)))
type LinkedList
head::Union(Node,Nothing)
end
LinkedList(nothing)
LinkedList(Node(1,nothing))
unshift!([2,3], 1)
arr = [2,3]
unshift!(arr,1)
push!(arr,4)
function shift!(ll::LinkedList)
current_head = ll.head
if ll.head != nothing
ll.head = ll.head.next
else
error("linked list must be non-empty")
end
return current_head.data
end
ll = LinkedList((Node(1,Node(2,nothing))))
function unshift!(ll:LinkedList, data::Any)
ll.head = Node(data,ll.head)
return ll
end
ll2 = deepcopy(ll) # copy data structure
for x in ll
println(x)
end
arr = [1,2,3]
s = start(arr)
done(arr, s)
(current, s) = next(arr,s)
function Base.start(ll::LinkedList)
ll.head
end
function Base.done(ll::LinkedList, n::Node)
return true
end
function Base.next(ll::LinkedList, n::Node)
return (n.data, n.next)
end
immutable ModInt1
k
n
ModInt1(k,n) = new(mod(k,n),n)
end
ModInt1(1,5)
ModInt1(5,5)
ModInt1(5,5).k = 10 # => ERROR: type ModInt1 is immutable
ModInt1(k) = ModInt1(k,5) # All arguments are now ModInt1(k)
ModInt1(3) # => ModInt1(3,5)
immutable ModInt2{N}
k
ModInt2(k) = new(mod(k,N))
end
immutable ModInt3{T}
k::T
n::T
ModInt3(k::T,n::T) = new(mod(k,n),n)
end
ModInt3{Int}(3,5)
# Forcing subtypes
arr = Number[x for x=1:3]
arr[1] = 3.2
arr = Any[x for x=1:3]
arr[1] = "read"
abstract Color
type BBBColor <: Color
b1
b2
b3
end
BBBColor(1,2,3)
# Package management
Pkg.status()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment