Last active
April 7, 2018 19:01
-
-
Save osamutake/5b6aa4f2cf74297e1bc4a6b22fd88a22 to your computer and use it in GitHub Desktop.
julia の関数について勉強
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"metadata": { | |
"toc": true | |
}, | |
"cell_type": "markdown", | |
"source": "<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#julia-の関数について勉強\" data-toc-modified-id=\"julia-の関数について勉強-1\"><span class=\"toc-item-num\">1 </span>julia の関数について勉強</a></span></li><li><span><a href=\"#試した環境\" data-toc-modified-id=\"試した環境-2\"><span class=\"toc-item-num\">2 </span>試した環境</a></span></li><li><span><a href=\"#関数の定義方法は2つ\" data-toc-modified-id=\"関数の定義方法は2つ-3\"><span class=\"toc-item-num\">3 </span>関数の定義方法は2つ</a></span></li><li><span><a href=\"#無名関数を-println\" data-toc-modified-id=\"無名関数を-println-4\"><span class=\"toc-item-num\">4 </span>無名関数を println</a></span></li><li><span><a href=\"#可変引数\" data-toc-modified-id=\"可変引数-5\"><span class=\"toc-item-num\">5 </span>可変引数</a></span></li><li><span><a href=\"#引数の展開\" data-toc-modified-id=\"引数の展開-6\"><span class=\"toc-item-num\">6 </span>引数の展開</a></span><ul class=\"toc-item\"><li><span><a href=\"#キーワード引数も展開して渡せる\" data-toc-modified-id=\"キーワード引数も展開して渡せる-6.1\"><span class=\"toc-item-num\">6.1 </span>キーワード引数も展開して渡せる</a></span></li></ul></li><li><span><a href=\"#引数による分岐\" data-toc-modified-id=\"引数による分岐-7\"><span class=\"toc-item-num\">7 </span>引数による分岐</a></span><ul class=\"toc-item\"><li><span><a href=\"#引数型によって異なる関数を呼び出す\" data-toc-modified-id=\"引数型によって異なる関数を呼び出す-7.1\"><span class=\"toc-item-num\">7.1 </span>引数型によって異なる関数を呼び出す</a></span></li><li><span><a href=\"#複数マッチするときは型指定の強い方が呼び出される\" data-toc-modified-id=\"複数マッチするときは型指定の強い方が呼び出される-7.2\"><span class=\"toc-item-num\">7.2 </span>複数マッチするときは型指定の強い方が呼び出される</a></span></li><li><span><a href=\"#可変引数関数との関係\" data-toc-modified-id=\"可変引数関数との関係-7.3\"><span class=\"toc-item-num\">7.3 </span>可変引数関数との関係</a></span></li><li><span><a href=\"#省略可能引数やばい?!\" data-toc-modified-id=\"省略可能引数やばい?!-7.4\"><span class=\"toc-item-num\">7.4 </span>省略可能引数やばい?!</a></span></li><li><span><a href=\"#キーワード引数付き引数はシグネチャが区別されない\" data-toc-modified-id=\"キーワード引数付き引数はシグネチャが区別されない-7.5\"><span class=\"toc-item-num\">7.5 </span>キーワード引数付き引数はシグネチャが区別されない</a></span></li><li><span><a href=\"#もしかして省略可能引数も?\" data-toc-modified-id=\"もしかして省略可能引数も?-7.6\"><span class=\"toc-item-num\">7.6 </span>もしかして省略可能引数も?</a></span></li></ul></li></ul></div>" | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "# julia の関数について勉強\n\njulia における関数の機微について調べてみた" | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "# 試した環境" | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"scrolled": true | |
}, | |
"cell_type": "code", | |
"source": "versioninfo()", | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Julia Version 0.6.2\nCommit d386e40 (2017-12-13 18:08 UTC)\nPlatform Info:\n OS: Linux (x86_64-linux-gnu)\n CPU: Intel(R) Core(TM) i7-6600U CPU @ 2.60GHz\n WORD_SIZE: 64\n BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)\n LAPACK: libopenblas64_\n LIBM: libopenlibm\n LLVM: libLLVM-3.9.1 (ORCJIT, skylake)\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "# 関数の定義方法は2つ" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "function func1(x)\n sin(x) # return が無くても最後の式が返値になる\nend", | |
"execution_count": 2, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 2, | |
"data": { | |
"text/plain": "func1 (generic function with 1 method)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "func1(x) = sin(x) # 同じシグネチャで再定義すると上書き可能", | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 3, | |
"data": { | |
"text/plain": "func1 (generic function with 1 method)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "次の書き方でも func2 を定義できるけれど以下の相違がある\n* 関数自体は無名のまま\n* func2 は変数なので代入が可能" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "func2 = x -> sin(x)", | |
"execution_count": 4, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 4, | |
"data": { | |
"text/plain": "(::#1) (generic function with 1 method)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "func2 = func1", | |
"execution_count": 5, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 5, | |
"data": { | |
"text/plain": "func1 (generic function with 1 method)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "func2 = 1", | |
"execution_count": 6, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 6, | |
"data": { | |
"text/plain": "1" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "関数名は const 扱いで上書きできない" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "func1 = x -> sin(x)", | |
"execution_count": 7, | |
"outputs": [ | |
{ | |
"output_type": "error", | |
"ename": "LoadError", | |
"evalue": "\u001b[91minvalid redefinition of constant func1\u001b[39m", | |
"traceback": [ | |
"\u001b[91minvalid redefinition of constant func1\u001b[39m", | |
"", | |
"Stacktrace:", | |
" [1] \u001b[1minclude_string\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m::String, ::String\u001b[1m)\u001b[22m\u001b[22m at \u001b[1m./loading.jl:522\u001b[22m\u001b[22m" | |
] | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "const にすれば代入は不可能になるけど、関数自体は無名のまま" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "const func3 = x -> sin(x)", | |
"execution_count": 8, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 8, | |
"data": { | |
"text/plain": "(::#5) (generic function with 1 method)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "func2 = func3", | |
"execution_count": 9, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 9, | |
"data": { | |
"text/plain": "(::#5) (generic function with 1 method)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "# 無名関数を println" | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "無名関数は内部的に #数字 という名前(?)で管理されている?\n\n知ってないと表示を見ても何のことだか分からない" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "println(x->x)", | |
"execution_count": 10, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "#7\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"scrolled": true | |
}, | |
"cell_type": "code", | |
"source": "x->x", | |
"execution_count": 11, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 11, | |
"data": { | |
"text/plain": "(::#9) (generic function with 1 method)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "# 可変引数\n\n引数やキーワード引数に ... を付けると、Tuple として受け取れる" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "function test(a, other... ; opt1=0, options...)\n println(\"a=\", a, \", other=\", other)\n println(\"opt1=\", opt1, \", options=\", options)\nend", | |
"execution_count": 12, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 12, | |
"data": { | |
"text/plain": "test (generic function with 1 method)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test(1, 2, 3, 4; opt1=1, opt2=2, opt3=3)", | |
"execution_count": 13, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "a=1, other=(2, 3, 4)\nopt1=1, options=Any[(:opt2, 2), (:opt3, 3)]\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "# 引数の展開\n\n逆に、Tupleや配列に ... を付けて渡すと、展開して渡せる" | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"scrolled": true | |
}, | |
"cell_type": "code", | |
"source": "test((1,2,3)...)", | |
"execution_count": 14, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "a=1, other=(2, 3)\nopt1=0, options=Any[]\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test([1,2,3]...)", | |
"execution_count": 15, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "a=1, other=(2, 3)\nopt1=0, options=Any[]\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test([1,2]...)", | |
"execution_count": 16, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "a=1, other=(2,)\nopt1=0, options=Any[]\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "あれ?これは正しいのか?" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "length((1,))", | |
"execution_count": 17, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 17, | |
"data": { | |
"text/plain": "1" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "ああ、(1) だけだと数字の 1 と区別が付かないから、1要素の Tuple は後ろに , を付けてるのか。" | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "## キーワード引数も展開して渡せる" | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"scrolled": true | |
}, | |
"cell_type": "code", | |
"source": "test(1;[(:opt1, 1), (:opt2, 2)]...)", | |
"execution_count": 18, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "a=1, other=()\nopt1=1, options=Any[(:opt2, 2)]\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "# 引数による分岐" | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "## 引数型によって異なる関数を呼び出す\n\nポリモーフィズム的な" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test2(a::Int) = println(\"Int\")", | |
"execution_count": 19, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 19, | |
"data": { | |
"text/plain": "test2 (generic function with 1 method)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test2(s::String) = println(\"String\")", | |
"execution_count": 20, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 20, | |
"data": { | |
"text/plain": "test2 (generic function with 2 methods)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test2(1)", | |
"execution_count": 21, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Int\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test2(\"a\")", | |
"execution_count": 22, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "String\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "## 複数マッチするときは型指定の強い方が呼び出される" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test2(a::Number) = println(\"Number\")", | |
"execution_count": 23, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 23, | |
"data": { | |
"text/plain": "test2 (generic function with 3 methods)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test2(1.0)", | |
"execution_count": 24, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Number\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "1 は Number でもあるが Int が採用される" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test2(1)", | |
"execution_count": 25, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Int\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test2(a) = println(\"Any type\")", | |
"execution_count": 26, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 26, | |
"data": { | |
"text/plain": "test2 (generic function with 4 methods)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test2(true)", | |
"execution_count": 27, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Number\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "ありゃ、Bool は Number なのか。。。" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test2([])", | |
"execution_count": 28, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Any type\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"scrolled": true | |
}, | |
"cell_type": "code", | |
"source": "test2(1.0)", | |
"execution_count": 29, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Number\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test2(1)", | |
"execution_count": 30, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Int\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "## 可変引数関数との関係" | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "可変引数関数ではない方が優先される" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test2(a...) = println(\"Variable args of any type\")", | |
"execution_count": 31, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 31, | |
"data": { | |
"text/plain": "test2 (generic function with 5 methods)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test2(1)", | |
"execution_count": 32, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Int\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test2([])", | |
"execution_count": 33, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Any type\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "可変引数でも型指定があればそちらが優先" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test2(a::Array...) = print(\"Variable args of Array\")", | |
"execution_count": 34, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 34, | |
"data": { | |
"text/plain": "test2 (generic function with 6 methods)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"scrolled": false | |
}, | |
"cell_type": "code", | |
"source": "test2([])", | |
"execution_count": 35, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Variable args of Array", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test2(:symbol)", | |
"execution_count": 36, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Any type\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "## 省略可能引数やばい?!" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test2(a, b=false) = println(\"With omittable b\")", | |
"execution_count": 37, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 37, | |
"data": { | |
"text/plain": "test2 (generic function with 7 methods)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test2(1)", | |
"execution_count": 38, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Int\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test2(:symbol)", | |
"execution_count": 39, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "With omittable b\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "どうしてそっちが呼ばれるの? → 答えは下の方に" | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "## キーワード引数付き引数はシグネチャが区別されない" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test3(a) = println(\"Any type\")", | |
"execution_count": 40, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 40, | |
"data": { | |
"text/plain": "test3 (generic function with 1 method)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test3(a; option=false) = println(\"Any type with option\")", | |
"execution_count": 41, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 41, | |
"data": { | |
"text/plain": "test3 (generic function with 1 method)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "2 methods にならない事に注意" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test3(1)", | |
"execution_count": 42, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Any type with option\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "これは、キーワード引数付きが優先されたのではなく後から書いたもので上書きされただけ" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test3(a) = println(\"Any type\")", | |
"execution_count": 43, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 43, | |
"data": { | |
"text/plain": "test3 (generic function with 1 method)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test3(1)", | |
"execution_count": 44, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Any type\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test3(1, option=true)", | |
"execution_count": 45, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Any type with option\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "ではなかった。\n\n優先順位が変わっただけでキーワード引数付きも呼べている。\n\n1 method と書かれている意味は何なのか???" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test3(a; option2=false) = println(\"Any type with option2\")", | |
"execution_count": 46, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 46, | |
"data": { | |
"text/plain": "test3 (generic function with 1 method)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"scrolled": true | |
}, | |
"cell_type": "code", | |
"source": "test3(1, option=true)", | |
"execution_count": 47, | |
"outputs": [ | |
{ | |
"output_type": "error", | |
"ename": "LoadError", | |
"evalue": "\u001b[91mMethodError: no method matching test3(::Int64; option=true)\u001b[0m\nClosest candidates are:\n test3(::Any; option2) at In[46]:1\u001b[91m got unsupported keyword argument \"option\"\u001b[39m\u001b[39m", | |
"traceback": [ | |
"\u001b[91mMethodError: no method matching test3(::Int64; option=true)\u001b[0m\nClosest candidates are:\n test3(::Any; option2) at In[46]:1\u001b[91m got unsupported keyword argument \"option\"\u001b[39m\u001b[39m", | |
"", | |
"Stacktrace:", | |
" [1] \u001b[1m(::#kw##test3)\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m::Array{Any,1}, ::#test3, ::Int64\u001b[1m)\u001b[22m\u001b[22m at \u001b[1m./<missing>:0\u001b[22m\u001b[22m", | |
" [2] \u001b[1minclude_string\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m::String, ::String\u001b[1m)\u001b[22m\u001b[22m at \u001b[1m./loading.jl:522\u001b[22m\u001b[22m" | |
] | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"scrolled": true | |
}, | |
"cell_type": "code", | |
"source": "test3(1, option2=true)", | |
"execution_count": 48, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Any type with option2\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "キーワード引数無しとキーワード引数付きは別シグネチャだけど、異なる名前のキーワード付き同士は同一シグネチャなので、後から定義したもので上書きされてしまうということか。" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test3(a; options...) = println(\"Any type with options\")", | |
"execution_count": 49, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 49, | |
"data": { | |
"text/plain": "test3 (generic function with 1 method)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test3(1, option2=true)", | |
"execution_count": 50, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Any type with options\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "可変キーワード引数付きの優先度が高い?" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test3(a; option2=false) = println(\"Any type with option2\")", | |
"execution_count": 51, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 51, | |
"data": { | |
"text/plain": "test3 (generic function with 1 method)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test3(1, option2=true)", | |
"execution_count": 52, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Any type with option2\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "じゃなくて、後から定義されたものが優先されているだけ?" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test3(1, option1=true)", | |
"execution_count": 53, | |
"outputs": [ | |
{ | |
"output_type": "error", | |
"ename": "LoadError", | |
"evalue": "\u001b[91mMethodError: no method matching test3(::Int64; option1=true)\u001b[0m\nClosest candidates are:\n test3(::Any; option2) at In[51]:1\u001b[91m got unsupported keyword argument \"option1\"\u001b[39m\u001b[39m", | |
"traceback": [ | |
"\u001b[91mMethodError: no method matching test3(::Int64; option1=true)\u001b[0m\nClosest candidates are:\n test3(::Any; option2) at In[51]:1\u001b[91m got unsupported keyword argument \"option1\"\u001b[39m\u001b[39m", | |
"", | |
"Stacktrace:", | |
" [1] \u001b[1m(::#kw##test3)\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m::Array{Any,1}, ::#test3, ::Int64\u001b[1m)\u001b[22m\u001b[22m at \u001b[1m./<missing>:0\u001b[22m\u001b[22m", | |
" [2] \u001b[1minclude_string\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m::String, ::String\u001b[1m)\u001b[22m\u001b[22m at \u001b[1m./loading.jl:522\u001b[22m\u001b[22m" | |
] | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "いやいや、上書きされてるや\n\n指定キーワード付きと、可変キーワード付きは同一シグネチャとして、互いに上書き対象になってる" | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "## もしかして省略可能引数も?" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test4(a) = \"only a\"\ntest4(a, b=false) = \"with omittable b\"", | |
"execution_count": 54, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 54, | |
"data": { | |
"text/plain": "test4 (generic function with 2 methods)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test4(\"which should be called?\")", | |
"execution_count": 55, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 55, | |
"data": { | |
"text/plain": "\"with omittable b\"" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "優先順位が同位なので、後から定義されたものが採用されてるわけだ\n\nだからもう一度「後から」定義してやればそちらが優先される" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test4(a) = \"only a\"", | |
"execution_count": 56, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 56, | |
"data": { | |
"text/plain": "test4 (generic function with 2 methods)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "test4(\"which should be called?\")", | |
"execution_count": 57, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 57, | |
"data": { | |
"text/plain": "\"only a\"" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "省略可能引数持ちとそうでないものとを両方定義するときは順番が重要と覚えよう" | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "ちゃんとこっちも呼べる" | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"scrolled": true | |
}, | |
"cell_type": "code", | |
"source": "test4(1, 2)", | |
"execution_count": 58, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 58, | |
"data": { | |
"text/plain": "\"with omittable b\"" | |
}, | |
"metadata": {} | |
} | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"name": "julia-0.6", | |
"display_name": "Julia 0.6.2", | |
"language": "julia" | |
}, | |
"toc": { | |
"nav_menu": {}, | |
"number_sections": true, | |
"sideBar": true, | |
"skip_h1_title": false, | |
"base_numbering": 1, | |
"title_cell": "Table of Contents", | |
"title_sidebar": "Contents", | |
"toc_cell": true, | |
"toc_position": {}, | |
"toc_section_display": true, | |
"toc_window_display": false | |
}, | |
"language_info": { | |
"mimetype": "application/julia", | |
"file_extension": ".jl", | |
"name": "julia", | |
"version": "0.6.2" | |
}, | |
"gist": { | |
"id": "5b6aa4f2cf74297e1bc4a6b22fd88a22", | |
"data": { | |
"description": "julia の関数について勉強", | |
"public": true | |
} | |
}, | |
"_draft": { | |
"nbviewer_url": "https://gist.github.com/5b6aa4f2cf74297e1bc4a6b22fd88a22" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment