Numbers¶

In [ ]:
1 + 1

In [ ]:
1 + 1.0

In [ ]:
typeof(1)

In [ ]:
typeof(1.0)

In [ ]:
typeof(Float64)

In [ ]:
bits(1)

In [ ]:
bits(1.0)


Strings¶

In [ ]:
str1 = "Hello, world."

In [ ]:
str2 = "\u2200 x \u2203 y s.t. f(y) > x"

In [ ]:
typeof(str1)

In [ ]:
typeof(str2)

In [ ]:
xdump(str1)

In [ ]:
str2.data

In [ ]:
str2[1]

In [ ]:
str2[2]

In [ ]:
str2[3]

In [ ]:
str2[4]


Vectors and Matrices¶

In [ ]:
[1, 2, 3]

In [ ]:
[1 2;
3 4;]

In [ ]:
[1 2 3;
4 5 6;
7 8 9;] * [1, 2, 3]


Iteration¶

In [ ]:
itr = 1:5
for x in itr
println(x)
end

In [ ]:
itr = 1:5
state = start(itr)
while !done(itr,state)
x, state = next(itr,state)
println(x)
end


Math Constants¶

In [ ]:
pi

In [ ]:
typeof(pi)

In [ ]:
2pi

In [ ]:
big(2)*pi

In [ ]:
big(2pi)


BigInts and BigFloats (and Rounding Modes)¶

In [ ]:
big(1234)

In [ ]:
typeof(big(1234))

In [ ]:
big(2)^100

In [ ]:
(3//big(4))^80

In [ ]:
float((3//big(4))^80)

In [ ]:
set_bigfloat_precision(100)

In [ ]:
1/big(7)

In [ ]:
with_bigfloat_precision(90) do
1/big(7)
end

In [ ]:
get_bigfloat_precision()

In [ ]:
0.1 + 0.2

In [ ]:
with_rounding(RoundDown) do
0.1 + 0.2
end


Functions¶

In [ ]:
double(x) = x + x

In [ ]:
double(1)

In [ ]:
double(1.0)

In [ ]:
triple(x) = x + x + x

In [ ]:
triple(1)

In [ ]:
triple(1.0)

In [ ]:
id(x) = x

In [ ]:
id(1)

In [ ]:
id("a")

In [ ]:
function qux(x, y::Real = 1.0; z = pi)
return x + y + z
end

In [ ]:
qux(1)

In [ ]:
qux(1, 1.0, z=pi)

In [ ]:
qux(1, 2)

In [ ]:
qux(1, z = e)

In [ ]:
qux(1, 2, z = 3)

In [ ]:
qux(1, "a", z = 4)

In [ ]:
bar(x::Real) = x^2

In [ ]:
code_llvm(bar, (Int, ))

In [ ]:
code_llvm(bar, (Float64, ))

In [ ]:
function collatz(n::Integer)
path = Int[n]
while n != 1
if iseven(n)
n = fld(n, 2)
else
n = 3n + 1
end
push!(path, n)
end
return path
end

In [ ]:
collatz(3)

In [ ]:
code_llvm(collatz, (Int,))


Collections¶

In [ ]:
[1,2,3]

In [ ]:
[1,pi,3.5]

In [ ]:
[1,pi,"Hello"]

In [ ]:
["a" => 1, "b" => 2]

In [ ]:
typeof(["a" => 1, "b" => 2])

In [ ]:
{"a" => 1, "b" => 2}

In [ ]:
typeof({"a" => 1, "b" => 2})


Types¶

In [ ]:
{1, 2, 3}

In [ ]:
a = [x + y*im for x = randn(2), y = randn(2)]

In [ ]:
sizeof(a)

In [ ]:
sizeof(a[1])

In [ ]:
size(a)


Mutable Composite Types¶

In [ ]:
type Foo
x::Int64
y::Float64
end

In [ ]:
foo = Foo(1, 2.5)

In [ ]:
foo.x = 2

In [ ]:
foo2 = foo

In [ ]:
foo2.x = 3

In [ ]:
foo

In [ ]:
foos = [foo, foo, foo]

In [ ]:
sizeof(foos) # 3 x 64-bit pointers [to tagged, heap-allocated values]


Immutable Composite Types¶

In [ ]:
immutable Bar
x::Int64
y::Float64
end

In [ ]:
baz = Bar(1,2.5)

In [ ]:
baz.x = 2

In [ ]:
sizeof(baz)

In [ ]:
bazes = [baz, baz, baz]

In [ ]:
sizeof(bazes) # 3 x (8-byte Int64 + 8-byte Float64)


Abstract Types¶

In [ ]:
Int <: Number

In [ ]:
Float64 <: Number

In [ ]:
ASCIIString <: Number

In [ ]:
h(x::Number) = 1

In [ ]:
h(x::Float64) = 2

In [ ]:
h(x::Integer) = 3

In [ ]:
h(1), h(1.5), h(1+2im)

In [ ]:
z = 1 + 2im

In [ ]:
isa(z, Number)

In [ ]:
typeof(z)


Parametric Types¶

See base/complex.jl and base/rational.jl for nice built-in examples.

In [ ]:
z = Complex(1,2)

In [ ]:
typeof(z)

In [ ]:
Complex{Int}

In [ ]:
Complex

In [ ]:
Complex{Rational{Int}}

In [ ]:
rz = 1//2 + 3//4*im

In [ ]:
typeof(rz)

In [ ]:
rz + pi


Modules (Namespaces)¶

In [ ]:
module TMP
a = 1
b = "a"
c = [1, 2.0]
end

In [ ]:
a

In [ ]:
TMP.a


Packages¶

In [ ]:
require("Stats")

In [ ]:
Stats.gmean([1, 2, 3])

In [ ]:
gmean([1, 2, 3])

In [ ]:
using Stats

In [ ]:
gmean([1, 2, 3])