Skip to content

Instantly share code, notes, and snippets.

@St-Hakky
Created January 30, 2018 05:32
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/797f8501ed66706289733506a668d297 to your computer and use it in GitHub Desktop.
Save St-Hakky/797f8501ed66706289733506a668d297 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=\"#Tuple\" data-toc-modified-id=\"Tuple-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>Tuple</a></div><div class=\"lev1 toc-item\"><a href=\"#Named-Tuple\" data-toc-modified-id=\"Named-Tuple-2\"><span class=\"toc-item-num\">2&nbsp;&nbsp;</span>Named Tuple</a></div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Tuple"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2018-01-30T14:32:12.397000Z",
"start_time": "2018-01-30T05:32:11.381Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"((1, 2, 3), (2, 3, 4))"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 普通のTupleの定義の仕方はPythonと同じ\n",
"tuple_data = (\"apple\", \"orange\", \"banana\")\n",
"several_types = (1, \"apple\", 2)\n",
"tuple_2d = ((1,2,3), (2,3,4))"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2018-01-30T14:32:13.330000Z",
"start_time": "2018-01-30T05:32:12.990Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"\"apple\""
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 要素の取り出し方も同じ\n",
"tuple_data[1]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2018-01-30T14:32:14.388000Z",
"start_time": "2018-01-30T05:32:13.814Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"([1, 2, 3],)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# これはPythonとは少し違う挙動。リストが一要素として入るのか。\n",
"tuple([1,2,3])"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2018-01-30T14:32:14.799000Z",
"start_time": "2018-01-30T05:32:14.661Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"(1, 2, 3)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# これはPythonではエラーがでるんですが、Juliaだとこれでtupleを定義できるらしい\n",
"tuple(1,2,3)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"ExecuteTime": {
"end_time": "2018-01-30T14:32:16.245000Z",
"start_time": "2018-01-30T05:32:15.373Z"
}
},
"outputs": [
{
"ename": "LoadError",
"evalue": "\u001b[91mMethodError: no method matching setindex!(::Tuple{String,String,String}, ::String, ::Int64)\u001b[39m",
"output_type": "error",
"traceback": [
"\u001b[91mMethodError: no method matching setindex!(::Tuple{String,String,String}, ::String, ::Int64)\u001b[39m",
""
]
}
],
"source": [
"# もちろん、tupleは不変なので、情報の更新などはできない。\n",
"tuple_data[1] = \"peach\""
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"ExecuteTime": {
"end_time": "2018-01-30T14:32:16.941000Z",
"start_time": "2018-01-30T05:32:16.661Z"
}
},
"outputs": [
{
"ename": "LoadError",
"evalue": "\u001b[91mMethodError: no method matching push!(::Tuple{String,String,String}, ::Int64)\u001b[0m\nClosest candidates are:\n push!(::Any, ::Any, \u001b[91m::Any\u001b[39m) at abstractarray.jl:1935\n push!(::Any, ::Any, \u001b[91m::Any\u001b[39m, \u001b[91m::Any...\u001b[39m) at abstractarray.jl:1936\n push!(\u001b[91m::Array{Any,1}\u001b[39m, ::ANY) at array.jl:624\n ...\u001b[39m",
"output_type": "error",
"traceback": [
"\u001b[91mMethodError: no method matching push!(::Tuple{String,String,String}, ::Int64)\u001b[0m\nClosest candidates are:\n push!(::Any, ::Any, \u001b[91m::Any\u001b[39m) at abstractarray.jl:1935\n push!(::Any, ::Any, \u001b[91m::Any\u001b[39m, \u001b[91m::Any...\u001b[39m) at abstractarray.jl:1936\n push!(\u001b[91m::Array{Any,1}\u001b[39m, ::ANY) at array.jl:624\n ...\u001b[39m",
""
]
}
],
"source": [
"# もちろん、これもできない。\n",
"push!(tuple_data,4)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Named Tuple\n",
"\n",
"- [blackrock/NamedTuples.jl | Github](https://github.com/blackrock/NamedTuples.jl)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2018-01-30T14:30:53.422000Z",
"start_time": "2018-01-30T05:30:31.709Z"
}
},
"outputs": [],
"source": [
"# Named Tupleを使うには、専用のパッケージをインストールする必要があります。\n",
"Pkg.add(\"NamedTuples\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"ExecuteTime": {
"end_time": "2018-01-30T14:32:20.234000Z",
"start_time": "2018-01-30T05:32:19.582Z"
}
},
"outputs": [],
"source": [
"# 使い方はREADME.mdに詳細が書かれているが、ここでもいくつか紹介する\n",
"using NamedTuples"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"ExecuteTime": {
"end_time": "2018-01-30T14:32:20.968000Z",
"start_time": "2018-01-30T05:32:20.006Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"1\n"
]
}
],
"source": [
"# 辞書と違い、普遍なオブジェクトでありながら、indexとして名前を指定できるのが嬉しい\n",
"named_tuple = @NT( apple=1, banana=2)\n",
"\n",
"# 名前で呼び出してみる\n",
"println(named_tuple.apple)\n",
"\n",
"# もちろん普通にインデックスで指定もできる。\n",
"# Juliaはインデックスが1始まりなのを忘れずに。\n",
"println(named_tuple[1])"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"ExecuteTime": {
"end_time": "2018-01-30T14:32:21.245000Z",
"start_time": "2018-01-30T05:32:20.501Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"(a = 1)"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# なんか型も指定できるらしい\n",
"named_tuple_type = @NT( apple::Int64, banana::Float64)\n",
"\n",
"# こんな風に指定もできる\n",
"named_tuple_1 = @NT( a, b)(\"apple\", \"banana\")\n",
"named_tuple_2 = @NT( a::Int64 )( 1 )"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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": "39px",
"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