In [10]:
f(x) = 2x^2 + 3x + 1
g(x) = f(x) - (2x+1)*(x+1)


Out[10]:
g (generic function with 1 method)

In [11]:
α = 2


Out[11]:
2

In [12]:
f(3)


Out[12]:
28

In [13]:
g(2)


Out[13]:
0

In [14]:
x = 3
y = 6
print("x = $x")
print("y=$y")
println()
println("x = $x")
println("y = $y")


x = 3y=6
x = 3
y = 6

In [15]:
f(2)


Out[15]:
15

In [16]:
ans


Out[16]:
15

In [17]:
big(10)


Out[17]:
10

In [18]:
typeof(ans)


Out[18]:
BigInt (constructor with 10 methods)

In [19]:
methodswith(ans)


Out[19]:
124-element Array{Method,1}:

In [20]:
rem(big(10), big(3))


Out[20]:
1

In [21]:
a = big(10)


Out[21]:
10

In [22]:
ans


Out[22]:
10

In [23]:
a^19 / 3


Out[23]:
3.33333333333333333333333333333333333333333333333333333333333333333333333333332e+18 with 256 bits of precision

In [24]:
10^19


Out[24]:
-8446744073709551616

In [25]:
2^7


Out[25]:
128

In [26]:
//


Out[26]:
// (generic function with 8 methods)

