Skip to content

Instantly share code, notes, and snippets.

@genkuroki
Last active May 18, 2021 09:22
Show Gist options
  • Save genkuroki/aaa62e86ac3fb1183db1b0e5782f0b4d to your computer and use it in GitHub Desktop.
Save genkuroki/aaa62e86ac3fb1183db1b0e5782f0b4d to your computer and use it in GitHub Desktop.
(;a, b, c) = x syntax
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "nt = (a = 1.0, b = 2.0, c = [3.0, 4.0], d = [5.0, 6.0])",
"execution_count": 1,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 1,
"data": {
"text/plain": "(a = 1.0, b = 2.0, c = [3.0, 4.0], d = [5.0, 6.0])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "(; d, b) = nt\nb, d",
"execution_count": 2,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 2,
"data": {
"text/plain": "(2.0, [5.0, 6.0])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "module A\nBase.@kwdef struct Foo{T, U<:AbstractArray{T}}\n a::T = 11.0\n b::T = 12.0\n c::U = [13.0, 14.0]\n d::U = [15.0, 16.0]\nend\nend\n\nfoo = A.Foo()",
"execution_count": 3,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 3,
"data": {
"text/plain": "Main.A.Foo{Float64, Vector{Float64}}(11.0, 12.0, [13.0, 14.0], [15.0, 16.0])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "(; c, a) = foo\na, c",
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 4,
"data": {
"text/plain": "(11.0, [13.0, 14.0])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "using ComponentArrays\n\nca = ComponentArray(a = 21.0, b = 22.0, c = [23.0, 24.0], d = [25.0, 26.0])",
"execution_count": 5,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 5,
"data": {
"text/plain": "ComponentVector{Float64}(a = 21.0, b = 22.0, c = [23.0, 24.0], d = [25.0, 26.0])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "(; a, d) = ca\na, d",
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 6,
"data": {
"text/plain": "(21.0, [25.0, 26.0])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "2ca",
"execution_count": 7,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 7,
"data": {
"text/plain": "ComponentVector{Float64}(a = 42.0, b = 44.0, c = [46.0, 48.0], d = [50.0, 52.0])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "ca .- 20",
"execution_count": 8,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 8,
"data": {
"text/plain": "ComponentVector{Float64}(a = 1.0, b = 2.0, c = [3.0, 4.0], d = [5.0, 6.0])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "ca .- 20 == ComponentArray(nt)",
"execution_count": 9,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 9,
"data": {
"text/plain": "true"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "f((; c, a)) = @show a c",
"execution_count": 10,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 10,
"data": {
"text/plain": "f (generic function with 1 method)"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "methods(f)",
"execution_count": 11,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 11,
"data": {
"text/plain": "# 1 method for generic function \"f\":\n[1] f(::Any) in Main at In[10]:1",
"text/html": "# 1 method for generic function <b>f</b>:<ul><li> f(::<b>Any</b>) in Main at In[10]:1</li> </ul>"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "f(nt);",
"execution_count": 13,
"outputs": [
{
"output_type": "stream",
"text": "a = 1.0\nc = [3.0, 4.0]\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "f(foo);",
"execution_count": 14,
"outputs": [
{
"output_type": "stream",
"text": "a = 11.0\nc = [13.0, 14.0]\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "f(ca);",
"execution_count": 15,
"outputs": [
{
"output_type": "stream",
"text": "a = 21.0\nc = [23.0, 24.0]\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "julia-1.7-depwarn-o3",
"display_name": "Julia 1.7.0-DEV depwarn -O3",
"language": "julia"
},
"language_info": {
"file_extension": ".jl",
"name": "julia",
"mimetype": "application/julia",
"version": "1.7.0"
},
"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": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"@webio": {
"lastKernelId": null,
"lastCommId": null
},
"gist": {
"id": "aaa62e86ac3fb1183db1b0e5782f0b4d",
"data": {
"description": "(;a, b, c) = x syntax",
"public": true
}
},
"_draft": {
"nbviewer_url": "https://gist.github.com/aaa62e86ac3fb1183db1b0e5782f0b4d"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment