The Julia REPL is modal. If you type a ? at the beginning of a line, you will enter help mode.
In [1]:
?cat
Out[1]:
Typing ; will enter a shell mode (not a full shell):
In [2]:
;pwd
In addition, the REPL has many of the usual line editing, tab completion, and history features we've come to expect.
Many things in Julia work like you'd expect:
In [3]:
1 + 2
Out[3]:
In [4]:
1. + 2
Out[4]:
In [5]:
1/2 # floating point division
Out[5]:
In [6]:
2^2 # ^, not **
Out[6]:
In [7]:
1//2 # Rational type, not integer division
Out[7]:
In [8]:
1 + 3 * im
Out[8]:
In [9]:
conj(1 + 3*im)
Out[9]:
In [10]:
(1 + 3im) * (2 + 4im)
Out[10]:
In [11]:
"This is a string!" # double quotes only
Out[11]:
In [12]:
't' # char
Out[12]:
Julia takes much of its notation for arrays from Matlab:
In [13]:
v = [1, 2, 3, 4] # array literal syntax
Out[13]:
In [14]:
vv = [1 2 3 4] # horizontal concatenation (hcat)
Out[14]:
In [15]:
vvv = [1 ; 2 ; 3 ; 4] # vertical concatenation (vcat)
Out[15]:
In [16]:
v[1] # indices start at 1, no negative indices (at least not for indexing from the end)
Out[16]:
In [17]:
v[2:end] # end keyword --> length(v)
Out[17]:
Slicing in Julia returns copy, not view (use sub or (v0.5) view)
Compat.jl == from __future__ import
In [18]:
A = [[1 2 3] ; [4 5 6]]
Out[18]:
In [19]:
A[:] # storage is column-major
Out[19]:
In [20]:
A' # transpose
Out[20]:
In [21]:
A + 1
Out[21]:
In [22]:
A * 2
Out[22]:
In [23]:
A * [0, 0, 1] # matrix multiplication
Out[23]:
In [24]:
A .* [0, 1] # elementwise
Out[24]:
In Julia, .<op> is elementwise <op>. In v0.5, this becomes syntactic sugar for map(<op>, ...).
In [25]:
A \ [5, 6] # backsolve: Ax = b ==> x = A \ b
Out[25]:
In [26]:
A ./ [5, 6]
Out[26]:
In [27]:
A ./ [3 4 5]
Out[27]:
In [28]:
A / diagm([1, 2, 3]) # diagm = make diagonal matrix; A / B = A * B^{-1}
Out[28]:
In [29]:
typeof(A)
Out[29]:
In [30]:
size(A)
Out[30]:
In [31]:
eltype(A)
Out[31]:
If we put disparate types in a list, Julia will try to figure out what common type is "big enough" for all of them:
In [32]:
[1.5, 1, -2, 3+2im]
Out[32]:
If we want to be able to put anything in a list, the element type of the list should be Any:
In [33]:
aa = [1, 2.5, 'f', "foo"]
Out[33]:
We can specify the type of a literal:
In [34]:
aa = Any[1, 2, 3]
Out[34]:
In [35]:
push!(aa, "foo") # ! means "mutating"
Out[35]:
In [36]:
[1, 2] + [3, 4] # not concatenation
Out[36]:
Julia has the basic data types we've come to know and love:
In [37]:
dd = Dict('a' => 1, 'b' => 2, 'c' => 3) # no curly brace literals
Out[37]:
In [38]:
keys(dd)
Out[38]:
In [39]:
values(dd)
Out[39]:
In [40]:
merge!(dd, Dict('z' => 26, 'y' => 25))
Out[40]:
In [41]:
dd
Out[41]:
In [42]:
ss = Set([1, 2, 3, 4])
Out[42]:
In [43]:
3 in ss, 5 in ss, 3 ∈ ss
Out[43]:
In [44]:
typeof(ss) # note: typed
Out[44]:
In [45]:
typeof((1.1, 2))
Out[45]:
In [46]:
typeof(('a', 2))
Out[46]:
In [47]:
aa, bb = (1, 2), (3, 4)
Out[47]:
In [48]:
(aa..., bb...) # splat
Out[48]:
In [49]:
[x^2 for x in 1:5]
Out[49]:
In [50]:
Dict(zip("abcde", 1:5))
Out[50]:
In [51]:
[(a, b) for a=1:5, b=6:10]
Out[51]:
In [52]:
for (k, v) in dd
println("key $k, value: $v")
end
In [ ]: