function mymult(x, y) x * y end mysquare(x) = mymult(x, x) @show mysquare(2) # Use integer arithmetic @show mymult(-1, 3. + 2im) # Use complex arithmetic @show mysquare(" abc "); # Use string concatenation mymult """The fold function, applying f from the left and accumulating the results""" function myfold(f, x, y, z) f(f(x, y), z) end myfold(mymult, "Hello", " Julia ", "World") myfold(f, x) = x myfold(f, x, y) = f(x, y) @show myfold(mymult, 2., 3.) @show myfold(+, 1) @show myfold(==, false, false, true) myfold(f, x, rest...) = myfold(f, f(x, myfold(rest...))) euler(A, Δt, xn, vn) = xn + vn * Δt, vn + A(xn) * Δt A(x) = -x euler(A, 0.1, 0, 1) Int32 <: Integer # Read Int32 is-a Integer UInt16 <: Integer Float32 <: Integer Float32 <: Number Integer <: Number # by transitivity: @show Int32 <: Number @show UInt16 <: Number @show Number <: Number; a = 4 println(typeof(a)) a = "bla" println(typeof(a)) a = 4 println(typeof(a)) a = "bla" println(typeof(a)) @code_typed euler(x -> -2x, 0.1, 0.0, 1.0) @code_typed euler(x -> -2x, 0.1f0, 0.0f0, 1.0f0) # Why not use single precision? @code_typed myfold(mymult, "Hello", " Julia ", " World!") mymult(x, y) = x * y mymult(2, " abc") "abc"^4 mymult(str::AbstractString, n::Integer) = str^n mymult(n::Integer, str::AbstractString) = mymult(str, n) @show mymult(2, " abc") @show mymult("def ", UInt16(3)); equal_type(x::T, y::T) where {T} = true equal_type(x, y) = false equal_type(1, 1.2) equal_type("abc", "ef") equal_type_and_integer(x::T, y::T) where {T <: Integer} = true equal_type_and_integer(x, y) where {T <: Integer} = false @show equal_type_and_integer(1, 2) @show equal_type_and_integer("abc", "ef"); characterise(x::String, y::Nothing) = "" characterise(x::Nothing, y::Complex) = "Only one of us is complex" characterise(x::Number, ::Nothing) = "I'm a number, Jack, and I'm ok" characterise(x::AbstractFloat, y::Integer) = "Pretty abstract" characterise(x::BigFloat, y) = "I'm big" characterise(x, y::Union{BigFloat, BigInt}) = "We're big" characterise(x::Float32, y::Union{Int32, Int64}) = "pretty wide" # For example: characterise("abc", 1.2) import Base: * *(str::AbstractString, n::Integer) = str^n *(n::Integer, str::AbstractString) = mymult(str, n) @show 4 * "abc" *(4, "abc")