There's a rapidly growing package ecosystem. See http://pkg.julialang.org
In [3]:
# "using" exports certain things from module into current namespace
# "import" does not.
using DustExtinction
import DustExtinction
In [4]:
DustExtinction.ccm89(4000., 3.1)
Out[4]:
In [2]:
ccm89(4000.0, 3.1)
Out[2]:
In [7]:
# C signature:
# char *getenv(const char *name)
#
# function library return type input types input values
path = ccall((:getenv, "libc"), Ptr{Uint8}, (Ptr{Uint8},), "SHELL")
bytestring(path)
Out[7]:
Macros are functions operate on expressions (code) rather than values: whereas a function takes input value(s), say 3
and returns some output value, say 9, a macro takes input expression(s), say x
and returns an output expression, say x^2
.
One might also say that macros rewrite or generate code.
Here is an example of why we might want to do this:
In [9]:
x = rand(5)
# suppose we want to time an element-wise square
t1 = time_ns()
x.^2
t2 = time_ns()
println(t2-t1, " nanoseconds")
In [10]:
@time x.^2
Out[10]:
In [16]:
macroexpand(:(@time x.^2))
Out[16]:
Macros can also be used to generate repetitive blocks of code. This is used extensively in the packages that wrap C code.
For example:
In [17]:
using FITSIO
In [18]:
# look at the source for fits_open_file
methods(fits_open_file)
Out[18]: