๐ Julia Introduction
๐ฏ Complete Definition
Julia is a high-level, high-performance, dynamic programming language for technical computing, with syntax that is familiar to users of other technical computing environments. It provides a sophisticated compiler, distributed parallel execution, numerical accuracy, and an extensive mathematical function library. Julia uses multiple dispatch as a paradigm, making it easy to express many object-oriented and functional programming patterns. The standard library provides I/O, networking, threading, graphics, and much more.
๐ฌ Core Characteristics
- Fast: JIT-compiled (LLVM) โ approaches C speed
- Dynamic: with optional typing and type inference
- Multiple dispatch: functions defined for combinations of argument types
- Designed for parallelism: built-in primitives for parallel and distributed computing
- Rich type system: parametric types, unions, abstract types
- Lisp-like macros: metaprogramming and generated functions
- Interoperability: call C, Fortran, Python directly
- Open source: MIT licensed
๐ Industry Usage
Julia is widely used in scientific computing, machine learning (Flux.jl, Turing.jl), data science (DataFrames.jl), optimization, and quantitative finance. It's adopted by institutions like MIT, BlackRock, AstraZeneca, and NASA. Julia ranks among the top 30 languages and is growing rapidly in the technical domain.
๐ Basics & REPL
๐ฏ Complete Definition
Julia has a simple, MATLAB-like syntax. Variables are assigned with `=`. Comments use `#`. The REPL (Read-Eval-Print-Loop) is powerful, with special modes for shell, help, and package management. Semicolons suppress output; multiple statements can be separated by `;`.
๐ท๏ธ Basic Syntax
- Assignment: x = 10; y = 20
- Constants: const PI = 3.14159
- Special characters: Unicode allowed (ฯ, ฯต)
- REPL modes: ] (pkg), ? (help), ; (shell)
- Printing: print(), println(), @printf
๐ Type System
๐ฏ Complete Definition
Julia's type system is dynamic but with optional type annotations. Types are organized in a hierarchy. Concrete types (no subtypes) can be instantiated; abstract types (e.g., Number, AbstractString) serve as category labels. Parametric types (e.g., Vector{Int}) are central. Union types allow multiple types.
๐ท๏ธ Primary Built-in Types
- Integer: Int8, UInt8, Int16, ..., Int128, BigInt
- Floating point: Float16, Float32, Float64, BigFloat
- Bool: true, false
- Char: 32-bit Unicode character
- String: immutable sequence of Char
- Tuple: immutable, heterogeneous
- Array: mutable, homogeneous (Vector, Matrix)
- Dict, Set: collections
- Nothing: type with only value nothing
๐งฎ Math & Operators
๐ฏ Complete Definition
Julia provides a full set of mathematical operators and functions. It supports vectorized operations (dot syntax) and has a rich set of numeric functions. Operators can be overloaded.
๐ Operator Categories
- Arithmetic: +, -, *, /, ^, รท (integer division), % (remainder), // (rational)
- Comparison: ==, !=, <, >, โค, โฅ, === (identical)
- Logical: &&, ||, ! (short-circuit)
- Bitwise: &, |, โป (xor), ~, <<, >>
- Dot syntax for broadcasting: .+, .*, .^ etc.
๐ Control Flow
๐ฏ Complete Definition
Julia provides familiar control structures: conditional evaluation, short-circuit evaluation, and loops. Ternary operator, if-elseif-else, and also @goto but discouraged.
๐๏ธ Structures
- if cond ... elseif ... else ... end
- cond ? value_if_true : value_if_false ternary
- && and || short-circuit conditional
- No switch, but can use
@evalor dictionaries
โ๏ธ Functions
๐ฏ Complete Definition
Functions are first-class objects. They can be defined in multiple ways (short form, long form, anonymous). Functions can have multiple methods (multiple dispatch). Keyword arguments and default values are supported.
๐๏ธ Function Features
- function name(args) ... end
- name(args) = expression one-liner
- anon = x -> x^2 anonymous
- do ... end for anonymous function blocks
- return can be implicit (last expression)
๐ Arrays & Matrices
๐ฏ Complete Definition
Arrays are mutable, N-dimensional, homogeneous collections. Indexing starts at 1. Julia's array syntax is powerful for numerical computing. Comprehensions, broadcasting, and linear algebra are built-in.
๐๏ธ Array Operations
- Array{Type}(undef, dims) uninitialized
- [1,2,3] column vector, [1 2 3] 1x3 matrix
- zeros, ones, rand, range
- a[end], a[2:4], a[2,3] indexing
- push!, pop!, append!, splice! modification
- Linear algebra:
I, inv, eigvals, +, *, ' (adjoint)
๐ Strings
๐ฏ Complete Definition
Strings are immutable sequences of Char (Unicode). Interpolation with $ or $(expr). Triple-quoted strings preserve indentation. Many functions for searching, replacing, and regex.
๐๏ธ String Functions
- length(s) number of characters
- s[2:4] substring (by index)
- string(x...) concatenation
- * concatenation operator
- occursin, findfirst, replace
- Regex: r"pattern"
๐ท Composite Types (struct)
๐ฏ Complete Definition
struct defines a composite type (like a class without methods). They are immutable by default; use mutable struct for mutable fields. Fields can have types. Constructors are automatically generated.
๐๏ธ Features
- struct Point immutable; fields accessible via
p.x - mutable struct allows field changes
- Parametric types:
struct Point{T} - Inner constructors for extra validation
- No inheritance, but composition and multiple dispatch
๐ Multiple Dispatch
๐ฏ Complete Definition
Multiple dispatch chooses which method of a function to call based on the types of all arguments. This is Julia's core paradigm. Functions can have many methods defined for different combinations of argument types.
๐๏ธ Features
- function f(x::Int, y::Float64) specific method
- f(x, y) fallback (Any, Any)
- Method ambiguity resolution
- Parametric methods for flexible typing
๐พ I/O & Files
๐ฏ Complete Definition
Julia's I/O system is based on streams. Files are opened with open and support reading/writing. The write and read functions work with bytes or text. do block syntax ensures automatic closing.
๐๏ธ File Operations
- open(filename, "w") do f ... end write
- read(filename, String) read entire
- eachline(filename) iterate lines
- write(f, data) write bytes or string
- @printf formatted printing
๐ฆ Modules & Packages
๐ฏ Complete Definition
Modules are separate namespaces. Packages are modules with versioning, installed via Pkg. Julia's package manager is built-in. using X brings names into scope; import X requires prefixing.
๐๏ธ Module Features
- module Name ... end define
- export marks names as public
- using and import
- Pkg.add("Package") install
- Standard library: Base, Core, Random, Statistics, LinearAlgebra
๐ก๏ธ Error Handling
๐ฏ Complete Definition
Julia uses exceptions for error handling. try/catch handles exceptions; error() throws an ErrorException. throw can throw any exception type. finally ensures cleanup.
๐๏ธ Handling
- try ... catch ... end
- catch e captures exception object
- finally always executed
- error(msg) throws ErrorException
- @assert cond msg raises AssertionError
โก Advanced (Metaprogramming, Parallel)
๐ฏ Complete Definition
Julia supports Lisp-like metaprogramming: code is represented as expressions (Expr) that can be manipulated and evaluated. Macros transform code. Parallel computing is built-in via @threads, @distributed, and @async.
๐๏ธ Advanced Features
- : (colon) quote expression
- quote ... end literal expression
- eval, Meta.parse
- macro name(args) ... end
- @time, @show, @which built-in macros
- Threads.@threads for loop parallelism
- Distributed.@distributed for parallel loops