Fast as C, readable as Python. Built for scientific computing, data science, and numerical analysis.
println("Hello, World!")
name = "Mayank"
age = 15
println("$name is $age")
# Run: julia file.jl
# Or: julia -i file.jl (interactive)
Int64, Float64, String, Bool, Vector, Dict. Multiple dispatch selects functions by type.
age::Int64 = 15
pi::Float64 = 3.14159
name::String = "Mayank"
# Collections
nums = [1, 2, 3, 4, 5]
person = Dict("name" => "Mayank", "age" => 15)
# Type inference
x = 42 # Int64
y = 3.14 # Float64
z = "hello" # String
# Multiple dispatch
f(x::Int) = x * 2
f(x::Float64) = x * 3.0
f(x::String) = x * 2
println(f(5)) # 10
println(f(3.14)) # 9.42
println(f("hi")) # hihi
function/end. if/elseif/else. for/while. Broadcasting with dot notation.
function add(a, b)
return a + b
end
# One-liner
square(x) = x^2
# Broadcasting (vectorized)
nums = [1, 2, 3, 4, 5]
println(square.(nums)) # [1, 4, 9, 16, 25]
println(sqrt.(nums))
# Control flow
score = 85
if score >= 90
println("A")
elseif score >= 80
println("B")
else
println("C")
end
# For loop
for i in 1:5
print(i, " ")
end
println()
# List comprehension
squares = [x^2 for x in 1:10]
println(squares)
# Map/filter
evens = filter(x -> x % 2 == 0, 1:20)
println(evens)
struct Point{T}
x::T
y::T
end
# Constructors
p1 = Point(3, 4) # Point{Int64}
p2 = Point(3.0, 4.0) # Point{Float64}
# Methods by type
distance(a::Point{Int64}, b::Point{Int64}) =
sqrt(Float64((a.x - b.x)^2 + (a.y - b.y)^2))
distance(a::Point{Float64}, b::Point{Float64}) =
sqrt((a.x - b.x)^2 + (a.y - b.y)^2)
println(distance(p1, Point(0, 0)))
println(distance(p2, Point(0.0, 0.0)))
# Union types
function describe(x::Union{Int, Float64})
"Number: $x"
end
println(describe(42))
println(describe(3.14))
using LinearAlgebra
# Matrices
A = [1 2; 3 4]
B = [5 6; 7 8]
println(A * B) # matrix multiply
println(A .* B) # element-wise
println(A') # transpose
println(det(A)) # determinant
println(inv(A)) # inverse
# Solve Ax = b
A = [2 1; 1 3]
b = [5, 7]
x = A \ b
println(x) # solution
# Eigenvalues
evals = eigvals(A)
println(evals)
# SVD
U, S, V = svd(A)
println(S)
# Norms
println(norm([3, 4])) # 5.0
println(norm(A))
# Multi-threading
using Base.Threads
function parallel_sum(n)
s = Atomic{Int}(0)
@threads for i in 1:n
atomic_add!(s, i)
end
return s[]
end
println(parallel_sum(1_000_000))
# Distributed
using Distributed
addprocs(4)
@everywhere function monte_carlo_pi(n)
count = 0
for i in 1:n
x, y = rand(), rand()
if x^2 + y^2 <= 1.0
count += 1
end
end
return 4.0 * count / n
end
results = pmap(_ -> monte_carlo_pi(100_000), 1:8)
println(sum(results) / length(results))
# SIMD vectorization
function fast_sum(arr)
s = 0.0
@simd for i in eachindex(arr)
@inbounds s += arr[i]
end
return s
end
using DataFrames, CSV, Statistics, Plots
# Read CSV
df = CSV.read("data.csv", DataFrame)
println(first(df, 5))
println(describe(df))
# Manipulate
subset = df[df.age .> 18, :]
grouped = combine(groupby(df, :grade), :score => mean => :avg_score)
# Plot
plot(df.age, df.score,
seriestype=:scatter,
title="Score vs Age",
xlabel="Age",
ylabel="Score",
legend=false)
savefig("plot.png")
# Statistics
println(mean(df.score))
println(std(df.score))
corr_matrix = cor(Matrix(df[:, [:age, :score, :hours]]))
println(corr_matrix)