# Integers 1 # Floating point numbers 1.0 # Strings "a" # Complex numbers 1 + 2im # Vectors [1, 2, 3] # Matrices [1.0 2.0; 3.1 4.2] # Elementary functions sin(1.0) subtypes(Any)[1:4] subtypes(Number) subtypes(Real) subtypes(Integer) subtypes(None) sqrt(2) sqrt(2.0) 1 + 1 1.0 + 1.0 "wat" + 1 1 + "wat" "wat" - 1 1 - "wat" function foo(a, b) return a + b end foo(1, 2) foo(1.0, 2) bar(a, b) = a * b bar(2, 3) bar(1, "wat") bar("NaN", "NaN") folly(a::Integer, b::Integer) = 1 folly(a::Float64, b::Float64) = 2 folly(1, 2) folly(1.0, 2.0) type NewType a::Int b::UTF8String end x = NewType(1, "this is a string") x.a, x.b module NewNamespace c = 42 export c end c using NewNamespace c c = 41 NewNamespace.c foo(a, b) = a + b code_llvm(foo, (Int, Int)) code_llvm(foo, (Float64, Float64)) code_native(foo, (Int, Int)) code_native(foo, (Float64, Float64)) function foo() r = 0 for i in 1:12 r += 2 end return r end code_llvm(foo, ()) function fib{T <: Integer}(n::T) if n == zero(T) return zero(T) elseif n == one(T) return one(T) else a, b = zero(T), one(T) i = 1 while i < n a, b = b, a + b i += 1 end return b end end fib(95) fib(BigInt(95)) using StatsBase modes([1, 1, 2, 2, 3]) corspearman(rand(100), rand(100)) using DataArrays da = @data([1, 2, NA, 4]) using DataFrames df = DataFrame( A = [1, 2, 3], B = ["a", "b", "c"], C = [1//2, 3//4, 5//6] ) using Distributions srand(1) x = rand(Normal(10, 1), 10) pdf(Normal(10, 1), 1.0) loglikelihood(Normal(10, 1), x) using RDatasets iris = dataset("datasets", "iris") using Optim x = rand(Normal(31, 11), 1_000) function nll(theta) -loglikelihood(Normal(theta[1], exp(theta[2])), x) end nll([1.0, 1.0]) fit = optimize(nll, [0.0, 0.0]) theta = fit.minimum Normal(theta[1], exp(theta[2]))