Skip to content

Instantly share code, notes, and snippets.

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 genkuroki/b2c2c096e241c6ded17f12f93989a8ab to your computer and use it in GitHub Desktop.
Save genkuroki/b2c2c096e241c6ded17f12f93989a8ab to your computer and use it in GitHub Desktop.
functions modifying their arguments
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "https://docs.julialang.org/en/v1/manual/style-guide/index.html#bang-convention-1"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = [1, 2, 3]",
"execution_count": 1,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 1,
"data": {
"text/plain": "3-element Array{Int64,1}:\n 1\n 2\n 3"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "function double_bugged!(a)\n a = 2a\nend\n\n@show a = [1, 2, 3]\ndouble_bugged!(a)\n@show a;",
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"text": "a = [1, 2, 3] = [1, 2, 3]\na = [1, 2, 3]\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "function double_by_for!(a)\n for i in eachindex(a)\n a[i] = 2a[i]\n end\nend\n\n@show a = [1, 2, 3]\ndouble_by_for!(a)\n@show a;",
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": "a = [1, 2, 3] = [1, 2, 3]\na = [2, 4, 6]\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "function double_by_dot!(a)\n a .= 2 .* a\nend\n\n@show a = [1, 2, 3]\ndouble_by_dot!(a)\n@show a;",
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"text": "a = [1, 2, 3] = [1, 2, 3]\na = [2, 4, 6]\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "function double_bugged_by_dot!(a)\n a = 2 .* a\nend\n\n@show a = [1, 2, 3]\ndouble_bugged_by_dot!(a)\n@show a;",
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"text": "a = [1, 2, 3] = [1, 2, 3]\na = [1, 2, 3]\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "function double_by_dot!(a)\n @. a = 2a\nend\n\n@show a = [1, 2, 3]\ndouble_by_dot!(a)\n@show a;",
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"text": "a = [1, 2, 3] = [1, 2, 3]\na = [2, 4, 6]\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "double!(a) = @. a = 2a\n\n@show a = [1, 2, 3]\ndouble!(a)\n@show a;",
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"text": "a = [1, 2, 3] = [1, 2, 3]\na = [2, 4, 6]\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "mutable struct Foo{Ta}\n a::Ta\nend\n\nfunction double!(x::Foo)\n x.a = 2x.a\nend\n\n@show x = Foo(123)\ndouble!(x)\n@show x;",
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"text": "x = Foo(123) = Foo{Int64}(123)\nx = Foo{Int64}(246)\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "julia-1.4",
"display_name": "Julia 1.4.2",
"language": "julia"
},
"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
},
"language_info": {
"file_extension": ".jl",
"name": "julia",
"mimetype": "application/julia",
"version": "1.4.2"
},
"@webio": {
"lastKernelId": null,
"lastCommId": null
},
"gist": {
"id": "b2c2c096e241c6ded17f12f93989a8ab",
"data": {
"description": "functions modifying their arguments",
"public": true
}
},
"_draft": {
"nbviewer_url": "https://gist.github.com/b2c2c096e241c6ded17f12f93989a8ab"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment