Last active
September 22, 2021 13:13
-
-
Save antimon2/d2dc94f472366d27230916ad93a01acf to your computer and use it in GitHub Desktop.
PropertyDestructuringAssignment.jl
This file contains hidden or 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
[compat] | |
julia = "1.6" | |
[deps] | |
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" | |
ExprTools = "e2ba6199-217a-4e67-a87a-7c52f15ade04" |
This file contains hidden or 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": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:13.099Z", | |
"end_time": "2021-09-22T22:08:17.072000+09:00" | |
}, | |
"trusted": true, | |
"scrolled": true | |
}, | |
"cell_type": "code", | |
"source": "versioninfo()", | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Julia Version 1.6.2\nCommit 1b93d53fc4 (2021-07-14 15:36 UTC)\nPlatform Info:\n OS: Linux (x86_64-pc-linux-gnu)\n CPU: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz\n WORD_SIZE: 64\n LIBM: libopenlibm\n LLVM: libLLVM-11.0.1 (ORCJIT, skylake)\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:07:42.020Z", | |
"end_time": "2021-09-22T22:07:51.278000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# ]activate .", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:07:42.021Z", | |
"end_time": "2021-09-22T22:07:51.278000+09:00" | |
}, | |
"trusted": true, | |
"scrolled": true | |
}, | |
"cell_type": "code", | |
"source": "# ]add BenchmarkTools ExprTools", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.035Z", | |
"end_time": "2021-09-22T22:08:28.679000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "ex1 = :((;x, y) = pt)", | |
"execution_count": 2, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 2, | |
"data": { | |
"text/plain": ":((; x, y) = pt)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.037Z", | |
"end_time": "2021-09-22T22:08:29.308000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "dump(ex1)", | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Expr\n head: Symbol =\n args: Array{Any}((2,))\n 1: Expr\n head: Symbol tuple\n args: Array{Any}((1,))\n 1: Expr\n head: Symbol parameters\n args: Array{Any}((2,))\n 1: Symbol x\n 2: Symbol y\n 2: Symbol pt\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.038Z", | |
"end_time": "2021-09-22T22:08:29.562000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "Meta.isexpr(ex1, :(=))", | |
"execution_count": 4, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 4, | |
"data": { | |
"text/plain": "true" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.040Z", | |
"end_time": "2021-09-22T22:08:29.563000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "Meta.isexpr(ex1.args[1], :tuple)", | |
"execution_count": 5, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 5, | |
"data": { | |
"text/plain": "true" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.042Z", | |
"end_time": "2021-09-22T22:08:29.563000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "Meta.isexpr(ex1.args[1].args[1], :parameters)", | |
"execution_count": 6, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 6, | |
"data": { | |
"text/plain": "true" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.043Z", | |
"end_time": "2021-09-22T22:08:30.020000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "function _is_property_assignee(ex)\n Meta.isexpr(ex, :tuple) &&\n Meta.isexpr(ex.args[1], :parameters)\nend ", | |
"execution_count": 7, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 7, | |
"data": { | |
"text/plain": "_is_property_assignee (generic function with 1 method)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.045Z", | |
"end_time": "2021-09-22T22:08:30.100000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "function _is_destructure_property_assignment(ex)\n Meta.isexpr(ex, :(=)) && _is_property_assignee(ex.args[1])\nend", | |
"execution_count": 8, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 8, | |
"data": { | |
"text/plain": "_is_destructure_property_assignment (generic function with 1 method)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.046Z", | |
"end_time": "2021-09-22T22:08:30.112000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "_is_destructure_property_assignment(ex1)", | |
"execution_count": 9, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 9, | |
"data": { | |
"text/plain": "true" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.048Z", | |
"end_time": "2021-09-22T22:08:30.189000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "function _is_destructure_property_for(ex)\n Meta.isexpr(ex, :for) && _is_destructure_property_assignment(ex.args[1])\nend", | |
"execution_count": 10, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 10, | |
"data": { | |
"text/plain": "_is_destructure_property_for (generic function with 1 method)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.049Z", | |
"end_time": "2021-09-22T22:08:30.275000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "function _destructure_property_for(ex)\n # @assert Meta.isexpr(ex, :for) && _is_destructure_property_assignment(ex.args[1])\n blk = _destructure(ex.args[2])\n variables = ex.args[1].args[1].args[1].args\n lh = Expr(:tuple, variables...)\n _el = gensym(:el)\n gvars = Expr(:tuple, (:($_el.$var) for var in variables)...)\n src0 = _destructure(ex.args[1].args[2])\n src = :(($gvars for $_el in $(src0)))\n Expr(:for, :($lh = $src), blk)\nend", | |
"execution_count": 11, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 11, | |
"data": { | |
"text/plain": "_destructure_property_for (generic function with 1 method)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.051Z", | |
"end_time": "2021-09-22T22:08:30.353000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "function _destructure_property_assignment(ex)\n # @assert _is_destructure_property_assignment(ex)\n variables = ex.args[1].args[1].args\n src = _destructure(ex.args[2])\n assignments = Expr(:block, (:($(var) = getproperty($src, $(QuoteNode(var)))) for var in variables)...)\n quote\n $assignments\n $src\n end\nend", | |
"execution_count": 12, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 12, | |
"data": { | |
"text/plain": "_destructure_property_assignment (generic function with 1 method)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.052Z", | |
"end_time": "2021-09-22T22:08:30.354000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "using ExprTools", | |
"execution_count": 13, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.055Z", | |
"end_time": "2021-09-22T22:08:30.370000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "exfd = :(function sample((;x, y)=(x=1,y=2))\n println(\"x=\", x, \", y=\", y)\nend)", | |
"execution_count": 14, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 14, | |
"data": { | |
"text/plain": ":(function sample((; x, y) = (x = 1, y = 2))\n #= In[14]:1 =#\n #= In[14]:2 =#\n println(\"x=\", x, \", y=\", y)\n end)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.057Z", | |
"end_time": "2021-09-22T22:08:31.609000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "exfd_dict = splitdef(exfd)", | |
"execution_count": 15, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 15, | |
"data": { | |
"text/plain": "Dict{Symbol, Any} with 4 entries:\n :args => Any[:($(Expr(:kw, :((; x, y)), :((x = 1, y = 2)))))]\n :body => quote…\n :name => :sample\n :head => :function" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.058Z", | |
"end_time": "2021-09-22T22:08:31.642000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# splitdef(exfd_dict[:body])\nsplitdef(exfd_dict[:body]; throw=false)", | |
"execution_count": 16, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.060Z", | |
"end_time": "2021-09-22T22:08:31.725000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# original(inspired by): https://github.com/FluxML/MacroTools.jl/blob/65c55530b63918daac5f041b144c50d4e34e7984/src/utils.jl#L438-L452\nsplitarg(name::Symbol) = (name, :Any, false, nothing)\nfunction splitarg(arg_expr)\n if Meta.isexpr(arg_expr, :kw)\n # デフォルト値が設定されている場合\n (splitarg(arg_expr.args[1])[1:3]..., arg_expr.args[2])\n elseif Meta.isexpr(arg_expr, :(...))\n # `...` が指定されている場合\n (splitarg(arg_expr.args[1])[1:2]..., true, nothing)\n elseif Meta.isexpr(arg_expr, :(::))\n # 型アノテーションが存在する場合\n _name = length(arg_expr.args) > 1 ? arg_expr.args[1] : nothing\n _type = arg_expr.args[end]\n (_name, _type, false, nothing)\n else\n (arg_expr, :Any, false, nothing)\n end\nend", | |
"execution_count": 17, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 17, | |
"data": { | |
"text/plain": "splitarg (generic function with 2 methods)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.061Z", | |
"end_time": "2021-09-22T22:08:32.910000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "exfd_args = splitarg.(exfd_dict[:args])", | |
"execution_count": 18, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 18, | |
"data": { | |
"text/plain": "1-element Vector{Tuple{Expr, Symbol, Bool, Expr}}:\n (:((; x, y)), :Any, 0, :((x = 1, y = 2)))" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.063Z", | |
"end_time": "2021-09-22T22:08:32.984000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# original(inspired by): https://github.com/FluxML/MacroTools.jl/blob/65c55530b63918daac5f041b144c50d4e34e7984/src/utils.jl#L414-L418\nfunction combinearg(_name, _type, _isslurp, _default)\n ex = isnothing(_name) ? :(::$_type) : :($_name::$_type)\n if _isslurp\n ex = Expr(:(...), ex)\n end\n if !isnothing(_default)\n ex = Expr(:kw, ex, _default)\n end\n ex\nend", | |
"execution_count": 19, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 19, | |
"data": { | |
"text/plain": "combinearg (generic function with 1 method)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.065Z", | |
"end_time": "2021-09-22T22:08:33.609000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "Base.splat(combinearg).([\n (:a, :Any, false, nothing),\n (:b, :Any, false, 1),\n (:c, :Int, false, nothing),\n (:d, :Int, false, 2),\n (:e, :Any, true, nothing)\n])", | |
"execution_count": 20, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 20, | |
"data": { | |
"text/plain": "5-element Vector{Expr}:\n :(a::Any)\n :($(Expr(:kw, :(b::Any), 1)))\n :(c::Int)\n :($(Expr(:kw, :(d::Int), 2)))\n :(e::Any...)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.067Z", | |
"end_time": "2021-09-22T22:08:33.623000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "combinearg(:((;x, y)), Any, false, :((x=1, y=2)))", | |
"execution_count": 21, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 21, | |
"data": { | |
"text/plain": ":($(Expr(:kw, :((; x, y)::Any), :((x = 1, y = 2)))))" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.069Z", | |
"end_time": "2021-09-22T22:08:33.711000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "function _destructure_function_definition(ex)\n # @assert ExprTools.isdef(ex)\n def_dict = ExprTools.splitdef(ex)\n body = def_dict[:body]\n # args = ExprTools.splitarg.(def_dict[:args])\n # if !any(_is_property_assignee, (arg[1] for arg in args))\n assignments = Expr[]\n if haskey(def_dict, :args)\n for (i, arg) in enumerate(def_dict[:args])\n (_name, _type, _slurp, _default) = splitarg(arg)\n if _is_property_assignee(_name)\n newvar = gensym()\n def_dict[:args][i] = combinearg(newvar, _type, _slurp, _default)\n variables = _name.args[1].args\n append!(assignments, [:($(var) = getproperty($newvar, $(QuoteNode(var)))) for var in variables])\n end\n end\n end\n body = Expr(body.head, _destructure.(body.args)...)\n if !isempty(assignments)\n body = Expr(:block, assignments..., body)\n end\n def_dict[:body] = body\n ExprTools.combinedef(def_dict)\nend", | |
"execution_count": 22, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 22, | |
"data": { | |
"text/plain": "_destructure_function_definition (generic function with 1 method)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.071Z", | |
"end_time": "2021-09-22T22:08:33.787000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "isdef(ex) = false\nisdef(ex::Expr) = !isnothing(ExprTools.splitdef(ex; throw=false))", | |
"execution_count": 23, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 23, | |
"data": { | |
"text/plain": "isdef (generic function with 2 methods)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.072Z", | |
"end_time": "2021-09-22T22:08:33.865000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "_destructure(ex) = ex\n\nfunction _destructure(ex::Expr)\n _is_destructure_property_for(ex) && return _destructure_property_for(ex)\n _is_destructure_property_assignment(ex) && return _destructure_property_assignment(ex)\n isdef(ex) && return _destructure_function_definition(ex)\n Expr(ex.head, _destructure.(ex.args)...)\nend", | |
"execution_count": 24, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 24, | |
"data": { | |
"text/plain": "_destructure (generic function with 2 methods)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.074Z", | |
"end_time": "2021-09-22T22:08:33.949000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "macro destructure(ex::Expr)\n VERSION < v\"1.7.0-DEV.364\" || return esc(ex)\n esc(_destructure(ex))\nend", | |
"execution_count": 25, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 25, | |
"data": { | |
"text/plain": "@destructure (macro with 1 method)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.075Z", | |
"end_time": "2021-09-22T22:08:34.121000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@macroexpand @destructure (;x, y) = pt", | |
"execution_count": 26, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 26, | |
"data": { | |
"text/plain": "quote\n #= In[12]:7 =#\n begin\n x = getproperty(pt, :x)\n y = getproperty(pt, :y)\n end\n #= In[12]:8 =#\n pt\nend" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.077Z", | |
"end_time": "2021-09-22T22:08:34.342000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@macroexpand @destructure for (;x, y) in pts\n println(\"x=$x, y=$y\")\nend", | |
"execution_count": 27, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 27, | |
"data": { | |
"text/plain": ":(for (x, y) = (((var\"##el#257\").x, (var\"##el#257\").y) for var\"##el#257\" = pts)\n #= In[27]:2 =#\n println(\"x=$(x), y=$(y)\")\n end)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.078Z", | |
"end_time": "2021-09-22T22:08:34.394000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@macroexpand @destructure function sample((;x, y))\n println(\"x=$x, y=$y\")\nend", | |
"execution_count": 28, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 28, | |
"data": { | |
"text/plain": ":(function sample(var\"##258\"::Any)\n x = getproperty(var\"##258\", :x)\n y = getproperty(var\"##258\", :y)\n begin\n #= In[28]:1 =#\n #= In[28]:2 =#\n println(\"x=$(x), y=$(y)\")\n end\n end)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.079Z", | |
"end_time": "2021-09-22T22:08:34.465000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@destructure function sample((;x, y))\n println(\"x=$x, y=$y\")\nend", | |
"execution_count": 29, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 29, | |
"data": { | |
"text/plain": "sample (generic function with 1 method)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.081Z", | |
"end_time": "2021-09-22T22:08:34.467000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "struct Point2D{T}\n x::T\n y::T\nend", | |
"execution_count": 30, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.082Z", | |
"end_time": "2021-09-22T22:08:34.726000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "pt1 = Point2D(1, 2)", | |
"execution_count": 31, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 31, | |
"data": { | |
"text/plain": "Point2D{Int64}(1, 2)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.084Z", | |
"end_time": "2021-09-22T22:08:34.731000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@destructure (;x, y) = pt1", | |
"execution_count": 32, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 32, | |
"data": { | |
"text/plain": "Point2D{Int64}(1, 2)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.085Z", | |
"end_time": "2021-09-22T22:08:34.987000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "(x, y)", | |
"execution_count": 33, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 33, | |
"data": { | |
"text/plain": "(1, 2)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.086Z", | |
"end_time": "2021-09-22T22:08:34.997000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "sample(pt1)", | |
"execution_count": 34, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "x=1, y=2\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.088Z", | |
"end_time": "2021-09-22T22:08:35.236000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "pt2 = Point2D(3.14, 9.99)", | |
"execution_count": 35, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 35, | |
"data": { | |
"text/plain": "Point2D{Float64}(3.14, 9.99)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.089Z", | |
"end_time": "2021-09-22T22:08:35.238000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "struct Point3D{T}\n x::T\n y::T\n z::T\nend", | |
"execution_count": 36, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.091Z", | |
"end_time": "2021-09-22T22:08:35.474000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# pt3 = Point3D(1//1, 1/3, π)\npt3 = Point3D(promote(1//1, 1/3, π)...)", | |
"execution_count": 37, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 37, | |
"data": { | |
"text/plain": "Point3D{Float64}(1.0, 0.3333333333333333, 3.141592653589793)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.092Z", | |
"end_time": "2021-09-22T22:08:35.910000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "nt = (x=:x, y='歪')", | |
"execution_count": 38, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 38, | |
"data": { | |
"text/plain": "(x = :x, y = '歪')" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.094Z", | |
"end_time": "2021-09-22T22:08:35.939000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "pts = [pt1, pt2, pt3, nt];", | |
"execution_count": 39, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.095Z", | |
"end_time": "2021-09-22T22:08:35.999000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@destructure for (;x, y) in pts\n println(\"x=$x, y=$y\")\nend", | |
"execution_count": 40, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "x=1, y=2\nx=3.14, y=9.99\nx=1.0, y=0.3333333333333333\nx=x, y=歪\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2021-09-22T13:08:29.096Z", | |
"end_time": "2021-09-22T22:08:36.580000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "sample.(pts)", | |
"execution_count": 41, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "x=1, y=2\nx=3.14, y=9.99\nx=1.0, y=0.3333333333333333\nx=x, y=歪\n", | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "execute_result", | |
"execution_count": 41, | |
"data": { | |
"text/plain": "4-element Vector{Nothing}:\n nothing\n nothing\n nothing\n nothing" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "", | |
"execution_count": null, | |
"outputs": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"name": "julia-1.6", | |
"display_name": "Julia 1.6.2", | |
"language": "julia" | |
}, | |
"language_info": { | |
"file_extension": ".jl", | |
"name": "julia", | |
"mimetype": "application/julia", | |
"version": "1.6.2" | |
}, | |
"gist": { | |
"id": "", | |
"data": { | |
"description": "PropertyDestructuringAssignment.jl", | |
"public": true | |
} | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment