Skip to content

Instantly share code, notes, and snippets.

@St-Hakky
Created February 8, 2018 03:36
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/6393b18cf98697e528e03f91ca35cde0 to your computer and use it in GitHub Desktop.
Save St-Hakky/6393b18cf98697e528e03f91ca35cde0 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=\"#Functions\" data-toc-modified-id=\"Functions-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>Functions</a></div><div class=\"lev2 toc-item\"><a href=\"#関数の宣言\" data-toc-modified-id=\"関数の宣言-11\"><span class=\"toc-item-num\">1.1&nbsp;&nbsp;</span>関数の宣言</a></div><div class=\"lev2 toc-item\"><a href=\"#代入形式での関数宣言\" data-toc-modified-id=\"代入形式での関数宣言-12\"><span class=\"toc-item-num\">1.2&nbsp;&nbsp;</span>代入形式での関数宣言</a></div><div class=\"lev2 toc-item\"><a href=\"#無名関数\" data-toc-modified-id=\"無名関数-13\"><span class=\"toc-item-num\">1.3&nbsp;&nbsp;</span>無名関数</a></div><div class=\"lev2 toc-item\"><a href=\"#return\" data-toc-modified-id=\"return-14\"><span class=\"toc-item-num\">1.4&nbsp;&nbsp;</span>return</a></div><div class=\"lev2 toc-item\"><a href=\"#operatorを利用した関数\" data-toc-modified-id=\"operatorを利用した関数-15\"><span class=\"toc-item-num\">1.5&nbsp;&nbsp;</span>operatorを利用した関数</a></div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Functions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 関数の宣言"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-08T12:34:49.794000Z",
"start_time": "2018-02-08T03:34:48.623Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"say_name (generic function with 1 method)"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# function~endで関数を定義する。\n",
"# Pythonはコロンとインデントでfunctionのbodyを判定するが、juliaでは、function~endでやるみたい。\n",
"function say_name(name)\n",
" println(\"$name\")\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-08T12:34:50.551000Z",
"start_time": "2018-02-08T03:34:50.317Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"banana\n"
]
}
],
"source": [
"# 関数を宣言したら、以下のように使用できる。\n",
"say_name(\"banana\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 代入形式での関数宣言"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-08T12:34:51.671000Z",
"start_time": "2018-02-08T03:34:51.668Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"say_name_oneline (generic function with 1 method)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 上で示した関数程度ならワンライナーで関数も宣言できる。\n",
"say_name_oneline(name) = println(\"$name\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-08T12:34:52.143000Z",
"start_time": "2018-02-08T03:34:52.134Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"banana\n"
]
}
],
"source": [
"# 使い方は同じ\n",
"say_name_oneline(\"banana\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-08T12:34:52.656000Z",
"start_time": "2018-02-08T03:34:52.651Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"say_name_lines (generic function with 1 method)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 以下みたいな感じで、複数行のやつもできる\n",
"say_name_lines(name) = begin\n",
" output = name * \" : 複数行\"\n",
" println(output)\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-08T12:34:53.153000Z",
"start_time": "2018-02-08T03:34:53.142Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"banana : 複数行\n"
]
}
],
"source": [
"say_name_lines(\"banana\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 無名関数"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-08T12:34:54.136000Z",
"start_time": "2018-02-08T03:34:54.132Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"(::#1) (generic function with 1 method)"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# pythonでいうところのlambdaみたいなことができる\n",
"say_name_anonymous = name -> println(\"$name\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-08T12:34:54.532000Z",
"start_time": "2018-02-08T03:34:54.520Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"banana\n"
]
}
],
"source": [
"say_name_anonymous(\"banana\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## return"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-08T12:34:55.493000Z",
"start_time": "2018-02-08T03:34:55.452Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"15\n"
]
}
],
"source": [
"# juliaではdefaultで、一番最後に評価した値を返します。\n",
"# そのため、return文を使わなくても値を返すことができます。\n",
"\n",
"function sample(x, y)\n",
" # returnを使っていない\n",
" x + y\n",
"end\n",
"\n",
"value = sample(5, 10)\n",
"# でも値は返ってくる\n",
"println(value)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-08T12:34:55.866000Z",
"start_time": "2018-02-08T03:34:55.853Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"50\n"
]
}
],
"source": [
"# returnを使うとそこで強制的に値を返す\n",
"function sample_return(x, y)\n",
" return x * y\n",
" x + y\n",
"end\n",
"\n",
"value = sample_return(5, 10)\n",
"\n",
"# 掛け算した値が返ってくる\n",
"println(value)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-08T12:34:56.425000Z",
"start_time": "2018-02-08T03:34:56.419Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"fizzbuzz (generic function with 1 method)"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 普通はもう少し入り組んだ処理を書くと思うので、returnを普通に使うと思います\n",
"# returnで値を返すとそれ以降の処理をしなくなるので、便利です。\n",
"function fizzbuzz(x)\n",
" if x % 3 == 0 && x % 5 == 0\n",
" return \"fizzbuzz\"\n",
" elseif x % 3 == 0\n",
" return \"fizz\"\n",
" elseif x % 5 == 0\n",
" return \"buzz\"\n",
" else\n",
" return \"\"\n",
" end\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-08T12:34:57.241000Z",
"start_time": "2018-02-08T03:34:56.843Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"\"fizzbuzz\""
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fizzbuzz(15)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## operatorを利用した関数"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-08T12:34:57.829000Z",
"start_time": "2018-02-08T03:34:57.699Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# こんなことができるんですね笑\n",
"+(1, 2, 3)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-08T12:34:58.138000Z",
"start_time": "2018-02-08T03:34:58.132Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"24"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# これはすなわち以下と同じです。\n",
"1 + 2 + 3\n",
"\n",
"# juliaでは、ほとんどのoperatorが関数として特別なsytaxをサポートしているみたいです。\n",
"# この書き方は斬新ですね笑\n",
"*(1, 2, 3, 4)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-08T12:34:58.824000Z",
"start_time": "2018-02-08T03:34:58.820Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# つまり、以下と同じことをしているみたい\n",
"f = +\n",
"f(1,2,3)\n",
"\n",
"# 足し算とかの処理程度であれば、入力が可変にできますし、便利ですね。"
]
}
],
"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": "115px",
"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