{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "8431e4b9-cba8-4d63-ada0-5fefc48ebc26", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "half (generic function with 1 method)" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "half(x::Int)::Int = x/2" ] }, { "cell_type": "code", "execution_count": 2, "id": "8ead86e3-3ecf-464f-a39b-45c6835a0fec", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "half(2)" ] }, { "cell_type": "code", "execution_count": 3, "id": "bb8c36a3-af33-41dc-ba18-0d434da96d57", "metadata": {}, "outputs": [ { "ename": "LoadError", "evalue": "InexactError: Int64(1.5)", "output_type": "error", "traceback": [ "InexactError: Int64(1.5)", "", "Stacktrace:", " [1] Int64", " @ .\\float.jl:812 [inlined]", " [2] convert", " @ .\\number.jl:7 [inlined]", " [3] half(x::Int64)", " @ Main .\\In[1]:1", " [4] top-level scope", " @ In[3]:1", " [5] eval", " @ .\\boot.jl:373 [inlined]", " [6] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)", " @ Base .\\loading.jl:1196" ] } ], "source": [ "half(3)" ] }, { "cell_type": "code", "execution_count": 4, "id": "d2186a6f-e109-4667-8962-6218a4787643", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "CodeInfo(\n", "\u001b[90m1 ─\u001b[39m %1 = Main.Int\n", "\u001b[90m│ \u001b[39m %2 = x / 2\n", "\u001b[90m│ \u001b[39m %3 = Base.convert(%1, %2)\n", "\u001b[90m│ \u001b[39m %4 = Core.typeassert(%3, %1)\n", "\u001b[90m└──\u001b[39m return %4\n", ")" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "@code_lowered half(2)" ] }, { "cell_type": "code", "execution_count": 5, "id": "d4c31280-570f-40d7-9570-a5180302c5b3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "f (generic function with 1 method)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function f(n::Integer)::Vector{Real}\n", " randn(n)\n", "end" ] }, { "cell_type": "code", "execution_count": 6, "id": "2419a220-7089-4aaf-92c1-66d298b992c1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 0.352352 seconds (2 allocations: 762.939 MiB, 1.84% gc time)\n" ] }, { "data": { "text/plain": [ "-14842.156808955893" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "@time sum(randn(10^8))" ] }, { "cell_type": "code", "execution_count": 7, "id": "3d290022-08d4-40f5-a9d6-7d9e799ebcb8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 9.420157 seconds (200.00 M allocations: 4.470 GiB, 58.79% gc time, 0.03% compilation time)\n" ] }, { "data": { "text/plain": [ "4626.280496458812" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "@time sum(f(10^8))" ] }, { "cell_type": "code", "execution_count": 8, "id": "1505636d-3066-4d12-b75a-5e16049ad90f", "metadata": {}, "outputs": [], "source": [ "abstract type AbstractFoo{T} end\n", "\n", "struct Foo{T} <: AbstractFoo{T} a::T end\n", "double(x::Foo) = Foo(2x.a)\n", "\n", "struct Bar{T} <: AbstractFoo{T} a::T end" ] }, { "cell_type": "code", "execution_count": 9, "id": "0ca151bf-2108-4d8f-adf2-5dfc7055f0b7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Foo{Float64}(2.46)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "double(Foo(1.23))" ] }, { "cell_type": "code", "execution_count": 10, "id": "756319b4-0e1a-4736-b3c0-7ec5b51a2254", "metadata": {}, "outputs": [ { "ename": "LoadError", "evalue": "MethodError: no method matching double(::Bar{Float64})\n\u001b[0mClosest candidates are:\n\u001b[0m double(\u001b[91m::Foo\u001b[39m) at In[8]:4", "output_type": "error", "traceback": [ "MethodError: no method matching double(::Bar{Float64})\n\u001b[0mClosest candidates are:\n\u001b[0m double(\u001b[91m::Foo\u001b[39m) at In[8]:4", "", "Stacktrace:", " [1] top-level scope", " @ In[10]:1", " [2] eval", " @ .\\boot.jl:373 [inlined]", " [3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)", " @ Base .\\loading.jl:1196" ] } ], "source": [ "double(Bar(1.23))" ] }, { "cell_type": "code", "execution_count": 11, "id": "c45f6fa6-5757-4f08-87c0-127c9b4baf13", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "double (generic function with 2 methods)" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "double(x::AbstractFoo) = throw(MethodError(double, (x,)))" ] }, { "cell_type": "code", "execution_count": 12, "id": "2606fa41-1c56-486c-9524-cad69b5b95ca", "metadata": {}, "outputs": [ { "ename": "LoadError", "evalue": "MethodError: no method matching double(::Bar{Float64})\n\u001b[0mClosest candidates are:\n\u001b[0m double(::AbstractFoo) at In[11]:1\n\u001b[0m double(\u001b[91m::Foo\u001b[39m) at In[8]:4", "output_type": "error", "traceback": [ "MethodError: no method matching double(::Bar{Float64})\n\u001b[0mClosest candidates are:\n\u001b[0m double(::AbstractFoo) at In[11]:1\n\u001b[0m double(\u001b[91m::Foo\u001b[39m) at In[8]:4", "", "Stacktrace:", " [1] double(x::Bar{Float64})", " @ Main .\\In[11]:1", " [2] top-level scope", " @ In[12]:1", " [3] eval", " @ .\\boot.jl:373 [inlined]", " [4] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)", " @ Base .\\loading.jl:1196" ] } ], "source": [ "double(Bar(1.23))" ] }, { "cell_type": "code", "execution_count": 13, "id": "0602f90e-be25-495c-bb78-f760d7f233a8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "multiply (generic function with 1 method)" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "multiply(x::Foo, y::Number) = Foo(x.a * y) " ] }, { "cell_type": "code", "execution_count": 14, "id": "89b60f4a-2b17-49dd-bbb4-8c1c4440d396", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Foo{Float64}(30.0)" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "multiply(Foo(3), 10.0)" ] }, { "cell_type": "code", "execution_count": 15, "id": "d09742d6-8ec9-4799-97db-e71468039033", "metadata": {}, "outputs": [ { "ename": "LoadError", "evalue": "MethodError: no method matching multiply(::Bar{Int64}, ::Float64)\n\u001b[0mClosest candidates are:\n\u001b[0m multiply(\u001b[91m::Foo\u001b[39m, ::Number) at In[13]:1", "output_type": "error", "traceback": [ "MethodError: no method matching multiply(::Bar{Int64}, ::Float64)\n\u001b[0mClosest candidates are:\n\u001b[0m multiply(\u001b[91m::Foo\u001b[39m, ::Number) at In[13]:1", "", "Stacktrace:", " [1] top-level scope", " @ In[15]:1", " [2] eval", " @ .\\boot.jl:373 [inlined]", " [3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)", " @ Base .\\loading.jl:1196" ] } ], "source": [ "multiply(Bar(3), 10.0)" ] }, { "cell_type": "code", "execution_count": 16, "id": "ace4dbd6-1dc3-4195-a399-73de62cc3158", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "multiply (generic function with 2 methods)" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "multiply(x::AbstractFoo, y) = error(\"`myltiply` has not been implemented for $(typeof(x))\")" ] }, { "cell_type": "code", "execution_count": 17, "id": "98c79eec-bc23-45d1-942d-6c8e1eb77801", "metadata": {}, "outputs": [ { "ename": "LoadError", "evalue": "`myltiply` has not been implemented for Bar{Int64}", "output_type": "error", "traceback": [ "`myltiply` has not been implemented for Bar{Int64}", "", "Stacktrace:", " [1] error(s::String)", " @ Base .\\error.jl:33", " [2] multiply(x::Bar{Int64}, y::Float64)", " @ Main .\\In[16]:1", " [3] top-level scope", " @ In[17]:1", " [4] eval", " @ .\\boot.jl:373 [inlined]", " [5] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)", " @ Base .\\loading.jl:1196" ] } ], "source": [ "multiply(Bar(3), 10.0)" ] }, { "cell_type": "code", "execution_count": 18, "id": "6b4e50dd-7d93-4ae6-b8b9-9c1134c5a1b1", "metadata": {}, "outputs": [ { "ename": "LoadError", "evalue": "`myltiply` has not been implemented for Foo{Int64}", "output_type": "error", "traceback": [ "`myltiply` has not been implemented for Foo{Int64}", "", "Stacktrace:", " [1] error(s::String)", " @ Base .\\error.jl:33", " [2] multiply(x::Foo{Int64}, y::Vector{Float64})", " @ Main .\\In[16]:1", " [3] top-level scope", " @ In[18]:1", " [4] eval", " @ .\\boot.jl:373 [inlined]", " [5] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)", " @ Base .\\loading.jl:1196" ] } ], "source": [ "multiply(Foo(3), [10.0])" ] }, { "cell_type": "code", "execution_count": 19, "id": "85c73214-69b5-48fe-9739-e1df13857e6f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "multiply (generic function with 2 methods)" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "multiply(x::AbstractFoo, y) = throw(MethodError(multiply, (x, y)))" ] }, { "cell_type": "code", "execution_count": 20, "id": "89a5e8af-47e3-4661-b5db-762d27746e0b", "metadata": {}, "outputs": [ { "ename": "LoadError", "evalue": "MethodError: no method matching multiply(::Foo{Int64}, ::Vector{Float64})\n\u001b[0mClosest candidates are:\n\u001b[0m multiply(::AbstractFoo, ::Any) at In[19]:1\n\u001b[0m multiply(::Foo, \u001b[91m::Number\u001b[39m) at In[13]:1", "output_type": "error", "traceback": [ "MethodError: no method matching multiply(::Foo{Int64}, ::Vector{Float64})\n\u001b[0mClosest candidates are:\n\u001b[0m multiply(::AbstractFoo, ::Any) at In[19]:1\n\u001b[0m multiply(::Foo, \u001b[91m::Number\u001b[39m) at In[13]:1", "", "Stacktrace:", " [1] multiply(x::Foo{Int64}, y::Vector{Float64})", " @ Main .\\In[19]:1", " [2] top-level scope", " @ In[20]:1", " [3] eval", " @ .\\boot.jl:373 [inlined]", " [4] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)", " @ Base .\\loading.jl:1196" ] } ], "source": [ "multiply(Foo(3), [10.0])" ] }, { "cell_type": "code", "execution_count": 21, "id": "656360cb-0bac-43d6-b9b0-687924635c0d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "double! (generic function with 1 method)" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function double!(a::Array)\n", " @. a = 2a\n", "end" ] }, { "cell_type": "code", "execution_count": 22, "id": "4c172625-c1a1-4129-a276-b9f847a86475", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "double!(v) = [2, 4, 6, 8]\n", "v = [2, 4, 6, 8]\n" ] } ], "source": [ "v = [1, 2, 3, 4]\n", "@show double!(v)\n", "@show v;" ] }, { "cell_type": "code", "execution_count": 23, "id": "09ad41bf-679a-4af3-910e-a3cd13c92ec6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2-element view(::Vector{Int64}, 2:3) with eltype Int64:\n", " 4\n", " 6" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "w = view(v, 2:3)" ] }, { "cell_type": "code", "execution_count": 24, "id": "282d110e-6342-4933-8ad6-52e97b43a48a", "metadata": {}, "outputs": [ { "ename": "LoadError", "evalue": "MethodError: no method matching double!(::SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true})\n\u001b[0mClosest candidates are:\n\u001b[0m double!(\u001b[91m::Array\u001b[39m) at In[21]:1", "output_type": "error", "traceback": [ "MethodError: no method matching double!(::SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true})\n\u001b[0mClosest candidates are:\n\u001b[0m double!(\u001b[91m::Array\u001b[39m) at In[21]:1", "", "Stacktrace:", " [1] top-level scope", " @ In[24]:1", " [2] eval", " @ .\\boot.jl:373 [inlined]", " [3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)", " @ Base .\\loading.jl:1196" ] } ], "source": [ "double!(w)" ] }, { "cell_type": "code", "execution_count": 25, "id": "32e6d977-7fff-4988-ae4d-5383cc6acb7c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "double! (generic function with 2 methods)" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function double!(v)\n", " @. v = 2v\n", "end" ] }, { "cell_type": "code", "execution_count": 26, "id": "979f8aaf-40a6-4660-844a-d82b788867b8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "double!(w) = [8, 12]\n", "w = [8, 12]\n", "v = [2, 8, 12, 8]\n" ] } ], "source": [ "@show double!(w)\n", "@show w\n", "@show v;" ] }, { "cell_type": "code", "execution_count": null, "id": "b74e19e7-9a48-4739-b436-84ce6718f28d", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "jupytext": { "formats": "ipynb,jl:hydrogen" }, "kernelspec": { "display_name": "Julia 1.7.3", "language": "julia", "name": "julia-1.7" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.7.3" } }, "nbformat": 4, "nbformat_minor": 5 }