Skip to content

Instantly share code, notes, and snippets.

@dfdx
Created July 28, 2016 17:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dfdx/a8a47273ef694647d0390629ecc68cbf to your computer and use it in GitHub Desktop.
Save dfdx/a8a47273ef694647d0390629ecc68cbf to your computer and use it in GitHub Desktop.
Julia Workshop
################ BASICS #################
# dynamic typing
x = 3 # => 3 (Int64)
x = 3.2 # => 3.2 (Float64)
x = 2 + 1im # => 2 + 1im (Complex{Int64})
x = 2//3 # => 2//3 (Rational{Int64})
# parametric types
d = Dict{UTF8String, Int64}()
d["answer"] = 42
d[42] = "answer"
# arrays
v = [1, 2, 3]
m = [1 2 3;
4 5 6]
a = reshape(1:27, 3, 3, 3)
sizeof(a) == 27 * 8
# composed types
type Point
x::Float64
y::Float64
end
p = Point(150, 320)
p.x = 12
sizeof(p)
immutable ImmutablePoint
x::Float64
y::Float64
end
ip = ImmutablePoint(150, 320)
ip.x = 12
# type parameters (generics) in types
type Point2{T}
x::T
y::T
end
p2 = Point2(2. + 1im, 3. - 2im)
p2.x
# typealiases
typealias ComplexPoint Point2{Complex}
ComplexPoint
Vector{Int}
Matrix
# control flow
if length(a) == 27
println("hello, Adform")
else
println("Go doesn't have generics, I do")
end
for i in 1:length(v)
println("element №$i is $(v[i])")
end
for i=1:length(v)
println("element №$i is $(v[i])")
end
# exception handling
try
v[1000]
catch e
println("Got error: $e")
end
############ FUNCTIONS AND TYPE SPECIALIZATION #############
# functions: notation
function double(x)
return 2 * x
end
double(x) = 2 * x
# functions: methods and type specialization
function add(x::Number, y::Number)
return x + y
end
function add(x::Vector{Float64}, y::Vector{Float64})
result = Array(Float64, length(x))
for i=1:length(x)
result[i] = x[i] + y[i]
end
end
function add(x, y)
error("don't know how to add objects of types $(typeof(x)) and $(typeof(y))")
end
add(4, 3)
add([1., 2., 3.], [4., 5., 6.])
add("bad", "args")
# type parameters (generics) in functions
function add{T}(x::Vector{T}, y::Vector{T})
result = Array(T, length(x))
for i=1:length(x)
result[i] = x[i] + y[i]
end
return result
end
# functions: extending existing functions
function Base.show(io::IO, p::Point)
print(io, "I am Point!")
end
############## SPEED ###################
const v1 = rand(1000)
const v2 = rand(1000)
@time for i=1:100_000 add(v1, v2) end
function add_inbounds{T}(x::Vector{T}, y::Vector{T})
result = Array(T, length(x))
@inbounds for i=1:length(x)
result[i] = x[i] + y[i]
end
return result
end
function add_inbounds!{T}(result::Vector{T}, x::Vector{T}, y::Vector{T})
# result = Array(T, length(x))
@inbounds for i=1:length(x)
result[i] = x[i] + y[i]
end
return result
end
@time for i=1:100_000 add_inbounds!(v2, v1, v2) end
function add_simd{T}(x::Vector{T}, y::Vector{T})
result = Array(T, length(x))
@simd for i=1:length(x)
result[i] = x[i] + y[i]
end
return result
end
@time for i=1:100_000 add_simd(v1, v2) end
function add_blas{T}(x::Vector{T}, y::Vector{T})
BLAS.axpy!(1.0, x, y)
end
@time for i=1:100_000 add_blas(v1, v2) end
############# METAPROGRAMMING #################
# symbols and expressions
s = :something
typeof(s)
eval(s)
x = 42
s = :x
eval(s)
ex = :(x + 1)
typeof(ex)
eval(ex)
ex = quote
x + 1
end
eval(ex)
ex2 = quote
$x + 1
end
ex2
# symbols and expressions: real-life example
for op in [:log, :exp, :sqrt, :round]
@eval function Base.$(op)(a::Array)
result = Array(eltype(a), length(a))
for i=1:length(a)
result[i] = $op(a[i])
end
return result
end
end
log([1., 2., 3.])
# macros
macro mytime(ex)
return quote
tic()
result = $ex
toc()
result
end
end
macroexpand(:(@mytime println("Macros rule!")))
@mytime println("Macros rule!")
########### CONCURRENCY AND PARALLELISM ###############
# concurrency (tasks == coroutines)
tsk = @task println("Ok, Coroutine")
consume(tsk)
tsk = @task begin
for i=1:5
produce(i)
end
end
@async println("Ok, Coroutine")
tsk = @async begin
server = listen(2000)
while true
sock = accept(server)
println("Hello World\n")
end
end
client = connect(2000)
using Requests
@time for i=1:10
Requests.get("http://www.google.com")
end
@time @sync for i=1:10
@async Requests.get("http://www.google.com")
end
# channels
ch = Channel{Float64}(10)
for i=1:10
@async put!(ch, rand())
end
for i=1:10
println(take!(ch))
end
# parallelism (processes and ClusterManager)
workers()
addprocs(3)
workers()
r = @spawn rand()
fetch(r)
@parallel for i=1:10
println(i)
end
@parallel (+) for i=1:10
i
end
addprocs(["127.0.0.1"])
workers()
############ CALLING OTHER LANGUAGES #################
# interop with C
# same as `clock()` in C
t = ccall((:clock, "libc"), Int32, ())
# interop with Java
# Pkg.add("JavaCall")
using JavaCall
JavaCall.init(["-Xmx128M"])
JURL = @jimport java.net.URL
url = JURL((JString,), "http://www.google.com")
jcall(url, "getHost", JString, ())
################## LEARNING ########################
# introspection
fieldnames(p)
methods(add)
methodswith(Expr)
@doc log
@edit jcall(url, "getHost", JString, ())
############ PACKAGE DEVELOPMENT ###################
# Pkg.add("NewPackage")
# Pkg.update()
# Pkg.generate("MyCoolPackage", "MIT")
# Pkg.checkout("MyCookPackage")
# Pkg.build("MyCoolPackage")
# Pkg.register("MyCoolPackage")
# Pkg.tag("MyCoolPackage", :minor)
# Pkg.publish()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment