summaryrefslogtreecommitdiffhomepage
path: root/julia.html.markdown
diff options
context:
space:
mode:
Diffstat (limited to 'julia.html.markdown')
-rw-r--r--julia.html.markdown55
1 files changed, 34 insertions, 21 deletions
diff --git a/julia.html.markdown b/julia.html.markdown
index cba7cd45..23d834f4 100644
--- a/julia.html.markdown
+++ b/julia.html.markdown
@@ -2,13 +2,14 @@
language: Julia
contributors:
- ["Leah Hanson", "http://leahhanson.us"]
+ - ["Pranit Bauva", "http://github.com/pranitbauva1997"]
filename: learnjulia.jl
---
Julia is a new homoiconic functional language focused on technical computing.
While having the full power of homoiconic macros, first-class functions, and low-level control, Julia is as easy to learn and use as Python.
-This is based on Julia 0.3.
+This is based on Julia 0.4.
```ruby
@@ -22,7 +23,7 @@ This is based on Julia 0.3.
## 1. Primitive Datatypes and Operators
####################################################
-# Everything in Julia is a expression.
+# Everything in Julia is an expression.
# There are several basic types of numbers.
3 # => 3 (Int64)
@@ -102,6 +103,11 @@ false
# Printing is easy
println("I'm Julia. Nice to meet you!")
+# String can be compared lexicographically
+"good" > "bye" # => true
+"good" == "good" # => true
+"1 + 2 = 3" == "1 + 2 = $(1+2)" # => true
+
####################################################
## 2. Variables and Collections
####################################################
@@ -145,12 +151,16 @@ a = Int64[] # => 0-element Int64 Array
# 1-dimensional array literals can be written with comma-separated values.
b = [4, 5, 6] # => 3-element Int64 Array: [4, 5, 6]
+b = [4; 5; 6] # => 3-element Int64 Array: [4, 5, 6]
b[1] # => 4
b[end] # => 6
-# 2-dimentional arrays use space-separated values and semicolon-separated rows.
+# 2-dimensional arrays use space-separated values and semicolon-separated rows.
matrix = [1 2; 3 4] # => 2x2 Int64 Array: [1 2; 3 4]
+# Arrays of a particular Type
+b = Int8[4, 5, 6] # => 3-element Int8 Array: [4, 5, 6]
+
# Add stuff to the end of a list with push! and append!
push!(a,1) # => [1]
push!(a,2) # => [1,2]
@@ -262,8 +272,8 @@ values(filled_dict)
# Note - Same as above regarding key ordering.
# Check for existence of keys in a dictionary with in, haskey
-in(("one", 1), filled_dict) # => true
-in(("two", 3), filled_dict) # => false
+in(("one" => 1), filled_dict) # => true
+in(("two" => 3), filled_dict) # => false
haskey(filled_dict, "one") # => true
haskey(filled_dict, 1) # => false
@@ -282,7 +292,7 @@ get(filled_dict,"four",4) # => 4
# Use Sets to represent collections of unordered, unique values
empty_set = Set() # => Set{Any}()
# Initialize a set with values
-filled_set = Set(1,2,2,3,4) # => Set{Int64}(1,2,3,4)
+filled_set = Set([1,2,2,3,4]) # => Set{Int64}(1,2,3,4)
# Add more values to a set
push!(filled_set,5) # => Set{Int64}(5,4,2,3,1)
@@ -292,7 +302,7 @@ in(2, filled_set) # => true
in(10, filled_set) # => false
# There are functions for set intersection, union, and difference.
-other_set = Set(3, 4, 5, 6) # => Set{Int64}(6,4,5,3)
+other_set = Set([3, 4, 5, 6]) # => Set{Int64}(6,4,5,3)
intersect(filled_set, other_set) # => Set{Int64}(3,4,5)
union(filled_set, other_set) # => Set{Int64}(1,2,3,4,5,6)
setdiff(Set(1,2,3,4),Set(2,3,5)) # => Set{Int64}(1,4)
@@ -390,6 +400,14 @@ end
add(5, 6) # => 11 after printing out "x is 5 and y is 6"
+# Compact assignment of functions
+f_add(x, y) = x + y # => "f (generic function with 1 method)"
+f_add(3, 4) # => 7
+
+# Function can also return multiple values as tuple
+f(x, y) = x + y, x - y
+f(3, 4) # => (7, -1)
+
# You can define functions that take a variable number of
# positional arguments
function varargs(args...)
@@ -402,14 +420,12 @@ varargs(1,2,3) # => (1,2,3)
# The ... is called a splat.
# We just used it in a function definition.
-# It can also be used in a fuction call,
+# It can also be used in a function call,
# where it will splat an Array or Tuple's contents into the argument list.
-Set([1,2,3]) # => Set{Array{Int64,1}}([1,2,3]) # produces a Set of Arrays
-Set([1,2,3]...) # => Set{Int64}(1,2,3) # this is equivalent to Set(1,2,3)
+add([5,6]...) # this is equivalent to add(5,6)
-x = (1,2,3) # => (1,2,3)
-Set(x) # => Set{(Int64,Int64,Int64)}((1,2,3)) # a Set of Tuples
-Set(x...) # => Set{Int64}(2,3,1)
+x = (5,6) # => (5,6)
+add(x...) # this is equivalent to add(5,6)
# You can define functions with optional positional arguments
@@ -531,12 +547,8 @@ abstract Cat # just a name and point in the type hierarchy
# Abstract types cannot be instantiated, but can have subtypes.
# For example, Number is an abstract type
-subtypes(Number) # => 6-element Array{Any,1}:
- # Complex{Float16}
- # Complex{Float32}
- # Complex{Float64}
+subtypes(Number) # => 2-element Array{Any,1}:
# Complex{T<:Real}
- # ImaginaryUnit
# Real
subtypes(Cat) # => 0-element Array{Any,1}
@@ -554,10 +566,11 @@ subtypes(AbstractString) # 8-element Array{Any,1}:
# Every type has a super type; use the `super` function to get it.
typeof(5) # => Int64
super(Int64) # => Signed
-super(Signed) # => Real
+super(Signed) # => Integer
+super(Integer) # => Real
super(Real) # => Number
super(Number) # => Any
-super(super(Signed)) # => Number
+super(super(Signed)) # => Real
super(Any) # => Any
# All of these type, except for Int64, are abstract.
typeof("fire") # => ASCIIString
@@ -723,7 +736,7 @@ code_native(square_area, (Float64,))
# ret
#
# Note that julia will use floating point instructions if any of the
-# arguements are floats.
+# arguments are floats.
# Let's calculate the area of a circle
circle_area(r) = pi * r * r # circle_area (generic function with 1 method)
circle_area(5) # 78.53981633974483