function collatz(n) # unproven: always terminates k = 0 while n > 1 n = isodd(n) ? 3n+1 : n>>1 k += 1 end return k end collatz(89) for i = 2:2:20 α = collatz(i) println("$i = $α") end @time for i = 1:1e6 collatz(18) end @code_native collatz(123) @code_llvm collatz(123) f(a::Any, b) = "fallback" f(a::Number, b::Number) = "a and b are both numbers" f(a::Number, b) = "a is a number" f(a, b::Number) = "b is a number" f(a::Integer, b::Integer) = "a and b are both integers" f(1.5, 2) print(typeof(1.5), ", ", typeof(2)) f(1, "bar") f(1, 2) f("foo", [1,2]) f{T<:Number}(a::T, b::T) = "a and b are both $(T)s" methods(f) f(big(1.5), big(2.5)) f("foo", "bar") #<== still doesn't apply to non-numbers immutable Interval{T<:Real} <: Number lo::T hi::T end (a::Real)..(b::Real) = Interval(a,b) Base.show(io::IO, iv::Interval) = print(io, "($(iv.lo))..($(iv.hi))") (1..2) + 3 # tries but fails to find a way to reconcile two Numbers 1..2 typeof(ans) sizeof(1..2) # two 64-bit/8-byte ints (1//2)..(2//3) a::Interval + b::Interval = (a.lo + b.lo)..(a.hi + b.hi) a::Interval - b::Interval = (a.lo - b.hi)..(a.hi - b.lo) (2..3) + (-1..1) (2..3) + (1.0..3.14159) # autoconverts @code_native (2..3) + (-1..1) methods(round) # click through... round(123.321) round(123.321, 2) round(123.321, -1) round(123.321, 1, 2) for i = 1:10 println(round(123.321, i, 2)) end Pkg.installed() using RDatasets iris = dataset("datasets", "iris") typeof(iris) by(iris, :Species, df -> DataFrame(mean_length = mean(df[:PetalLength]))) using GLM lm1 = fit(LinearModel, SepalLength ~ SepalWidth + PetalLength, iris) a ~ b + c