Skip to content

Latest commit

 

History

History
64 lines (43 loc) · 1.11 KB

julia-snippets.md

File metadata and controls

64 lines (43 loc) · 1.11 KB

Resources

http://blog.leahhanson.us/

Calling C and Fortran code is important for a scientific language due to legacy libraries, OpenMPI and other related work.

Manual - http://docs.julialang.org/en/release-0.2/manual/

Language

Types

(1+2)
(1+2)::Int

Loops

for x=1:5
  println(x)
end

Conditionals

if a == 5
  println("true")
end

Functions

Functions can have a number of methods (by defining the same function with different types)

Define a function with a body:

f(x::Float64, y::Float64) = 2x + y
f(x::Number, y::Number) = 2x - y

Inspect the function with:

f

Define a catch-all method for a function when a user passes an incorrect set of arguments:

f(x,y) = println("Whoa there, Nelly.")

Metaprogramming and Macros

http://docs.julialang.org/en/release-0.2/manual/metaprogramming/

macro time(ex)
  quote
    local t0 = time()
    local val = $ex
    local t1 = time()
    println("elapsed time: ", t1-t0, " seconds")
    val
  end
end