Skip to content

Instantly share code, notes, and snippets.

@genkuroki
Last active May 12, 2021 10:06
Show Gist options
  • Save genkuroki/9a0aa74c051cf9c0590c3cfdcd04ba9c to your computer and use it in GitHub Desktop.
Save genkuroki/9a0aa74c051cf9c0590c3cfdcd04ba9c to your computer and use it in GitHub Desktop.
fill and comprehension
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "別々に作られた `Int64[]` と `Int64[]` は区別可能な互いに異なるオブジェクトになる。"
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "Int64[] === Int64[]",
"execution_count": 1,
"outputs": [
{
"data": {
"text/plain": "false"
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "別々に作られた `Int64[]` と `Int64[]` の「値」は等しい。"
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "Int64[] == Int64[]",
"execution_count": 2,
"outputs": [
{
"data": {
"text/plain": "true"
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "n = 3",
"execution_count": 3,
"outputs": [
{
"data": {
"text/plain": "3"
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "`fill(Int64[], 5)` は互いに完全に同一の `Int64[]` で埋められた配列を作る。"
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "@show a = fill(Int64[], n);",
"execution_count": 4,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "a = fill(Int64[], n) = [Int64[], Int64[], Int64[]]\n"
}
]
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "all(a[i] === a[j] for i in 1:n-1 for j in i+1:n)",
"execution_count": 5,
"outputs": [
{
"data": {
"text/plain": "true"
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "`[Int64[] for _ in 1:5]` は互いに異なる `Int64[]` で埋められた配列を作る。"
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "@show b = [Int64[] for _ in 1:n];",
"execution_count": 6,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "b = [Int64[] for _ = 1:n] = [Int64[], Int64[], Int64[]]\n"
}
]
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "all(b[i] !== b[j] for i in 1:n-1 for j in i+1:n)",
"execution_count": 7,
"outputs": [
{
"data": {
"text/plain": "true"
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "push!(a[1], 666)\npush!(b[1], 666)\n@show a b;",
"execution_count": 8,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "a = [[666], [666], [666]]\nb = [[666], Int64[], Int64[]]\n"
}
]
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "push!.(a, 111)\npush!.(b, 111)\n@show a b;",
"execution_count": 9,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "a = [[666, 111, 111, 111], [666, 111, 111, 111], [666, 111, 111, 111]]\nb = [[666, 111], [111], [111]]\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"@webio": {
"lastCommId": null,
"lastKernelId": null
},
"_draft": {
"nbviewer_url": "https://gist.github.com/9a0aa74c051cf9c0590c3cfdcd04ba9c"
},
"gist": {
"id": "9a0aa74c051cf9c0590c3cfdcd04ba9c",
"data": {
"description": "fill and comprehension",
"public": true
}
},
"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
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment