Skip to content

Instantly share code, notes, and snippets.

@St-Hakky
Created February 7, 2018 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save St-Hakky/8b76c0dd42002ff69d1e1bdd9afedd1c to your computer and use it in GitHub Desktop.
Save St-Hakky/8b76c0dd42002ff69d1e1bdd9afedd1c to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"toc": "true"
},
"source": [
"# Table of Contents\n",
" <p><div class=\"lev1 toc-item\"><a href=\"#Conditionals\" data-toc-modified-id=\"Conditionals-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>Conditionals</a></div><div class=\"lev2 toc-item\"><a href=\"#Compound-Expressions\" data-toc-modified-id=\"Compound-Expressions-11\"><span class=\"toc-item-num\">1.1&nbsp;&nbsp;</span>Compound Expressions</a></div><div class=\"lev2 toc-item\"><a href=\"#if\" data-toc-modified-id=\"if-12\"><span class=\"toc-item-num\">1.2&nbsp;&nbsp;</span>if</a></div><div class=\"lev2 toc-item\"><a href=\"#三項演算子(ternary-operators)\" data-toc-modified-id=\"三項演算子(ternary-operators)-13\"><span class=\"toc-item-num\">1.3&nbsp;&nbsp;</span>三項演算子(ternary operators)</a></div><div class=\"lev1 toc-item\"><a href=\"#Short---Circuit-Evaluation\" data-toc-modified-id=\"Short---Circuit-Evaluation-2\"><span class=\"toc-item-num\">2&nbsp;&nbsp;</span>Short - Circuit Evaluation</a></div><div class=\"lev2 toc-item\"><a href=\"#&amp;と&amp;&amp;の違い\" data-toc-modified-id=\"&amp;と&amp;&amp;の違い-21\"><span class=\"toc-item-num\">2.1&nbsp;&nbsp;</span>&amp;と&amp;&amp;の違い</a></div><div class=\"lev2 toc-item\"><a href=\"#|と||も同様に異なる\" data-toc-modified-id=\"|と||も同様に異なる-22\"><span class=\"toc-item-num\">2.2&nbsp;&nbsp;</span>|と||も同様に異なる</a></div><div class=\"lev2 toc-item\"><a href=\"#これを利用した評価後の処理の書き方\" data-toc-modified-id=\"これを利用した評価後の処理の書き方-23\"><span class=\"toc-item-num\">2.3&nbsp;&nbsp;</span>これを利用した評価後の処理の書き方</a></div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Conditionals"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Compound Expressions\n",
"\n",
"これは、if文だけに適用されるものではないんですが、一応やっておきたいと思います。"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-07T23:03:38.006000Z",
"start_time": "2018-02-07T14:03:36.040Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"2-element Array{String,1}:\n",
" \"apple\" \n",
" \"orange\""
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# これは結構数式っぽい考え方かもしれないです(julia初心者なので雰囲気だけですが笑)。\n",
"# 複数の式の計算を一つの固まりとして捉えたいときに使います。\n",
"# 以下は、なんの意味もない例ですが、こんな風にbeginとendでまとまりにできます。\n",
"# latexでの数式っぽい書き方だなぁと笑\n",
"fruits = begin\n",
" apple = \"apple\"\n",
" orange = \"orange\"\n",
" [apple, orange]\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-07T23:03:39.093000Z",
"start_time": "2018-02-07T14:03:39.086Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"2-element Array{String,1}:\n",
" \"apple\" \n",
" \"orange\""
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# ちなみに、「;」でも対応できます。\n",
"z = (apple = \"apple\"; orange = \"orange\"; [apple, orange])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"塊としてではなく、普通に書いても同じことなのですが、数式などで表現されたものを扱うときに、その範囲がどこにあるかや、ワンライナーで式を表現したいときには使えるなぁと想いました。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## if"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-07T23:03:41.978000Z",
"start_time": "2018-02-07T14:03:41.733Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"if_example (generic function with 1 method)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# if文の書き方は、pythonとほぼおなじ。elseifだけ違うって感じですね。\n",
"# ここでもpythonとは違い、コロンを使いません。とてもつけたくなる…。\n",
"function if_example(x, y)\n",
" if x < y\n",
" println(\"x : $x は y : $y よりも小さい\")\n",
" elseif x > y\n",
" println(\"x : $x は y : $y よりも大きい\")\n",
" else\n",
" println(\"x : $x と y : $y は等しい\")\n",
" end\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-07T23:03:44.146000Z",
"start_time": "2018-02-07T14:03:43.990Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x : 10 は y : 100 よりも小さい\n"
]
}
],
"source": [
"# test1\n",
"if_example(10, 100)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-07T23:03:44.700000Z",
"start_time": "2018-02-07T14:03:44.577Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x : 1000 は y : 100 よりも大きい\n"
]
}
],
"source": [
"# test2\n",
"if_example(1000, 100)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-07T23:03:45.160000Z",
"start_time": "2018-02-07T14:03:45.157Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x : 100 と y : 100 は等しい\n"
]
}
],
"source": [
"# test3\n",
"if_example(100, 100)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"こんな感じで使うことができます。ちなみに、elseとelseifはoptionalなので、なくても使えます。"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-07T23:03:46.670000Z",
"start_time": "2018-02-07T14:03:46.660Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"FizzBuzz\n"
]
}
],
"source": [
"N = 15\n",
"if (N % 3 == 0) & (N % 5 == 0)\n",
" println(\"FizzBuzz\")\n",
"end"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 三項演算子(ternary operators)\n",
"\n",
"以下の三項演算子は、\n",
"\n",
"```\n",
"a ? b : c\n",
"```\n",
"\n",
"普通にif文を使って書くと、\n",
"```\n",
"if a\n",
" b\n",
"else\n",
" c\n",
"end\n",
"```\n",
"\n",
"みたいな感じになります。aが条件になっていて、trueのときにbを、falseのときにcを返します。"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-07T23:03:50.317000Z",
"start_time": "2018-02-07T14:03:50.125Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# こんな感じです\n",
"x = (10 > 5) ? 10 : 5"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-07T23:03:50.959000Z",
"start_time": "2018-02-07T14:03:50.956Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"false\n"
]
}
],
"source": [
"# 出力を返すときにも使える。便利。\n",
"# 以下の例だと、falseが返ってくる\n",
"println(10 < 5 ? \"true\" : \"false\")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-07T23:03:51.627000Z",
"start_time": "2018-02-07T14:03:51.620Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"example (generic function with 1 method)"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 三項演算子でif-elseif-elseみたいなこともできる。\n",
"# 以下がその例\n",
"example(x, y) = println(x < y ? \"$x は $y よりも小さい(if)\" :\n",
" x > y ? \"$x は $y よりも大きい(elseif)\" : \"$x と $y は等しい\")"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-07T23:03:52.160000Z",
"start_time": "2018-02-07T14:03:52.138Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2 は 5 よりも小さい(if)\n"
]
}
],
"source": [
"example(2, 5)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-07T23:03:52.721000Z",
"start_time": "2018-02-07T14:03:52.718Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10 は 5 よりも大きい(elseif)\n"
]
}
],
"source": [
"example(10, 5)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-07T23:03:53.286000Z",
"start_time": "2018-02-07T14:03:53.277Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2 と 2 は等しい\n"
]
}
],
"source": [
"example(2, 2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Short - Circuit Evaluation\n",
"\n",
"これは個人的にとてもおもしろかったです。こんな考え方というか使い方ができるのか、という点で。"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"## &と&&の違い"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-07T23:03:56.387000Z",
"start_time": "2018-02-07T14:03:56.204Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"apple => 1\n",
"orange => 2\n"
]
},
{
"data": {
"text/plain": [
"false"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# この2つはどちらも、「2つの条件がともに真の時、trueを返します」が、挙動が違います。\n",
"# まずは挙動を確認してみたいと思います。\n",
"# 挙動を確認するために適当な関数を使います。\n",
"# 以下は、果物の個数を出力した後にtrueとfalseをそれぞれ返す関数です。\n",
"apple_true(x) = (println(\"apple => $x\"); true)\n",
"orange_false(x) = (println(\"orange => $x\"); false)\n",
"\n",
"# &から挙動を確認します。\n",
"apple_true(1) & orange_false(2)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-07T23:03:56.391000Z",
"start_time": "2018-02-07T14:03:56.380Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"orange => 2\n",
"apple => 1\n"
]
},
{
"data": {
"text/plain": [
"false"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# &では、一つ目の要素がtrueでもfalseでも両方評価します。\n",
"# そのため、一つ目がfalseを返しても、二つ目を評価します。\n",
"orange_false(2) & apple_true(1)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-07T23:03:56.559000Z",
"start_time": "2018-02-07T14:03:56.552Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"apple => 1\n",
"orange => 2\n"
]
},
{
"data": {
"text/plain": [
"false"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 次に&&を見ます。以下は、&と同じ挙動をします笑い\n",
"apple_true(1) && orange_false(2)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-07T23:03:58.472000Z",
"start_time": "2018-02-07T14:03:58.468Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"orange => 2\n"
]
},
{
"data": {
"text/plain": [
"false"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# &&では、一つ目の要素を評価してfalseであればそこで評価をとめます。\n",
"# そのため、以下のコードでは、二つ目を評価するところまで行きません\n",
"orange_false(2) && apple_true(1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## |と||も同様に異なる"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-07T23:03:59.794000Z",
"start_time": "2018-02-07T14:03:59.790Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"apple => 1\n",
"orange => 2\n"
]
},
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# この2つはどちらも、「2つの条件のどちらかが真の時、trueを返します」が、「&」と「&&」と同様に挙動が違います。\n",
"\n",
"# 以下は、一つ目がtureの場合、2つ目を評価する必要が無いんですが、評価しています。\n",
"apple_true(1) | orange_false(2)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-07T23:04:00.856000Z",
"start_time": "2018-02-07T14:04:00.852Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"apple => 1\n"
]
},
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# これが「||」の場合だと、二つ目を評価しません。\n",
"apple_true(1) || orange_false(2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## これを利用した評価後の処理の書き方\n",
"\n",
"これは、単純に評価が手短になる以外の効果を持っています。\n",
"\n",
"具体的には、「&&」を用いると、一つ目の条件を評価してそれがtrueだったときだけ次の処理を行うみたいなことができます。\n",
"\n",
"また、「||」を用いると、一つ目の条件を評価してそれがfalseだったときだけ次の処理を行うみたいなことができます。"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-07T23:04:01.932000Z",
"start_time": "2018-02-07T14:04:01.925Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"get_fruits_num (generic function with 1 method)"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# これを利用すると、以下のようなことができます。\n",
"function get_fruits_num(name)\n",
" name == \"orange\" && return 1\n",
" name == \"apple\" && return 2\n",
" name == \"banana\" && return 3\n",
" name in [\"orange\", \"apple\", \"banana\"] || error(\"$name はこの関数の中にありません\")\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-07T23:04:02.671000Z",
"start_time": "2018-02-07T14:04:02.647Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# この関数を使ってみます。\n",
"# orangeなので、1が返ってくるはずです\n",
"get_fruits_num(\"orange\")"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-07T23:04:03.187000Z",
"start_time": "2018-02-07T14:04:03.185Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# appleなので、2が返ってくるはずです\n",
"get_fruits_num(\"apple\")"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-07T23:04:03.603000Z",
"start_time": "2018-02-07T14:04:03.600Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# bananaなので、3が返ってくるはずです\n",
"get_fruits_num(\"banana\")"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-07T23:04:04.546000Z",
"start_time": "2018-02-07T14:04:03.972Z"
}
},
"outputs": [
{
"ename": "LoadError",
"evalue": "\u001b[91mpeach はこの関数の中にありません\u001b[39m",
"output_type": "error",
"traceback": [
"\u001b[91mpeach はこの関数の中にありません\u001b[39m",
"",
"Stacktrace:",
" [1] \u001b[1mget_fruits_num\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m::String\u001b[1m)\u001b[22m\u001b[22m at \u001b[1m.\\In[20]:6\u001b[22m\u001b[22m"
]
}
],
"source": [
"# エラーが返ってくるはずです\n",
"get_fruits_num(\"peach\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"こんな風に、条件に対してtrueだった時あるいは、falseだった時のみだけ処理を走らせたいときにはこんな方法もあることをしっておくと便利だなと思いました。"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 0.6.0",
"language": "julia",
"name": "julia-0.6"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "0.6.0"
},
"latex_envs": {
"LaTeX_envs_menu_present": true,
"autocomplete": true,
"bibliofile": "biblio.bib",
"cite_by": "apalike",
"current_citInitial": 1,
"eqLabelWithNumbers": true,
"eqNumInitial": 1,
"hotkeys": {
"equation": "Ctrl-E",
"itemize": "Ctrl-I"
},
"labels_anchors": false,
"latex_user_defs": false,
"report_style_numbering": false,
"user_envs_cfg": false
},
"toc": {
"colors": {
"hover_highlight": "#DAA520",
"navigate_num": "#000000",
"navigate_text": "#333333",
"running_highlight": "#FF0000",
"selected_highlight": "#FFD700",
"sidebar_border": "#EEEEEE",
"wrapper_background": "#FFFFFF"
},
"moveMenuLeft": true,
"nav_menu": {
"height": "99px",
"width": "254px"
},
"navigate_menu": true,
"number_sections": true,
"sideBar": true,
"threshold": 4,
"toc_cell": true,
"toc_section_display": "block",
"toc_window_display": true,
"widenNotebook": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment