1 + 1
1 + 1.0
typeof(1)
typeof(1.0)
typeof(Float64)
bits(1)
bits(1.0)
str1 = "Hello, world."
str2 = "\u2200 x \u2203 y s.t. f(y) > x"
typeof(str1)
typeof(str2)
xdump(str1)
str2.data
str2[1]
str2[2]
str2[3]
str2[4]
[1, 2, 3]
[1 2;
3 4;]
[1 2 3;
4 5 6;
7 8 9;] * [1, 2, 3]
itr = 1:5
for x in itr
println(x)
end
itr = 1:5
state = start(itr)
while !done(itr,state)
x, state = next(itr,state)
println(x)
end
pi
typeof(pi)
2pi
big(2)*pi
big(2pi)
big(1234)
typeof(big(1234))
big(2)^100
(3//big(4))^80
float((3//big(4))^80)
set_bigfloat_precision(100)
1/big(7)
with_bigfloat_precision(90) do
1/big(7)
end
get_bigfloat_precision()
0.1 + 0.2
with_rounding(RoundDown) do
0.1 + 0.2
end
double(x) = x + x
double(1)
double(1.0)
triple(x) = x + x + x
triple(1)
triple(1.0)
id(x) = x
id(1)
id("a")
function qux(x, y::Real = 1.0; z = pi)
return x + y + z
end
qux(1)
qux(1, 1.0, z=pi)
qux(1, 2)
qux(1, z = e)
qux(1, 2, z = 3)
qux(1, "a", z = 4)
bar(x::Real) = x^2
code_llvm(bar, (Int, ))
code_llvm(bar, (Float64, ))
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
collatz(3)
code_llvm(collatz, (Int,))
[1,2,3]
[1,pi,3.5]
[1,pi,"Hello"]
["a" => 1, "b" => 2]
typeof(["a" => 1, "b" => 2])
{"a" => 1, "b" => 2}
typeof({"a" => 1, "b" => 2})
{1, 2, 3}
a = [x + y*im for x = randn(2), y = randn(2)]
sizeof(a)
sizeof(a[1])
size(a)
type Foo
x::Int64
y::Float64
end
foo = Foo(1, 2.5)
foo.x = 2
foo2 = foo
foo2.x = 3
foo
foos = [foo, foo, foo]
sizeof(foos) # 3 x 64-bit pointers [to tagged, heap-allocated values]
immutable Bar
x::Int64
y::Float64
end
baz = Bar(1,2.5)
baz.x = 2
sizeof(baz)
bazes = [baz, baz, baz]
sizeof(bazes) # 3 x (8-byte Int64 + 8-byte Float64)
Int <: Number
Float64 <: Number
ASCIIString <: Number
h(x::Number) = 1
h(x::Float64) = 2
h(x::Integer) = 3
h(1), h(1.5), h(1+2im)
z = 1 + 2im
isa(z, Number)
typeof(z)
See base/complex.jl
and base/rational.jl
for nice built-in examples.
z = Complex(1,2)
typeof(z)
Complex{Int}
Complex
Complex{Rational{Int}}
rz = 1//2 + 3//4*im
typeof(rz)
rz + pi
module TMP
a = 1
b = "a"
c = [1, 2.0]
end
a
TMP.a
require("Stats")
Stats.gmean([1, 2, 3])
gmean([1, 2, 3])
using Stats
gmean([1, 2, 3])