In [27]:
methods(//)


Out[27]:
8 methods for generic function //:

In [28]:
3 + -(1, 2)


Out[28]:
2

In [29]:
methods(-)


Out[29]:
129 methods for generic function -:

In [30]:
l = [1, 2, "3"]


Out[30]:
3-element Array{Any,1}:
 1   
 2   
  "3"

In [31]:
l + 3


`.+` has no method matching .+(::ASCIIString, ::Int64)
while loading In[31], in expression starting on line 1

 in .+ at array.jl:761
 in + at array.jl:773

In [32]:
append!(l, 3)


`append!` has no method matching append!(::Array{Any,1}, ::Int64)
while loading In[32], in expression starting on line 1

In [33]:
?listen


INFO: Loading help data...
Base.listen([addr], port) -> TcpServer

   Listen on port on the address specified by "addr". By default
   this listens on localhost only. To listen on all interfaces pass,
   "IPv4(0)" or "IPv6(0)" as appropriate.

Base.listen(path) -> PipeServer

   Listens on/Creates a Named Pipe/Domain Socket

In [34]:
l[1:end]


Out[34]:
3-element Array{Any,1}:
 1   
 2   
  "3"

In [35]:
push!(l, 3)


Out[35]:
4-element Array{Any,1}:
 1   
 2   
  "3"
 3   

In [36]:
ll = [1, 2, [3, 5]]


Out[36]:
4-element Array{Int64,1}:
 1
 2
 3
 5

In [37]:
[1 2 3; 4 5 6]


Out[37]:
2x3 Array{Int64,2}:
 1  2  3
 4  5  6

In [38]:
transpose(reshape(ll, 2, 2))


Out[38]:
2x2 Array{Int64,2}:
 1  2
 3  5

In [39]:
methods(push!)


Out[39]:
22 methods for generic function push!:

In [40]:
t = methodswith(Array)


Out[40]:
103-element Array{Method,1}:

In [41]:
typeof(t)


Out[41]:
Array{Method,1}

In [42]:
t[1]


Out[42]:
.^(A::SparseMatrixCSC{Tv,Ti<:Integer},B::Array{T,N}) at sparse/sparsematrix.jl:662

In [43]:
a = [1, 2, 3]
b = [3.3, 5.6, 7.0]


Out[43]:
3-element Array{Float64,1}:
 3.3
 5.6
 7.0

In [44]:
c = cross(a, b)


Out[44]:
3-element Array{Float64,1}:
 -2.8
  2.9
 -1.0

In [45]:
dot(c, a)


Out[45]:
0.0

In [46]:
dot(c, b)


Out[46]:
3.552713678800501e-15

In [47]:
a'


Out[47]:
1x3 Array{Int64,2}:
 1  2  3

In [48]:
m = [1 2 3; 2 4 6]


Out[48]:
2x3 Array{Int64,2}:
 1  2  3
 2  4  6

In [49]:
m'


Out[49]:
3x2 Array{Int64,2}:
 1  2
 2  4
 3  6

In [50]:
M = reshape([1, 2, 3, 4, 5, 6], 2, 3)'


Out[50]:
3x2 Array{Int64,2}:
 1  2
 3  4
 5  6

In [51]:
a  b


Out[51]:
35.5

In [52]:
a × b


Out[52]:
3-element Array{Float64,1}:
 -2.8
  2.9
 -1.0

In [53]:
print_with_color(symbol("blue"), "Hello world!")


Hello world!

In [54]:
x_n = 0.01
for i = 1:1000
    x_n = x_n + (2-x_n^2)/(2*x_n);
end

In [55]:
x_n


Out[55]:
1.4142135623730951

In [56]:
tensor = [i+j+k for i = 1:10, j = 1:6, k = 1:5];

In [57]:
tensor[1, 2, :]


Out[57]:
1x1x5 Array{Int64,3}:
[:, :, 1] =
 4

[:, :, 2] =
 5

[:, :, 3] =
 6

[:, :, 4] =
 7

[:, :, 5] =
 8

In [58]:
big(zeros(3, 3))


Out[58]:
3x3 Array{BigFloat,2}:
 0e+00  0e+00  0e+00
 0e+00  0e+00  0e+00
 0e+00  0e+00  0e+00

In [59]:
?big


Base.big(x)

   Convert a number to a maximum precision representation (typically
   "BigInt" or "BigFloat"). See "BigFloat" for information about
   some pitfalls with floating-point numbers.

In [60]:
methods(big)


Out[60]:
9 methods for generic function big:

In [61]:
@which big(zeros(3, 3))


Out[61]:
big{T<:FloatingPoint,N}(x::AbstractArray{T<:FloatingPoint,N}) at abstractarray.jl:299

In [62]:
;ls


Fractoal_in_Julia.ipynb
Intro_to_Julia.ipynb
sierpinski.svg
try_julia.ipynb

In [63]:
run("ls")


`run` has no method matching run(::ASCIIString)
while loading In[63], in expression starting on line 1

In [64]:
run(`ls`)


Fractoal_in_Julia.ipynb
Intro_to_Julia.ipynb
sierpinski.svg
try_julia.ipynb

In [65]:
t = `ls`


Out[65]:
`ls`

In [66]:
typeof(t)


Out[66]:
Cmd (constructor with 1 method)

In [67]:
?Cmd


DataType   : Cmd (constructor with 1 method)
  supertype: AbstractCmd
  fields   : (:exec,:ignorestatus,:detach,:env,:dir)

In [68]:
"Hello"^2


Out[68]:
"HelloHello"

In [69]:
"Hello"*2


`*` has no method matching *(::ASCIIString, ::Int64)
while loading In[69], in expression starting on line 1

In [70]:
function *(a::String, t::Int)
    a^t
end


Out[70]:
* (generic function with 116 methods)

In [71]:
"Hello"*2


Out[71]:
"HelloHello"

In [72]:
super(BigInt)


Out[72]:
Integer

In [73]:
super(Integer)


Out[73]:
Real

In [74]:
super(Real)


Out[74]:
Number

In [75]:
super(Number)


Out[75]:
Any

In [76]:
t = super(Real)


Out[76]:
Number

In [77]:
typeof(t)


Out[77]:
DataType

In [78]:
t("3")


type cannot be constructed
while loading In[78], in expression starting on line 1

In [79]:
@which big(3)





In [80]:
methods(show)


Out[80]:
89 methods for generic function show:

In [81]:
a = 3


Out[81]:
3

In [82]:
show(a)


3

In [1]:
immutable Vector2D
    x::Float64
    y::Float64
end

In [2]:
methods(show)


Out[2]:
89 methods for generic function show:

In [3]:
show(io::IO, v::Vector2D) = println(io, "[$v.x, $v.y]")


error in method definition: function Base.show must be explicitly imported to be extended
while loading In[3], in expression starting on line 1

In [4]:
import Base.show

In [5]:
v = Vector2D(3, 5)


Out[5]:
Vector2D(3.0,5.0)

In [6]:
v


Out[6]:
Vector2D(3.0,5.0)

In [11]:
show(io::IO, v::Vector2D) = print(io, "[$(v.x), $(v.y)]")


Out[11]:
show (generic function with 90 methods)

In [12]:
v


Out[12]:
[3.0, 5.0]

In [1]:
immutable Vector2D{T <: Number}
    x::T
    y::T
end

In [3]:
v = Vector2D(3.6, 3.5)


Out[3]:
Vector2D{Float64}(3.6,3.5)

In [5]:
import Base.show
show(io::IO, v::Vector2D) = print(io, "[$(v.x), $(v.y)]")


Out[5]:
show (generic function with 90 methods)

In [6]:
show(v)


[3.6, 3.5]

In [7]:
v2 = Vector2D(3//4, 5//6)


Out[7]:
[3//4, 5//6]

In [9]:
@which show(v2)


Out[9]:
show(x) at show.jl:2

In [10]:
show{T}(io::IO, v::Vector2D{T}) = print(io, "[$(v.x), $(v.y)]")


Out[10]:
show (generic function with 91 methods)

In [11]:
@which show(v2)


Out[11]:
show(x) at show.jl:2

In [12]:
show(v2)


[3//4, 5//6]

In [13]:
using Gadfly


Gadfly not found
while loading In[13], in expression starting on line 1

 in require at loading.jl:47

In [14]:
Pkg.add("Gadfly")


INFO: Cloning cache of ArrayViews from git://github.com/JuliaLang/ArrayViews.jl.git
INFO: Cloning cache of Calculus from git://github.com/johnmyleswhite/Calculus.jl.git
INFO: Cloning cache of Codecs from git://github.com/dcjones/Codecs.jl.git
INFO: Cloning cache of Contour from git://github.com/tlycken/Contour.jl.git
INFO: Cloning cache of DataArrays from git://github.com/JuliaStats/DataArrays.jl.git
INFO: Cloning cache of DataFrames from git://github.com/JuliaStats/DataFrames.jl.git
INFO: Cloning cache of Distances from git://github.com/JuliaStats/Distances.jl.git
INFO: Cloning cache of Distributions from git://github.com/JuliaStats/Distributions.jl.git
INFO: Cloning cache of DualNumbers from git://github.com/JuliaDiff/DualNumbers.jl.git
INFO: Cloning cache of GZip from git://github.com/JuliaLang/GZip.jl.git
INFO: Cloning cache of Gadfly from git://github.com/dcjones/Gadfly.jl.git
INFO: Cloning cache of Grid from git://github.com/timholy/Grid.jl.git
INFO: Cloning cache of Hexagons from git://github.com/dcjones/Hexagons.jl.git
INFO: Cloning cache of ImmutableArrays from git://github.com/twadleigh/ImmutableArrays.jl.git
INFO: Cloning cache of KernelDensity from git://github.com/JuliaStats/KernelDensity.jl.git
INFO: Cloning cache of Loess from git://github.com/dcjones/Loess.jl.git
INFO: Cloning cache of NaNMath from git://github.com/mlubin/NaNMath.jl.git
INFO: Cloning cache of Optim from git://github.com/JuliaOpt/Optim.jl.git
INFO: Cloning cache of PDMats from git://github.com/JuliaStats/PDMats.jl.git
INFO: Cloning cache of Reexport from git://github.com/simonster/Reexport.jl.git
INFO: Cloning cache of Showoff from git://github.com/dcjones/Showoff.jl.git
INFO: Cloning cache of SortingAlgorithms from git://github.com/JuliaLang/SortingAlgorithms.jl.git
INFO: Cloning cache of StatsBase from git://github.com/JuliaStats/StatsBase.jl.git
INFO: Installing ArrayViews v0.4.8
INFO: Installing Calculus v0.1.6
INFO: Installing Codecs v0.1.3
INFO: Installing Contour v0.0.6
INFO: Installing DataArrays v0.2.11
INFO: Installing DataFrames v0.6.1
INFO: Installing Distances v0.2.0
INFO: Installing Distributions v0.6.3
INFO: Installing DualNumbers v0.1.2
INFO: Installing GZip v0.2.13
INFO: Installing Gadfly v0.3.11
INFO: Installing Grid v0.3.7
INFO: Installing Hexagons v0.0.2
INFO: Installing ImmutableArrays v0.0.6
INFO: Installing KernelDensity v0.1.0
INFO: Installing Loess v0.0.3
INFO: Installing NaNMath v0.0.2
INFO: Installing Optim v0.4.0
INFO: Installing PDMats v0.3.1
INFO: Installing Reexport v0.0.2
INFO: Installing Showoff v0.0.3
INFO: Installing SortingAlgorithms v0.0.4
INFO: Installing StatsBase v0.6.12
INFO: Package database updated
INFO: METADATA is out-of-date — you may not have the latest version of Gadfly
INFO: Use `Pkg.update()` to get the latest versions of your packages

In [1]:
using Gadfly

In [2]:
using DataFrames
using RDatasets

In [3]:
?dataset


INFO: Loading help data...
dataset (generic function with 1 method)

In [8]:
iris = dataset("datasets", "iris");
head(iris)


Out[8]:
SepalLengthSepalWidthPetalLengthPetalWidthSpecies
15.13.51.40.2setosa
24.93.01.40.2setosa
34.73.21.30.2setosa
44.63.11.50.2setosa
55.03.61.40.2setosa
65.43.91.70.4setosa

In [9]:
size(iris)


Out[9]:
(150,5)

In [10]:
typeof(iris)


Out[10]:
DataFrame (constructor with 11 methods)

In [22]:
plot(iris, x = "PetalWidth", y = "PetalLength", color = "Species", Geom.point)


Out[22]:
PetalWidth -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 -2.5 -2.4 -2.3 -2.2 -2.1 -2.0 -1.9 -1.8 -1.7 -1.6 -1.5 -1.4 -1.3 -1.2 -1.1 -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5.0 -2.5 0.0 2.5 5.0 -2.6 -2.4 -2.2 -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 3.2 3.4 3.6 3.8 4.0 4.2 4.4 4.6 4.8 5.0 setosa versicolor virginica Species -10 -8 -6 -4 -2 0 2 4 6 8 10 12 14 16 18 -8.0 -7.5 -7.0 -6.5 -6.0 -5.5 -5.0 -4.5 -4.0 -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0 10.5 11.0 11.5 12.0 12.5 13.0 13.5 14.0 14.5 15.0 15.5 16.0 -10 0 10 20 -8.0 -7.5 -7.0 -6.5 -6.0 -5.5 -5.0 -4.5 -4.0 -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0 10.5 11.0 11.5 12.0 12.5 13.0 13.5 14.0 14.5 15.0 15.5 16.0 PetalLength

In [16]:
a = [1, 2, 3]


Out[16]:
3-element Array{Int64,1}:
 1
 2
 3

In [18]:
a[end-1]


Out[18]:
2

In [23]:
@which a[1]


Out[23]:
getindex(A::Array{T,N},i0::Real) at array.jl:246

In [24]:
@which length(a)


Out[24]:
length(a::Array{T,N}) at array.jl:21

In [25]:
methodswith(Array)


Out[25]:
204-element Array{Method,1}:

In [11]:
function sum1(N::Int)
    total = 0 # A integer
    
    for i = 1:N
        total += i/2 # i/2 will be promoted to float, which makes total has to be promoted to float, too.
    end
    return total
end

function sum2(N::Int)
    total = 0.0 # Initialize total as float.
    
    for i = 1:N
        total += i/2
    end
    return total
end


Out[11]:
sum2 (generic function with 1 method)

In [17]:
@time sum1(1000000)


elapsed time: 0.086144781 seconds (32000080 bytes allocated, 55.17% gc time)
Out[17]:
2.5000025e11

In [18]:
@time sum2(1000000)


elapsed time: 0.001042188 seconds (96 bytes allocated)
Out[18]:
2.5000025e11

In [19]:
using PyCall

In [21]:
@pyimport requests

In [22]:
s = requests.session()


Out[22]:
PyObject <requests.sessions.Session object at 0x1123f5250>

In [30]:
ismatch(r"^\s*(?:#|$)", "\$ a comment")


Out[30]:
false

In [33]:
m = match(r"[0-9]","aaaa1aaaa2aaaa3",11)


Out[33]:
RegexMatch("3")

In [36]:
m.captures


Out[36]:
0-element Array{Union(Nothing,SubString{UTF8String}),1}

In [42]:
m = match(r"(a|b)(c)?(d)?", "bc")


Out[42]:
RegexMatch("bc", 1="b", 2="c", 3=nothing)

In [43]:
m.captures


Out[43]:
3-element Array{Union(Nothing,SubString{UTF8String}),1}:
 "b"    
 "c"    
 nothing

In [44]:
VERSION


Out[44]:
v"0.3.6"

In [45]:
a = [1, 3.0, 2]


Out[45]:
3-element Array{Float64,1}:
 1.0
 3.0
 2.0

In [46]:
@pyimport numpy as np


Warning: imported binding for transpose overwritten in module __anon__

In [47]:
M_py = np.matrix([[1, 2, 3], [4, 5, 6]])


Out[47]:
1x6 Array{Int64,2}:
 1  2  3  4  5  6

In [50]:
M_py = reshape(M_py, 2, 3)


Out[50]:
2x3 Array{Int64,2}:
 1  3  5
 2  4  6

In [56]:
Σ(x...) = sum(x)


Out[56]:
Σ (generic function with 1 method)

In [58]:
Σ(1, 2, 3, 4, 5, 6, 7)


Out[58]:
28

In [59]:
function test(a::Array, b::Any)
    push!(a, b)
end


Out[59]:
test (generic function with 1 method)

In [60]:
m = [1]
test(m, 2)
m


Out[60]:
2-element Array{Int64,1}:
 1
 2

In [62]:
m = [1 2 3; 4 5 6]


Out[62]:
2x3 Array{Int64,2}:
 1  2  3
 4  5  6

In [69]:
@which [(1, 2), (3, 4)]


Out[69]:
vcat{T}(X::T...) at abstractarray.jl:517

In [94]:
function test1(a = b, b=1)
    return a+b
end


Out[94]:
test1 (generic function with 3 methods)

In [89]:
b = 5


Out[89]:
5

In [91]:
test1()


Out[91]:
6

In [93]:
function test2(;a = b, b=1)
    return a+b
end


Out[93]:
test2 (generic function with 1 method)

In [95]:
test2()


b not defined
while loading In[95], in expression starting on line 1

In [96]:
function test3(b=1, a = b)
    return a+b
end


Out[96]:
test3 (generic function with 3 methods)

In [97]:
test3()


Out[97]:
2

In [104]:
if ζ == nothing
    println("nothing!")
end


ζ not defined
while loading In[104], in expression starting on line 1

In [108]:
@pyimport requests

In [109]:
s = requests.session()


Out[109]:
PyObject <requests.sessions.Session object at 0x11192fdd0>

In [110]:
getfield(s.o, :get)


type Ptr has no field get
while loading In[110], in expression starting on line 1

In [115]:
function return1()
    true
end


Out[115]:
return1 (generic function with 1 method)

In [116]:
function return2()
    false
end


Out[116]:
return2 (generic function with 1 method)

In [121]:
t = return1() || return1() && return2()


Out[121]:
true

In [122]:
t


Out[122]:
true

In [124]:
x = 1:5


Out[124]:
1:5

In [125]:
typeof(x)


Out[125]:
UnitRange{Int64} (constructor with 1 method)

In [128]:
for i in x
    println(i)
end


1
2
3
4
5

In [130]:
for i in 0:6
    println(i)
end


0
1
2
3
4
5
6

In [131]:
sizeof(x)


Out[131]:
16

In [132]:
y = [1:6]


Out[132]:
6-element Array{Int64,1}:
 1
 2
 3
 4
 5
 6

In [133]:
sizeof(y)


Out[133]:
48

In [134]:
?sizeof


sizeof(s::String)

   The number of bytes in string "s".

In [135]:
?filter


Base.filter(function, collection)

   Return a copy of "collection", removing elements for which
   "function" is false. For associative collections, the function is
   passed two arguments (key and value).

In [136]:
reverse("abcd")


Out[136]:
"dcba"

In [138]:
for i in 1:10
    for j in 1:30
        if j > 10
            break
        end
        println((i, j))
    end
end


(1,1)
(1,2)
(1,3)
(1,4)
(1,5)
(1,6)
(1,7)
(1,8)
(1,9)
(1,10)
(2,1)
(2,2)
(2,3)
(2,4)
(2,5)
(2,6)
(2,7)
(2,8)
(2,9)
(2,10)
(3,1)
(3,2)
(3,3)
(3,4)
(3,5)
(3,6)
(3,7)
(3,8)
(3,9)
(3,10)
(4,1)
(4,2)
(4,3)
(4,4)
(4,5)
(4,6)
(4,7)
(4,8)
(4,9)
(4,10)
(5,1)
(5,2)
(5,3)
(5,4)
(5,5)
(5,6)
(5,7)
(5,8)
(5,9)
(5,10)
(6,1)
(6,2)
(6,3)
(6,4)
(6,5)
(6,6)
(6,7)
(6,8)
(6,9)
(6,10)
(7,1)
(7,2)
(7,3)
(7,4)
(7,5)
(7,6)
(7,7)
(7,8)
(7,9)
(7,10)
(8,1)
(8,2)
(8,3)
(8,4)
(8,5)
(8,6)
(8,7)
(8,8)
(8,9)
(8,10)
(9,1)
(9,2)
(9,3)
(9,4)
(9,5)
(9,6)
(9,7)
(9,8)
(9,9)
(9,10)
(10,1)
(10,2)
(10,3)
(10,4)
(10,5)
(10,6)
(10,7)
(10,8)
(10,9)
(10,10)

In [139]:
for i in 1:10, j in 1:30
        if j > 10
            break
        end
        println((i, j))
end


(1,1)
(1,2)
(1,3)
(1,4)
(1,5)
(1,6)
(1,7)
(1,8)
(1,9)
(1,10)

In [141]:
typeof(DomainError)


Out[141]:
DataType

In [143]:
typeof(Number)


Out[143]:
DataType

In [144]:
info("Hello")


INFO: Hello

In [145]:
warn("It's not joking, I'm warning you")


WARNING: It's not joking, I'm warning you

In [146]:
error("Error!!!")


Error!!!
while loading In[146], in expression starting on line 1

 in error at error.jl:21

In [1]:
# Julia generater?
function generator(N::Int)
    for i in 1:N
        produce(i)
    end
end


Out[1]:
generator (generic function with 1 method)

In [159]:
g = @task generator(20)


Out[159]:
Task (runnable) @0x00007fd51d55cca0

In [160]:
consume(g)


Out[160]:
1

In [161]:
consume(g)


Out[161]:
2

In [167]:
consume(g)


Out[167]:
8

In [169]:
current_task()


Out[169]:
Task (runnable) @0x00007fd523382e00

In [170]:
g2 = @task generator(30)


Out[170]:
Task (runnable) @0x00007fd51f4e4060

In [172]:
consume(g2)


Out[172]:
1

In [173]:
current_task()


Out[173]:
Task (runnable) @0x00007fd523382e00

In [174]:



`delete!` has no method matching delete!(::Task)
while loading In[174], in expression starting on line 1

In [176]:


In [2]:
function fun_arry()
    sum = 0
    c = [1:100000]
    for i in c
        sum += i
    end
    sum
end


Out[2]:
fun_arry (generic function with 1 method)

In [3]:
function fun_gener()
    sum = 0
    g = @task generator(100000)
    for i in g
        sum += i
    end
    sum
end


Out[3]:
fun_gener (generic function with 1 method)

In [8]:
@time fun_arry()


elapsed time: 0.000667396 seconds (800144 bytes allocated)
Out[8]:
5000050000

In [9]:
@time fun_gener()


elapsed time: 0.318235352 seconds (4784288 bytes allocated)
Out[9]:
5000050000

In [20]:
I = [(i, j) for i=1:10, j=1:5];

In [33]:
## β can not be seen outside catch block since catch and try block will introduce new scope 
try
    β;
catch
    β = 3;
end


Out[33]:
3

In [34]:
β


β not defined
while loading In[34], in expression starting on line 1

In [35]:
## But catch block will return last line.
r = try
    β;
catch
    β = 3
end


Out[35]:
3

In [36]:
r


Out[36]:
3

In [27]:
## Or use global keyword.
try
    α
catch
    global α
    α = 3
end


Out[27]:
3

In [28]:
α


Out[28]:
3

In [39]:
γ = 0
try
    ζ
catch
    γ += 1
end


Out[39]:
1

In [40]:
γ


Out[40]:
1

In [37]:
for i in 1:3
    x = i
end

In [38]:
x


x not defined
while loading In[38], in expression starting on line 1

In [42]:
Fs = cell(2)


Out[42]:
2-element Array{Any,1}:
 #undef
 #undef

In [43]:
?cell


INFO: Loading help data...
Base.cell(dims)

   Construct an uninitialized cell array (heterogeneous array).
   "dims" can be either a tuple or a series of integer arguments.

In [44]:
push!(Fs, 1)
push!(Fs, "1")


Out[44]:
4-element Array{Any,1}:
 #undef
 #undef
   1   
    "1"

In [46]:
Fs[1] = [1, 2, 3]


Out[46]:
3-element Array{Int64,1}:
 1
 2
 3

In [47]:
Fs[2] = true


Out[47]:
true

In [48]:
Fs


Out[48]:
4-element Array{Any,1}:
     [1,2,3]
 true       
    1       
     "1"    

In [50]:
println(Fs[1])
println(Fs[2])
println(Fs[3])
println(Fs[4])


[1,2,3]
true
1
1

In [51]:
for item in Fs
    println(typeof(item))
end


Array{Int64,1}
Bool
Int64
ASCIIString

In [61]:
@time (1 + 2)::Int


elapsed time: 4.237e-6 seconds (80 bytes allocated)
Out[61]:
3

In [62]:
@time (1 + 2)


elapsed time: 4.104e-6 seconds (80 bytes allocated)
Out[62]:
3

In [74]:
function test(x)
    x += 1
end


Out[74]:
test (generic function with 2 methods)

In [75]:
methods(test)


Out[75]:
2 methods for generic function test:
  • test(x::Int64) at In[64]:2
  • test(x) at In[74]:2

In [77]:
?Dict


Base.Dict()

   "Dict{K,V}()" constructs a hash

   table with keys of type K and values of type V. The literal syntax
   is "{"A"=>1, "B"=>2}" for a "Dict{Any,Any}", or
   "["A"=>1, "B"=>2]" for a "Dict" of inferred type.

In [78]:
dd = Dict{String, Int16}()


Out[78]:
Dict{String,Int16} with 0 entries

In [80]:
dd["a"] = 16


Out[80]:
16

In [82]:
dd = {"A"=> 3}


Out[82]:
Dict{Any,Any} with 1 entry:
  "A" => 3

In [83]:
dd


Out[83]:
Dict{Any,Any} with 1 entry:
  "A" => 3

In [1]:
test(x::Number) = x+1


Out[1]:
test (generic function with 1 method)

In [2]:
@which test(2.5)


Out[2]:
test(x::Number) at In[1]:1

In [3]:
@which test(2)


Out[3]:
test(x::Number) at In[1]:1

In [4]:
?convert


INFO: Loading help data...
Base.convert(type, x)

   Try to convert "x" to the given type. Conversions from floating
   point to integer, rational to integer, and complex to real will
   raise an "InexactError" if "x" cannot be represented exactly in
   the new type.

In [5]:
convert(FloatingPoint, 1)


Out[5]:
1.0

In [6]:
subtypes(Number)


Out[6]:
2-element Array{Any,1}:
 Complex{T<:Real}
 Real            

In [10]:
subtypes(Integer)


Out[10]:
5-element Array{Any,1}:
 BigInt  
 Bool    
 Char    
 Signed  
 Unsigned

In [13]:
type MyComplex<:Complex
    im::Number
    real::Number
    name::String
end


invalid subtyping in definition of MyComplex
while loading In[13], in expression starting on line 1

In [16]:
isa(1, Number)


Out[16]:
true

In [17]:
type Foo
    bar
    baz::Int
    qux::Float64
end

In [18]:
foo = Foo("ss", 1, 3.5)


Out[18]:
Foo("ss",1,3.5)

In [20]:
typeof(foo)


Out[20]:
Foo (constructor with 2 methods)

In [26]:
repr(foo)


Out[26]:
"Foo(\"ss\",1,3.5)"

In [27]:
typeof(Top)


Out[27]:
UnionType

In [32]:
type Point{T}
  x::T
  y::T
end

In [33]:
Point{Float64}(1.0,2) # 2 will be converted to 2.0


Out[33]:
Point{Float64}(1.0,2.0)

In [34]:
Point(1.0, 2) # This will fail since neither the arguments does not provide sufficient information of 
              # this construction of Point type nor the T parameter has been explicitly declared


`Point{T}` has no method matching Point{T}(::Float64, ::Int64)
while loading In[34], in expression starting on line 1

In [39]:
?is


Base.is(x, y) -> Bool
===(x, y) -> Bool
≡(x, y) -> Bool

   Determine whether "x" and "y" are identical, in the sense that
   no program could distinguish them. Compares mutable objects by
   address in memory, and compares immutable objects (such as numbers)
   by contents at the bit level. This function is sometimes called
   "egal".

In [40]:
?isa


Base.isa(x, type) -> Bool

   Determine whether "x" is of the given "type".

In [42]:
?listen


Base.listen([addr], port) -> TcpServer

   Listen on port on the address specified by "addr". By default
   this listens on localhost only. To listen on all interfaces pass,
   "IPv4(0)" or "IPv6(0)" as appropriate.

Base.listen(path) -> PipeServer

   Listens on/Creates a Named Pipe/Domain Socket

In [43]:
p = listen("/Users/DboyLiao/pipe")


Out[43]:
PipeServer(active)

In [44]:
conn = accept(p)


interrupt
while loading In[44], in expression starting on line 1

In [50]:
if is(Int, Int64)
    typealias UInt Uint64
else
    typealias UInt Uint32
end


Out[50]:
Uint64

In [51]:
UInt


Out[51]:
Uint64

In [47]:
subtypes(Integer)


Out[47]:
5-element Array{Any,1}:
 BigInt  
 Bool    
 Char    
 Signed  
 Unsigned

In [48]:
Unsigned("2")


type cannot be constructed
while loading In[48], in expression starting on line 1

In [49]:
subtypes(Unsigned)


Out[49]:
5-element Array{Any,1}:
 Uint128
 Uint16 
 Uint32 
 Uint64 
 Uint8  

In [52]:
typealias Vector{T} Array{T, 1}


Out[52]:
Array{T,1}

In [58]:
Vector{Int} <: Vector{Float64}


Out[58]:
false

In [59]:
isa(None, Union())


Out[59]:
false

In [60]:
Union() == None


Out[60]:
true

In [63]:
function firstlast(b::Bool)
    return b ? "First" : "Last"
end

println(firstlast(true))


First

In [1]:
x1 = Nullable{Int}()


Nullable not defined
while loading In[1], in expression starting on line 1

In [4]:
function f(x, y)
    println("Dboy Liao")
end


Out[4]:
f (generic function with 1 method)

In [5]:
methods(f)


Out[5]:
1 method for generic function f:
  • f(x,y) at In[4]:2

In [6]:
function f(x::Float64, y::Float64)
    x + y
end


Out[6]:
f (generic function with 2 methods)

In [7]:
methods(f)


Out[7]:
2 methods for generic function f:
  • f(x::Float64,y::Float64) at In[6]:2
  • f(x,y) at In[4]:2

In [9]:
function js_closure()
    state = 1
    function add_one()
        state += 1
        state
    end
    return add_one
end


Out[9]:
js_closure (generic function with 1 method)

In [10]:
t = js_closure()


Out[10]:
add_one (generic function with 1 method)

In [12]:
t()


Out[12]:
3

In [13]:
t2 = js_closure()


Out[13]:
add_one (generic function with 1 method)

In [14]:
t2()


Out[14]:
2

In [15]:
Float64 <: Number


Out[15]:
true

In [9]:
type Incomplete
    xx
    Incomplete() = new()
end

In [10]:
z = Incomplete()


Out[10]:
Incomplete(#undef)

In [11]:
z.xx = 3


Out[11]:
3

In [12]:
z.xx


Out[12]:
3

In [1]:
type Node{T<:Number}
    value::T
    next::Node{T}
    Node{T}(value::T) = new(value)
end

In [2]:
n = Node{Int64}(4)


Out[2]:
Node{Int64}(4,#undef)

In [4]:
n.next = Node{Int64}(3)


Out[4]:
Node{Int64}(3,#undef)

In [5]:
n.next


Out[5]:
Node{Int64}(3,#undef)

In [6]:
type HasPlain
    n::Int
    HasPlain() = new()
end

In [8]:
a = HasPlain()


Out[8]:
HasPlain(4494843176)

In [9]:
a.n = 3


Out[9]:
3

In [10]:
type Point{T<:Real}
  x::T
  y::T

  Point(x,y) = new(x,y)
end

In [12]:
Point{T<:Real}(x::T, y::T) = Point{T}(x,y)


Out[12]:
Point{T<:Real} (constructor with 1 method)

In [13]:
Point(3, 3)


Out[13]:
Point{Int64}(3,3)

In [15]:
x = 12


Out[15]:
12

In [19]:
y = convert(Uint8, x);

In [18]:
typeof(x)


Out[18]:
Int64

In [21]:
print(y)


12

In [23]:
@which y + x


Out[23]:
+(x::Number,y::Number) at promotion.jl:158

In [27]:
ct(::Type{Bool}, x::Real) = (x!=0)


Out[27]:
ct (generic function with 1 method)

In [28]:
ct(Bool, 0.5)


Out[28]:
true

In [29]:
typeof(Type)


Out[29]:
DataType

In [34]:
parse("2")


Out[34]:
2

In [32]:
?parse


INFO: Loading help data...
Base.parse(str, start; greedy=true, raise=true)

   Parse the expression string and return an expression (which could
   later be passed to eval for execution). Start is the index of the
   first character to start parsing. If "greedy" is true (default),
   "parse" will try to consume as much input as it can; otherwise,
   it will stop as soon as it has parsed a valid expression.
   Incomplete but otherwise syntactically valid expressions will
   return "Expr(:incomplete, "(error message)")". If "raise" is
   true (default), syntax errors other than incomplete expressions
   will raise an error. If "raise" is false, "parse" will return
   an expression that will raise an error upon evaluation.

Base.parse(str; raise=true)

   Parse the whole string greedily, returning a single expression.  An
   error is thrown if there are additional characters after the first
   expression. If "raise" is true (default), syntax errors will
   raise an error; otherwise, "parse" will return an expression that
   will raise an error upon evaluation.

In [36]:
super(Type{Bool})


Out[36]:
Any

In [38]:
ct2(::Bool, x::Number) = (x!=0)


Out[38]:
ct2 (generic function with 2 methods)

In [42]:
NaN


Out[42]:
NaN

In [43]:
x = NaN


Out[43]:
NaN

In [44]:
0//0


invalid rational: 0//0
while loading In[44], in expression starting on line 1

 in Rational at rational.jl:6
 in // at rational.jl:15

In [45]:
?show


Base.show(x)

   Write an informative text representation of a value to the current
   output stream. New types should overload "show(io, x)" where the
   first argument is a stream. The representation used by "show"
   generally includes Julia-specific formatting and type information.

In [46]:
convert(Rational, 1.2)


Out[46]:
6//5

In [47]:
round(1.2)


Out[47]:
1.0

In [50]:
round(1/0.2)


Out[50]:
5.0

In [52]:
div(3, 2)


Out[52]:
1

In [53]:
?promote_rule


Base.promote_rule(type1, type2)

   Specifies what type should be used by "promote" when given values
   of types "type1" and "type2". This function should not be
   called directly, but should have definitions added to it for new
   types as appropriate.

In [55]:
t = Colon()


Out[55]:
Colon()

In [1]:
type MyType <: Number
    xx::Int
    yy::Int
end

In [2]:
isa(TypeConstructor, MyType)


Out[2]:
false

In [3]:
MyType(3, 4)


Out[3]:
MyType(3,4)

In [9]:
methods(typejoin)


Out[9]:
4 methods for generic function typejoin:

In [10]:
Pkg.add("Gadfly")


INFO: Cloning cache of ArrayViews from git://github.com/JuliaLang/ArrayViews.jl.git
INFO: Cloning cache of Calculus from git://github.com/johnmyleswhite/Calculus.jl.git
INFO: Cloning cache of Codecs from git://github.com/dcjones/Codecs.jl.git
INFO: Cloning cache of Color from git://github.com/JuliaLang/Color.jl.git
INFO: Cloning cache of Compose from git://github.com/dcjones/Compose.jl.git
INFO: Cloning cache of Contour from git://github.com/tlycken/Contour.jl.git
INFO: Cloning cache of DataArrays from git://github.com/JuliaStats/DataArrays.jl.git
INFO: Cloning cache of DataFrames from git://github.com/JuliaStats/DataFrames.jl.git
INFO: Cloning cache of DataStructures from git://github.com/JuliaLang/DataStructures.jl.git
INFO: Cloning cache of Dates from git://github.com/quinnj/Dates.jl.git
INFO: Cloning cache of Distances from git://github.com/JuliaStats/Distances.jl.git
INFO: Cloning cache of Distributions from git://github.com/JuliaStats/Distributions.jl.git
INFO: Cloning cache of Docile from git://github.com/MichaelHatherly/Docile.jl.git
INFO: Cloning cache of DualNumbers from git://github.com/JuliaDiff/DualNumbers.jl.git
INFO: Cloning cache of FixedPointNumbers from git://github.com/JeffBezanson/FixedPointNumbers.jl.git
INFO: Cloning cache of GZip from git://github.com/JuliaLang/GZip.jl.git
INFO: Cloning cache of Gadfly from git://github.com/dcjones/Gadfly.jl.git
INFO: Cloning cache of Graphics from git://github.com/JuliaLang/Graphics.jl.git
INFO: Cloning cache of Grid from git://github.com/timholy/Grid.jl.git
INFO: Cloning cache of Hexagons from git://github.com/dcjones/Hexagons.jl.git
INFO: Cloning cache of ImmutableArrays from git://github.com/twadleigh/ImmutableArrays.jl.git
INFO: Cloning cache of Iterators from git://github.com/JuliaLang/Iterators.jl.git
INFO: Cloning cache of KernelDensity from git://github.com/JuliaStats/KernelDensity.jl.git
INFO: Cloning cache of Loess from git://github.com/dcjones/Loess.jl.git
INFO: Cloning cache of NaNMath from git://github.com/mlubin/NaNMath.jl.git
INFO: Cloning cache of Optim from git://github.com/JuliaOpt/Optim.jl.git
INFO: Cloning cache of PDMats from git://github.com/JuliaStats/PDMats.jl.git
INFO: Cloning cache of Reexport from git://github.com/simonster/Reexport.jl.git
INFO: Cloning cache of Showoff from git://github.com/dcjones/Showoff.jl.git
INFO: Cloning cache of SortingAlgorithms from git://github.com/JuliaLang/SortingAlgorithms.jl.git
INFO: Cloning cache of StatsBase from git://github.com/JuliaStats/StatsBase.jl.git
INFO: Installing ArrayViews v0.4.10
INFO: Installing Calculus v0.1.6
INFO: Installing Codecs v0.1.3
INFO: Installing Color v0.4.4
INFO: Installing Compose v0.3.11
INFO: Installing Contour v0.0.6
INFO: Installing DataArrays v0.2.12
INFO: Installing DataFrames v0.6.2
INFO: Installing DataStructures v0.3.6
INFO: Installing Dates v0.3.2
INFO: Installing Distances v0.2.0
INFO: Installing Distributions v0.6.6
INFO: Installing Docile v0.4.8
INFO: Installing DualNumbers v0.1.2
INFO: Installing FixedPointNumbers v0.0.6
INFO: Installing GZip v0.2.14
INFO: Installing Gadfly v0.3.11
INFO: Installing Graphics v0.1.0
INFO: Installing Grid v0.3.7
INFO: Installing Hexagons v0.0.2
INFO: Installing ImmutableArrays v0.0.6
INFO: Installing Iterators v0.1.7
INFO: Installing KernelDensity v0.1.0
INFO: Installing Loess v0.0.3
INFO: Installing NaNMath v0.0.2
INFO: Installing Optim v0.4.0
INFO: Installing PDMats v0.3.1
INFO: Installing Reexport v0.0.2
INFO: Installing Showoff v0.0.3
INFO: Installing SortingAlgorithms v0.0.4
INFO: Installing StatsBase v0.6.13
INFO: Package database updated
INFO: METADATA is out-of-date — you may not have the latest version of Gadfly
INFO: Use `Pkg.update()` to get the latest versions of your packages

In [13]:
using MyModule

In [15]:
x()


Out[15]:
"x"

In [16]:
MyModule.p()


Out[16]:
"p"

In [17]:
import MyModule

In [18]:
MyModule.x


Out[18]:
x (generic function with 1 method)

In [19]:
?Pkg.add


INFO: Loading help data...
Base.Pkg.add(pkg, vers...)

   Add a requirement entry for "pkg" to "Pkg.dir("REQUIRE")" and
   call "Pkg.resolve()". If "vers" are given, they must be
   "VersionNumber" objects and they specify acceptable version
   intervals for "pkg".

In [21]:
Pkg.dir("REQUIRE")


Out[21]:
"/Users/DboyLiao/.julia/v0.3/REQUIRE"

In [22]:
LOAD_PATH


Out[22]:
2-element Array{Union(ASCIIString,UTF8String),1}:
 "/usr/local/Cellar/julia/0.3.6_1/local/share/julia/site/v0.3"
 "/usr/local/Cellar/julia/0.3.6_1/share/julia/site/v0.3"      

In [26]:
function gen_gen()
    state = 0
    function gen()
        state += 1
        state
    end
    return gen
end


Out[26]:
gen_gen (generic function with 1 method)

In [27]:
g = gen_gen()


Out[27]:
gen (generic function with 1 method)

In [31]:
import Main

In [36]:
Main.dirname("/User/DboyLiao")


Out[36]:
"/User"

In [37]:
homedir()


Out[37]:
"/Users/DboyLiao"

In [38]:
f(x, y) = x + y
Base.+ = f


syntax: unexpected "="
while loading In[38], in expression starting on line 2

In [39]:
prog = "1+1"


Out[39]:
"1+1"

In [40]:
ex1 = parse(prog)


Out[40]:
:(1 + 1)

In [41]:
typeof(ex1)


Out[41]:
Expr

In [45]:
typeof(ex1.args[1])


Out[45]:
Symbol

In [46]:
ex1.head


Out[46]:
:call

In [47]:
ex3 = parse("(4 + 4) / 2")


Out[47]:
:((4 + 4) / 2)

In [48]:
ex3.head


Out[48]:
:call

In [49]:
ex3.args


Out[49]:
3-element Array{Any,1}:
  :/      
  :(4 + 4)
 2        

In [50]:
ex3.typ


Out[50]:
Any

In [51]:
ex = Expr(:call, :+, :a, :b)


Out[51]:
:(a + b)

In [54]:
a = 1; b = 1;

In [55]:
eval(ex)


Out[55]:
2

In [61]:
@time 1 + 1


elapsed time: 5.082e-6 seconds (80 bytes allocated)
Out[61]:
2

In [63]:
@time 1+1


elapsed time: 3.834e-6 seconds (80 bytes allocated)
Out[63]:
2

In [64]:
Array(Int64, 0)


Out[64]:
0-element Array{Int64,1}

In [66]:
"" * "3" * "__"


Out[66]:
"3__"

In [67]:
ex = :(println(sin(3)))


Out[67]:
:(println(sin(3)))

In [71]:
quote
    $ex
end


Out[71]:
quote  # In[71], line 2:
    println(sin(3))
end

In [72]:
import Base

In [74]:
methods(Base.gc_bytes)


Out[74]:
1 method for generic function gc_bytes:

In [75]:
Base.gc_bytes()


Out[75]:
1385489984

In [1]:
macro until(cond, ex)
    quote
        while !($cond)
            $ex
        end
    end
end

In [7]:
x = 0
@time @until x>3 (println(x); x +=1;)


0
1
2
3
elapsed time: 0.001873346 seconds (15624 bytes allocated)

In [9]:
macroexpand(:(@time @until x<3 (println(x); x +=1;)))


Out[9]:
quote  # util.jl, line 53:
    local #408#b0 = Base.gc_bytes() # line 54:
    local #409#t0 = Base.time_ns() # line 55:
    local #410#g0 = Base.gc_time_ns() # line 56:
    local #411#val = begin  # In[1], line 3:
                while !(x < 3) # line 4:
                    begin 
                        println(x)
                        begin 
                            x += 1
                        end
                    end
                end
            end # line 57:
    local #412#g1 = Base.gc_time_ns() # line 58:
    local #413#t1 = Base.time_ns() # line 59:
    local #414#b1 = Base.gc_bytes() # line 60:
    Base.time_print(Base.-(#413#t1,#409#t0),Base.-(#414#b1,#408#b0),Base.-(#412#g1,#410#g0)) # line 61:
    #411#val
end

In [6]:
?esc


INFO: Loading help data...
Base.esc(e::ANY)

   Only valid in the context of an Expr returned from a macro.
   Prevents the macro hygiene pass from turning embedded variables
   into gensym variables. See the *Non-Standard String Literals*
   section of the Metaprogramming chapter of the manual for more
   details and examples.

In [11]:
Pkg.status()


2 required packages:
 - Gadfly                        0.3.11
 - IJulia                        0.2.1
39 additional packages:
 - ArrayViews                    0.4.10
 - BinDeps                       0.3.9
 - Calculus                      0.1.6
 - Codecs                        0.1.3
 - Color                         0.4.4
 - Compat                        0.3.6
 - Compose                       0.3.11
 - Contour                       0.0.6
 - DataArrays                    0.2.12
 - DataFrames                    0.6.2
 - DataStructures                0.3.6
 - Dates                         0.3.2
 - Distances                     0.2.0
 - Distributions                 0.6.6
 - Docile                        0.4.8
 - DualNumbers                   0.1.2
 - FixedPointNumbers             0.0.6
 - GZip                          0.2.14
 - Graphics                      0.1.0
 - Grid                          0.3.7
 - Hexagons                      0.0.2
 - Homebrew                      0.1.14
 - ImmutableArrays               0.0.6
 - Iterators                     0.1.7
 - JSON                          0.4.2
 - KernelDensity                 0.1.0
 - Loess                         0.0.3
 - NaNMath                       0.0.2
 - Nettle                        0.1.8
 - Optim                         0.4.0
 - PDMats                        0.3.1
 - REPLCompletions               0.0.3
 - Reexport                      0.0.2
 - SHA                           0.0.4
 - Showoff                       0.0.3
 - SortingAlgorithms             0.0.4
 - StatsBase                     0.6.13
 - URIParser                     0.0.5
 - ZMQ                           0.1.17

In [ ]: