1 + 1
2
1 + 1.0
2.0
typeof(1)
Int64
typeof(1.0)
Float64
typeof(Float64)
DataType
bits(1)
"0000000000000000000000000000000000000000000000000000000000000001"
bits(1.0)
"0011111111110000000000000000000000000000000000000000000000000000"
str1 = "Hello, world."
"Hello, world."
str2 = "\u2200 x \u2203 y s.t. f(y) > x"
"∀ x ∃ y s.t. f(y) > x"
typeof(str1)
ASCIIString (constructor with 1 method)
typeof(str2)
UTF8String (constructor with 1 method)
xdump(str1)
ASCIIString data: Array(Uint8,(13,)) [0x48,0x65,0x6c,0x6c,0x6f,0x2c,0x20,0x77,0x6f,0x72,0x6c,0x64,0x2e]
str2.data
25-element Array{Uint8,1}: 0xe2 0x88 0x80 0x20 0x78 0x20 0xe2 0x88 0x83 0x20 0x79 0x20 0x73 0x2e 0x74 0x2e 0x20 0x66 0x28 0x79 0x29 0x20 0x3e 0x20 0x78
str2[1]
'∀'
str2[2]
invalid UTF-8 character index at In[15]:1 in getindex at utf8.jl:63
str2[3]
invalid UTF-8 character index at In[16]:1 in getindex at utf8.jl:63
str2[4]
' '
[1, 2, 3]
3-element Array{Int64,1}: 1 2 3
[1 2;
3 4;]
2x2 Array{Int64,2}: 1 2 3 4
[1 2 3;
4 5 6;
7 8 9;] * [1, 2, 3]
3-element Array{Int64,1}: 14 32 50
itr = 1:5
for x in itr
println(x)
end
1 2 3 4 5
itr = 1:5
state = start(itr)
while !done(itr,state)
x, state = next(itr,state)
println(x)
end
1 2 3 4 5
pi
π = 3.1415926535897...
typeof(pi)
MathConst{:π} (constructor with 1 method)
2pi
6.283185307179586
big(2)*pi
6.283185307179586476925286766559005768394338798750211641949889184615632812572396e+00 with 256 bits of precision
big(2pi)
6.28318530717958623199592693708837032318115234375e+00 with 256 bits of precision
big(1234)
1234
typeof(big(1234))
BigInt (constructor with 7 methods)
big(2)^100
1267650600228229401496703205376
(3//big(4))^80
147808829414345923316083210206383297601//1461501637330902918203684832716283019655932542976
float((3//big(4))^80)
1.011349051132674770188096856141860000460715719660739369037392074223812446723229e-10 with 256 bits of precision
set_bigfloat_precision(100)
100
1/big(7)
1.4285714285714285714285714285711e-01 with 100 bits of precision
with_bigfloat_precision(90) do
1/big(7)
end
1.4285714285714285714285714294e-01 with 90 bits of precision
get_bigfloat_precision()
100
0.1 + 0.2
0.30000000000000004
with_rounding(RoundDown) do
0.1 + 0.2
end
0.3
double(x) = x + x
double (generic function with 1 method)
double(1)
2
double(1.0)
2.0
triple(x) = x + x + x
triple (generic function with 1 method)
triple(1)
3
triple(1.0)
3.0
id(x) = x
id (generic function with 1 method)
id(1)
1
id("a")
"a"
function qux(x, y::Real = 1.0; z = pi)
return x + y + z
end
qux (generic function with 2 methods)
qux(1)
5.141592653589793
qux(1, 1.0, z=pi)
5.141592653589793
qux(1, 2)
6.141592653589793
qux(1, z = e)
4.718281828459045
qux(1, 2, z = 3)
6
qux(1, "a", z = 4)
no method qux(Array{Any,1},Int64,ASCIIString) at In[54]:1
bar(x::Real) = x^2
bar (generic function with 1 method)
code_llvm(bar, (Int, ))
define i64 @julia_bar(i64) { top: %1 = mul i64 %0, %0, !dbg !10813 ret i64 %1, !dbg !10813 }
code_llvm(bar, (Float64, ))
define double @julia_bar2288(double) { top: %1 = fmul double %0, %0, !dbg !10825 ret double %1, !dbg !10825 }
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 (generic function with 1 method)
collatz(3)
8-element Array{Int64,1}: 3 10 5 16 8 4 2 1
code_llvm(collatz, (Int,))
define %jl_value_t* @julia_collatz2292(i64) { top: %1 = alloca [5 x %jl_value_t*], align 8 %.sub = getelementptr inbounds [5 x %jl_value_t*]* %1, i64 0, i64 0 %2 = getelementptr [5 x %jl_value_t*]* %1, i64 0, i64 2, !dbg !10844 store %jl_value_t* inttoptr (i64 6 to %jl_value_t*), %jl_value_t** %.sub, align 8 %3 = load %jl_value_t*** @jl_pgcstack, align 8, !dbg !10844 %4 = getelementptr [5 x %jl_value_t*]* %1, i64 0, i64 1, !dbg !10844 %.c = bitcast %jl_value_t** %3 to %jl_value_t*, !dbg !10844 store %jl_value_t* %.c, %jl_value_t** %4, align 8, !dbg !10844 store %jl_value_t** %.sub, %jl_value_t*** @jl_pgcstack, align 8, !dbg !10844 store %jl_value_t* null, %jl_value_t** %2, align 8, !dbg !10844 %5 = getelementptr [5 x %jl_value_t*]* %1, i64 0, i64 3 store %jl_value_t* null, %jl_value_t** %5, align 8 %6 = getelementptr [5 x %jl_value_t*]* %1, i64 0, i64 4 store %jl_value_t* null, %jl_value_t** %6, align 8 %7 = load %jl_value_t** inttoptr (i64 4358252752 to %jl_value_t**), align 16, !dbg !10849 store %jl_value_t* %7, %jl_value_t** %5, align 8, !dbg !10849 %8 = call %jl_value_t* @jl_box_int64(i64 %0), !dbg !10849 store %jl_value_t* %8, %jl_value_t** %6, align 8, !dbg !10849 %9 = call %jl_value_t* @julia_getindex2291(%jl_value_t* inttoptr (i64 4480817760 to %jl_value_t*), %jl_value_t** %5, i32 2), !dbg !10849 store %jl_value_t* %9, %jl_value_t** %2, align 8, !dbg !10849 %10 = icmp eq i64 %0, 1, !dbg !10850 br i1 %10, label %L6, label %if, !dbg !10850 if: ; preds = %top, %L3 %n.07 = phi i64 [ %n.1, %L3 ], [ %0, %top ] %11 = and i64 %n.07, 1, !dbg !10851 %12 = icmp eq i64 %11, 0, !dbg !10851 br i1 %12, label %pass, label %L2, !dbg !10851 pass: ; preds = %if %13 = sdiv i64 %n.07, 2, !dbg !10852 %14 = lshr i64 %n.07, 63, !dbg !10852 %15 = and i64 %n.07, %14, !dbg !10852 %16 = sub i64 %13, %15, !dbg !10852 br label %L3, !dbg !10852 L2: ; preds = %if %17 = mul i64 %n.07, 3, !dbg !10853 %18 = add i64 %17, 1, !dbg !10853 br label %L3, !dbg !10853 L3: ; preds = %L2, %pass %n.1 = phi i64 [ %18, %L2 ], [ %16, %pass ] %19 = call %jl_value_t* @"julia_push!581"(%jl_value_t* %9, i64 %n.1), !dbg !10854 %20 = icmp eq i64 %n.1, 1, !dbg !10850 br i1 %20, label %L6, label %if, !dbg !10850 L6: ; preds = %L3, %top %21 = load %jl_value_t** %4, align 8, !dbg !10855 %22 = getelementptr inbounds %jl_value_t* %21, i64 0, i32 0, !dbg !10855 store %jl_value_t** %22, %jl_value_t*** @jl_pgcstack, align 8, !dbg !10855 ret %jl_value_t* %9, !dbg !10855 }
[1,2,3]
3-element Array{Int64,1}: 1 2 3
[1,pi,3.5]
3-element Array{Float64,1}: 1.0 3.14159 3.5
[1,pi,"Hello"]
3-element Array{Any,1}: 1 π = 3.1415926535897... "Hello"
["a" => 1, "b" => 2]
["b"=>2,"a"=>1]
typeof(["a" => 1, "b" => 2])
Dict{ASCIIString,Int64} (constructor with 2 methods)
{"a" => 1, "b" => 2}
{"b"=>2,"a"=>1}
typeof({"a" => 1, "b" => 2})
Dict{Any,Any} (constructor with 2 methods)
{1, 2, 3}
3-element Array{Any,1}: 1 2 3
a = [x + y*im for x = randn(2), y = randn(2)]
2x2 Array{Complex{Float64},2}: 0.430993-0.894536im 0.430993+0.0309993im 1.85966-0.894536im 1.85966+0.0309993im
sizeof(a)
64
sizeof(a[1])
16
size(a)
(2,2)
type Foo
x::Int64
y::Float64
end
foo = Foo(1, 2.5)
Foo(1,2.5)
foo.x = 2
2
foo2 = foo
Foo(2,2.5)
foo2.x = 3
3
foo
Foo(3,2.5)
foos = [foo, foo, foo]
3-element Array{Foo,1}: Foo(3,2.5) Foo(3,2.5) Foo(3,2.5)
sizeof(foos) # 3 x 64-bit pointers [to tagged, heap-allocated values]
24
immutable Bar
x::Int64
y::Float64
end
baz = Bar(1,2.5)
Bar(1,2.5)
baz.x = 2
type Bar is immutable at In[83]:1
sizeof(baz)
16
bazes = [baz, baz, baz]
3-element Array{Bar,1}: Bar(1,2.5) Bar(1,2.5) Bar(1,2.5)
sizeof(bazes) # 3 x (8-byte Int64 + 8-byte Float64)
48
Int <: Number
true
Float64 <: Number
true
ASCIIString <: Number
false
h(x::Number) = 1
h (generic function with 1 method)
h(x::Float64) = 2
h (generic function with 2 methods)
h(x::Integer) = 3
h (generic function with 3 methods)
h(1), h(1.5), h(1+2im)
(3,2,1)
z = 1 + 2im
1 + 2im
isa(z, Number)
true
typeof(z)
Complex{Int64} (constructor with 1 method)
See base/complex.jl
and base/rational.jl
for nice built-in examples.
z = Complex(1,2)
1 + 2im
typeof(z)
Complex{Int64} (constructor with 1 method)
Complex{Int}
Complex{Int64} (constructor with 1 method)
Complex
Complex{T<:Real} (constructor with 3 methods)
Complex{Rational{Int}}
Complex{Rational{Int64}} (constructor with 1 method)
rz = 1//2 + 3//4*im
1//2 + 3//4im
typeof(rz)
Complex{Rational{Int64}} (constructor with 1 method)
rz + pi
3.641592653589793 + 0.75im
module TMP
a = 1
b = "a"
c = [1, 2.0]
end
a
2x2 Array{Complex{Float64},2}: 0.430993-0.894536im 0.430993+0.0309993im 1.85966-0.894536im 1.85966+0.0309993im
TMP.a
1
require("Stats")
Stats.gmean([1, 2, 3])
1.8171205928321394
gmean([1, 2, 3])
gmean not defined at In[110]:1
using Stats
gmean([1, 2, 3])
1.8171205928321394