{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Defining new types in Julia" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "mutable struct MyDiscreteWalker # capital letters for each word in type name -- camel case\n", " x::Int64\n", "end" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "mutable struct SomethingElse \n", " x::Int64 # several fields\n", " y::Float64\n", " name::String\n", "end" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "MyDiscreteWalker" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "MyDiscreteWalker # refers to the *type*" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "# 2 methods for type constructor:" ], "text/plain": [ "# 2 methods for type constructor:\n", "[1] MyDiscreteWalker(x::Int64) in Main at In[1]:2\n", "[2] MyDiscreteWalker(x) in Main at In[1]:2" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "methods(MyDiscreteWalker) # which methods / versions of a function exist" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Julia automatically created 2 **constructors** -- functions to create objects of that type" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "MyDiscreteWalker(3)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "w = MyDiscreteWalker(3)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "MyDiscreteWalker" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "typeof(w)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3 + 4im" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Complex(3, 4)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3 + 4im" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 + 4im # im is i (sqrt of -1)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "(::Type{Complex})(re::T, im::T) where T<:Real in Base at complex.jl:12" ], "text/plain": [ "(::Type{Complex})(re::T, im::T) where T<:Real in Base at complex.jl:12" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "@which Complex(3, 4)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "MyDiscreteWalker(3)" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "w" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "w isa MyDiscreteWalker # w is an **instance** of the type MyDiscreteWalker" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "# 2 methods for type constructor:" ], "text/plain": [ "# 2 methods for type constructor:\n", "[1] MyDiscreteWalker(x::Int64) in Main at In[1]:2\n", "[2] MyDiscreteWalker(x) in Main at In[1]:2" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "methods(MyDiscreteWalker)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`MyDiscreteWalker(x::Int64)` is a method that works *only* when it receives an argument `x` of type `Int64`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`MyDiscreteWalker(x)` will accept an `x` of any type" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Any" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Any" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "false" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3.0 isa Int64" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Float64" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "typeof(3.0)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "MyDiscreteWalker(3)" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "MyDiscreteWalker(3.0)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "ename": "InexactError", "evalue": "InexactError: Int64(3.1)", "output_type": "error", "traceback": [ "InexactError: Int64(3.1)", "", "Stacktrace:", " [1] Int64 at ./float.jl:710 [inlined]", " [2] convert at ./number.jl:7 [inlined]", " [3] MyDiscreteWalker(::Float64) at ./In[1]:2", " [4] top-level scope at In[21]:1" ] } ], "source": [ "MyDiscreteWalker(3.1)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Int64(3.0)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "ename": "InexactError", "evalue": "InexactError: Int64(3.1)", "output_type": "error", "traceback": [ "InexactError: Int64(3.1)", "", "Stacktrace:", " [1] Int64(::Float64) at ./float.jl:710", " [2] top-level scope at In[23]:1" ] } ], "source": [ "Int64(3.1)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "trunc(Int64, 3.1)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "floor(Int64, 3.1)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.0" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "floor(3.1)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Int64(floor(3.1))" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "MyDiscreteWalker(3)" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "w = MyDiscreteWalker(3)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "MyDiscreteWalker(3)" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "w" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Problem set 3:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Collecting information into one packet that belongs together: **encapsulation**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mutable struct Agent\n", " infection_status::Int\n", " num_infected::Int\n", "end" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "MyDiscreteWalker(3)" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "w = MyDiscreteWalker(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "More common to use *immutable* structs:" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "struct ExampleImmutable\n", " x::Int\n", " y::Int\n", "end" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "z = ExampleImmutable()" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "convert(Int64, 3.0)" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "ename": "InexactError", "evalue": "InexactError: Int64(3.1)", "output_type": "error", "traceback": [ "InexactError: Int64(3.1)", "", "Stacktrace:", " [1] Int64 at ./float.jl:710 [inlined]", " [2] convert(::Type{Int64}, ::Float64) at ./number.jl:7", " [3] top-level scope at In[33]:1" ] } ], "source": [ "convert(Int64, 3.1)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Int64(3.0)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/html": [ "Int64(x::Float64) in Base at float.jl:707" ], "text/plain": [ "Int64(x::Float64) in Base at float.jl:707" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "@which Int64(3.0)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/html": [ "# 14 methods for type constructor:" ], "text/plain": [ "# 14 methods for type constructor:\n", "[1] Int64(x::Union{Bool, Int32, Int64, UInt32, UInt64, UInt8, Int128, Int16, Int8, UInt128, UInt16}) in Core at boot.jl:707\n", "[2] Int64(x::Ptr) in Core at boot.jl:717\n", "[3] Int64(x::Float32) in Base at float.jl:707\n", "[4] Int64(x::Float64) in Base at float.jl:707\n", "[5] (::Type{T})(x::Float16) where T<:Integer in Base at float.jl:71\n", "[6] (::Type{T})(z::Complex) where T<:Real in Base at complex.jl:37\n", "[7] (::Type{T})(x::Rational) where T<:Integer in Base at rational.jl:94\n", "[8] (::Type{T})(x::BigInt) where T<:Union{Int128, Int16, Int32, Int64, Int8} in Base.GMP at gmp.jl:352\n", "[9] (::Type{T})(x::BigFloat) where T<:Integer in Base.MPFR at mpfr.jl:340\n", "[10] (::Type{T})(x::T) where T<:Number in Core at boot.jl:715\n", "[11] (::Type{T})(x::Base.TwicePrecision) where T<:Number in Base at twiceprecision.jl:243\n", "[12] (::Type{T})(x::AbstractChar) where T<:Union{AbstractChar, Number} in Base at char.jl:50\n", "[13] (::Type{T})(x::Enum{T2}) where {T<:Integer, T2<:Integer} in Base.Enums at Enums.jl:19\n", "[14] (dt::Type{#s662} where #s662<:Integer)(ip::Sockets.IPAddr) in Sockets at /Applications/Julia-1.4.app/Contents/Resources/julia/share/julia/stdlib/v1.4/Sockets/src/IPAddr.jl:11" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "methods(Int64)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/html": [ "convert(::Type{T}, x::Number) where T<:Number in Base at number.jl:7" ], "text/plain": [ "convert(::Type{T}, x::Number) where T<:Number in Base at number.jl:7" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "@which convert(Int64, 3.1)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "ExampleImmutable(1, 2)" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z = ExampleImmutable(1, 2)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z.x" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z.y" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "ename": "ErrorException", "evalue": "setfield! immutable struct of type ExampleImmutable cannot be changed", "output_type": "error", "traceback": [ "setfield! immutable struct of type ExampleImmutable cannot be changed", "", "Stacktrace:", " [1] setproperty!(::ExampleImmutable, ::Symbol, ::Int64) at ./Base.jl:34", " [2] top-level scope at In[45]:1" ] } ], "source": [ "z.x = 10" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "ename": "ErrorException", "evalue": "setfield! immutable struct of type ExampleImmutable cannot be changed", "output_type": "error", "traceback": [ "setfield! immutable struct of type ExampleImmutable cannot be changed", "", "Stacktrace:", " [1] setproperty!(::ExampleImmutable, ::Symbol, ::Int64) at ./Base.jl:34", " [2] top-level scope at In[46]:1" ] } ], "source": [ "z.y = 10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Make walker move" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "MyDiscreteWalker(3)" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "w = MyDiscreteWalker(3)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "ename": "MethodError", "evalue": "MethodError: no method matching MyDiscreteWalker()\nClosest candidates are:\n MyDiscreteWalker(!Matched::Int64) at In[1]:2\n MyDiscreteWalker(!Matched::Any) at In[1]:2", "output_type": "error", "traceback": [ "MethodError: no method matching MyDiscreteWalker()\nClosest candidates are:\n MyDiscreteWalker(!Matched::Int64) at In[1]:2\n MyDiscreteWalker(!Matched::Any) at In[1]:2", "", "Stacktrace:", " [1] top-level scope at In[48]:1" ] } ], "source": [ "MyDiscreteWalker()" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/html": [ "# 2 methods for type constructor:" ], "text/plain": [ "# 2 methods for type constructor:\n", "[1] MyDiscreteWalker(x::Int64) in Main at In[1]:2\n", "[2] MyDiscreteWalker(x) in Main at In[1]:2" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "methods(MyDiscreteWalker)" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "MyDiscreteWalker" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "MyDiscreteWalker() = MyDiscreteWalker(0) # outer constructor (lives outside definition of type)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/html": [ "# 3 methods for type constructor:" ], "text/plain": [ "# 3 methods for type constructor:\n", "[1] MyDiscreteWalker() in Main at In[50]:1\n", "[2] MyDiscreteWalker(x::Int64) in Main at In[1]:2\n", "[3] MyDiscreteWalker(x) in Main at In[1]:2" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "methods(MyDiscreteWalker)" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "MyDiscreteWalker(0)" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "w = MyDiscreteWalker()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Now make it move" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "jump! (generic function with 1 method)" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function jump!(w::MyDiscreteWalker)\n", " w.x += rand( (-1, +1) )\n", "end" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "pos (generic function with 1 method)" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pos(w::MyDiscreteWalker) = w.x # getter function" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "w.x # w." ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(:x,)" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "propertynames(w)" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(:x, :y)" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "propertynames(z)" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pos(w) # interface to my object; removes me from the internal details" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "set_pos! (generic function with 1 method)" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function set_pos!(w, x) # setter function\n", " w.x = x\n", "end" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "jump (generic function with 1 method)" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "jump(MyDiscreteWalker) = rand( (-1, +1) )" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "function jump!(w::MyDiscreteWalker)\n", " old_pos = pos(w)\n", " set_pos!(old_pos + jump(w))\n", "end" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this version of `jump!`, I never refer to internal details of the object." ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "walk! (generic function with 1 method)" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function walk!(w::MyDiscreteWalker, N)\n", " for i in 1:N\n", " jump!(w)\n", " end\n", " \n", " return w\n", "end" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "MyDiscreteWalker(0)" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "w" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "ename": "MethodError", "evalue": "MethodError: no method matching walk!(::Int64)\nClosest candidates are:\n walk!(!Matched::MyDiscreteWalker, !Matched::Any) at In[64]:2", "output_type": "error", "traceback": [ "MethodError: no method matching walk!(::Int64)\nClosest candidates are:\n walk!(!Matched::MyDiscreteWalker, !Matched::Any) at In[64]:2", "", "Stacktrace:", " [1] top-level scope at In[66]:1" ] } ], "source": [ "walk!(10)" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "walk! (generic function with 1 method)" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "walk!" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/html": [ "# 1 method for generic function walk!:" ], "text/plain": [ "# 1 method for generic function \"walk!\":\n", "[1] walk!(w::MyDiscreteWalker, N) in Main at In[64]:2" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "methods(walk!)" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "ename": "UndefVarError", "evalue": "UndefVarError: aweirdfunction not defined", "output_type": "error", "traceback": [ "UndefVarError: aweirdfunction not defined", "", "Stacktrace:", " [1] top-level scope at In[69]:1" ] } ], "source": [ "aweirdfunction(10)" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "MyDiscreteWalker(4)" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "walk!(w, 10)" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "MyDiscreteWalker(4)" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "w # has been mutated / modified" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Continuous walkers" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [], "source": [ "mutable struct MyContinuousWalker\n", " y::Float64\n", "end " ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "MyContinuousWalker(3.0)" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "w = MyContinuousWalker(3)" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "w isa MyContinuousWalker" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "ename": "MethodError", "evalue": "MethodError: no method matching jump!(::MyContinuousWalker)\nClosest candidates are:\n jump!(!Matched::MyDiscreteWalker) at In[53]:2", "output_type": "error", "traceback": [ "MethodError: no method matching jump!(::MyContinuousWalker)\nClosest candidates are:\n jump!(!Matched::MyDiscreteWalker) at In[53]:2", "", "Stacktrace:", " [1] top-level scope at In[75]:1" ] } ], "source": [ "jump!(w)" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "data": { "text/html": [ "# 1 method for generic function jump!:" ], "text/plain": [ "# 1 method for generic function \"jump!\":\n", "[1] jump!(w::MyDiscreteWalker) in Main at In[53]:2" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "methods(jump!)" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "jump (generic function with 2 methods)" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "jump(w::MyContinuousWalker) = randn()" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "ename": "MethodError", "evalue": "MethodError: no method matching jump!(::MyContinuousWalker)\nClosest candidates are:\n jump!(!Matched::MyDiscreteWalker) at In[53]:2", "output_type": "error", "traceback": [ "MethodError: no method matching jump!(::MyContinuousWalker)\nClosest candidates are:\n jump!(!Matched::MyDiscreteWalker) at In[53]:2", "", "Stacktrace:", " [1] top-level scope at In[78]:1" ] } ], "source": [ "jump!(w::MyContinuousWalker)" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/html": [ "# 1 method for generic function jump!:" ], "text/plain": [ "# 1 method for generic function \"jump!\":\n", "[1] jump!(w::MyDiscreteWalker) in Main at In[53]:2" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "methods(jump!)" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "jump! (generic function with 2 methods)" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function jump!(w) # takes argument w of *any* type\n", " old_pos = pos(w)\n", " set_pos!(w, old_pos + jump(w))\n", "end" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "ename": "MethodError", "evalue": "MethodError: no method matching pos(::Int64)\nClosest candidates are:\n pos(!Matched::MyDiscreteWalker) at In[54]:1", "output_type": "error", "traceback": [ "MethodError: no method matching pos(::Int64)\nClosest candidates are:\n pos(!Matched::MyDiscreteWalker) at In[54]:1", "", "Stacktrace:", " [1] jump!(::Int64) at ./In[80]:2", " [2] top-level scope at In[81]:1" ] } ], "source": [ "jump!(1)" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "MyContinuousWalker(3.0)" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "w" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "ename": "MethodError", "evalue": "MethodError: no method matching pos(::MyContinuousWalker)\nClosest candidates are:\n pos(!Matched::MyDiscreteWalker) at In[54]:1", "output_type": "error", "traceback": [ "MethodError: no method matching pos(::MyContinuousWalker)\nClosest candidates are:\n pos(!Matched::MyDiscreteWalker) at In[54]:1", "", "Stacktrace:", " [1] jump!(::MyContinuousWalker) at ./In[82]:2", " [2] top-level scope at In[84]:1" ] } ], "source": [ "jump!(w)" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "data": { "text/html": [ "# 1 method for generic function pos:" ], "text/plain": [ "# 1 method for generic function \"pos\":\n", "[1] pos(w::MyDiscreteWalker) in Main at In[54]:1" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "methods(pos)" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "pos (generic function with 2 methods)" ] }, "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pos(w::MyContinuousWalker) = w.y" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "set_pos! (generic function with 2 methods)" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function set_pos!(w::MyContinuousWalker, pos)\n", " w.y = pos\n", "end" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.1822730537401607" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "jump!(w)" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "ename": "MethodError", "evalue": "MethodError: no method matching walk!(::MyContinuousWalker, ::Int64)\nClosest candidates are:\n walk!(!Matched::MyDiscreteWalker, ::Any) at In[64]:2", "output_type": "error", "traceback": [ "MethodError: no method matching walk!(::MyContinuousWalker, ::Int64)\nClosest candidates are:\n walk!(!Matched::MyDiscreteWalker, ::Any) at In[64]:2", "", "Stacktrace:", " [1] top-level scope at In[91]:1" ] } ], "source": [ "walk!(w, 10)" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [ { "data": { "text/html": [ "# 1 method for generic function walk!:" ], "text/plain": [ "# 1 method for generic function \"walk!\":\n", "[1] walk!(w::MyDiscreteWalker, N) in Main at In[64]:2" ] }, "execution_count": 92, "metadata": {}, "output_type": "execute_result" } ], "source": [ "methods(walk!)" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "walk! (generic function with 2 methods)" ] }, "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function walk!(w, N)\n", " for i in 1:N\n", " jump!(w)\n", " end\n", " \n", " return w\n", "end" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [ { "ename": "MethodError", "evalue": "MethodError: no method matching pos(::Int64)\nClosest candidates are:\n pos(!Matched::MyContinuousWalker) at In[86]:1\n pos(!Matched::MyDiscreteWalker) at In[54]:1", "output_type": "error", "traceback": [ "MethodError: no method matching pos(::Int64)\nClosest candidates are:\n pos(!Matched::MyContinuousWalker) at In[86]:1\n pos(!Matched::MyDiscreteWalker) at In[54]:1", "", "Stacktrace:", " [1] jump!(::Int64) at ./In[89]:2", " [2] walk!(::Int64, ::Int64) at ./In[93]:3", " [3] top-level scope at In[94]:1" ] } ], "source": [ "walk!(1, 2)" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "MyContinuousWalker(-3.3119507350317736)" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "walk!(w, 10)" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "MyContinuousWalker(-3.3119507350317736)" ] }, "execution_count": 96, "metadata": {}, "output_type": "execute_result" } ], "source": [ "w" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Abstract types" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\"A discrete walker is a **kind of** walker\"" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [], "source": [ "abstract type RandomWalker end" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "RandomWalker" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "RandomWalker" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [ { "data": { "text/html": [ "# 0 methods for type constructor:" ], "text/plain": [ "# 0 methods for type constructor:" ] }, "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ "methods(RandomWalker)" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [], "source": [ "mutable struct DiscreteWalker <: RandomWalker\n", " x::Int64\n", "end" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [], "source": [ "mutable struct ContinuousWalker <: RandomWalker\n", " y::Int64\n", "end" ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "pos (generic function with 4 methods)" ] }, "execution_count": 103, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pos(w::DiscreteWalker) = w.x\n", "pos(w::ContinuousWalker) = w.y" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "walk! (generic function with 3 methods)" ] }, "execution_count": 104, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function walk!(w::RandomWalker, N)\n", " for i in 1:N\n", " jump!(w)\n", " end\n", " \n", " return w\n", "end" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DiscreteWalker(10)" ] }, "execution_count": 105, "metadata": {}, "output_type": "execute_result" } ], "source": [ "w = DiscreteWalker(10)" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 106, "metadata": {}, "output_type": "execute_result" } ], "source": [ "w isa DiscreteWalker" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 107, "metadata": {}, "output_type": "execute_result" } ], "source": [ "w isa RandomWalker" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2-element Array{DiscreteWalker,1}:\n", " DiscreteWalker(1)\n", " DiscreteWalker(2)" ] }, "execution_count": 108, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[DiscreteWalker(1), DiscreteWalker(2)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Julia 1.4.0", "language": "julia", "name": "julia-1.4" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.4.0" } }, "nbformat": 4, "nbformat_minor": 4 }