Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jakehemmerle/a5322a501be025e76e2f7886021ff167 to your computer and use it in GitHub Desktop.
Save jakehemmerle/a5322a501be025e76e2f7886021ff167 to your computer and use it in GitHub Desktop.
qwq

Here's a Julia program that demonstrates several key features of the language:

# Import plotting package
using Plots

# 1. Multiple Dispatch and Type System
struct Circle
    radius::Float64
end

struct Rectangle
    width::Float64
    height::Float64
end

# Method dispatch based on type
function area(shape::Circle)
    return π * shape.radius^2
end

function area(shape::Rectangle)
    return shape.width * shape.height
end

# 2. Metaprogramming
macro timed_block(name)
    quote
        @info "Starting $(esc(name))"
        @time $(esc(name))()
        @info "Finished $(esc(name))"
    end
end

# 3. High-Performance Numerical Computing
function fast_fib(n::Int)
    a, b = (1, 1)
    for i in 2:n
        a, b = b, a + b
    end
    return a
end

# 4. Interactivity and Plotting
function plot_sine_wave()
    x = range(0, stop=2π, length=100)
    y = sin.(x)
    
    plot(x, y, 
         label="sin(x)", 
         title="Sine Wave Example",
         xlabel="x", 
         ylabel="sin(x)",
         legend=:bottomright,
         dpi=100)
    savefig("sine_plot.png")
end

# 5. Parametric Types (Generic Programming)
struct Vector2D{T<:Real}
    x::T
    y::T
end

function Base.show(io::IO, v::Vector2D)
    print(io, "Vector2D($(v.x), $(v.y))")
end

# Demonstration of features
println("Demonstrating Julia Features:")

# Multiple Dispatch Example
circle = Circle(5.0)
rectangle = Rectangle(4.0, 6.0)
println("Circle area: $(area(circle))")
println("Rectangle area: $(area(rectangle))")

# Combined dispatch example
combined_area = area(circle) + area(rectangle)
println("Combined area: $combined_area")

# High-performance computation
@timed_block fast_fib do
    fib_result = fast_fib(1000)
    println("fib(1000) = $fib_result (calculated quickly)")
end

# Plotting example
@timed_block plot_sine_wave

# Parametric Type Example
v1 = Vector2D{Float64}(3.5, 4.2)
v2 = Vector2D{Int}(2, 3)
println("v1: $v1, v2: $v2")

# Metaprogramming Example
macro square(ex)
    return :( $(esc(ex)) ^ 2 )
end

result = @square(5 + 3)
println("Square of (5+3): $result")

# Multiple Dispatch with Method Specialization
function compute(a::Number)
    return a^2
end

function compute(a::AbstractString)
    return uppercase(a)
end

println("compute(5) = $(compute(5))")
println("compute(\"hello\") = $(compute(\"hello\"))")

This program demonstrates the following Julia features:

  1. Multiple Dispatch:

    • Different area methods for Circle and Rectangle types
    • Method specialization in compute function
  2. Type System:

    • Custom structs with type parameters (Vector2D{T})
    • Parametric types and type constraints (T<:Real)
  3. High-Performance Computing:

    • Efficient numerical computation in fast_fib
    • Just-in-time (JIT) compilation shown through timing
  4. Metaprogramming:

    • Custom macro @timed_block for code instrumentation
    • @square macro for expression transformation
  5. Interactivity:

    • Plotting with Plots.jl
    • Immediate feedback through REPL-like output
  6. Interoperability:

    • Works with external packages (Plots.jl)
    • Can easily interface with C/Fortran code (not shown here)
  7. Ease of Use:

    • Clean syntax
    • Built-in plotting
    • Interactive development environment
  8. Vectorization:

    • Automatic vectorization in sin.(x) syntax

To run this program:

  1. Install Julia from https://julialang.org
  2. Create a new .jl file with this code
  3. Install required packages with ] add Plots
  4. Run with julia yourfile.jl

The program will:

  • Print various computed results
  • Generate a sine wave plot saved as "sine_plot.png"
  • Show timing information for performance-critical sections

This example shows how Julia combines the ease of a dynamic language with the performance of a compiled language, while providing powerful metaprogramming capabilities and excellent support for numerical computing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment