Skip to content

Instantly share code, notes, and snippets.

@genkuroki
Last active May 29, 2021 13:46
Show Gist options
  • Save genkuroki/4e8ea0fe9478044808c31c2140b5f4ca to your computer and use it in GitHub Desktop.
Save genkuroki/4e8ea0fe9478044808c31c2140b5f4ca to your computer and use it in GitHub Desktop.
fn_generator
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "VERSION",
"execution_count": 1,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 1,
"data": {
"text/plain": "v\"1.7.0-DEV.1129\""
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "* https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/15?u=genkuroki\n* https://github.com/JuliaLang/julia/issues/15602"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "function fn_generator()\n if true\n f() = 0\n else\n f(x, y) = x+y\n end\nend\n\nname = fn_generator()\n\nname(1,2) # 3",
"execution_count": 2,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 2,
"data": {
"text/plain": "3"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "name === fn_generator()",
"execution_count": 3,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 3,
"data": {
"text/plain": "true"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "name === fn_generator()",
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 4,
"data": {
"text/plain": "true"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "dump(name)",
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"text": "f (function of type var\"#f#1\")\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "methods(name)",
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 6,
"data": {
"text/plain": "# 2 methods:\n[1] (::var\"#f#1\")() in Main at In[2]:3\n[2] (::var\"#f#1\")(x, y) in Main at In[2]:5",
"text/html": "# 2 methods:<ul><li> (::<b>var\"#f#1\"</b>)() in Main at In[2]:3</li> <li> (::<b>var\"#f#1\"</b>)(x, y) in Main at In[2]:5</li> </ul>"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "name()",
"execution_count": 7,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 7,
"data": {
"text/plain": "0"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "function make_func()\n if true\n f() = 1\n elseif true\n f(x) = 2x\n elseif true\n f(x, y) = x * y\n elseif true\n f(x, y, z) = x * y + z\n else\n f(x, y, z, w) = x * y + z * w\n end\nend\n\nf = make_func()\n\n@show f() f(3) f(3, 4) f(3, 4, 5) f(3, 4, 5, 6);",
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"text": "f() = 1\nf(3) = 6\nf(3, 4) = 12\nf(3, 4, 5) = 17\nf(3, 4, 5, 6) = 42\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "methods(f)",
"execution_count": 9,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 9,
"data": {
"text/plain": "# 5 methods for generic function \"f\":\n[1] f() in Main at In[8]:3\n[2] f(x) in Main at In[8]:5\n[3] f(x, y) in Main at In[8]:7\n[4] f(x, y, z) in Main at In[8]:9\n[5] f(x, y, z, w) in Main at In[8]:11",
"text/html": "# 5 methods for generic function <b>f</b>:<ul><li> f() in Main at In[8]:3</li> <li> f(x) in Main at In[8]:5</li> <li> f(x, y) in Main at In[8]:7</li> <li> f(x, y, z) in Main at In[8]:9</li> <li> f(x, y, z, w) in Main at In[8]:11</li> </ul>"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "const a=true\nfunction blah()\n if a==true\n println(\"here\")\n f_() = 1\n return f_\n elseif a==false\n println(\"there\")\n f_() = 2\n return f_\n end\nend\nblah()()",
"execution_count": 10,
"outputs": [
{
"output_type": "stream",
"text": "here\n",
"name": "stdout"
},
{
"output_type": "stream",
"text": "WARNING: Method definition f_() in module Main at In[10]:5 overwritten at In[10]:9.\n",
"name": "stderr"
},
{
"output_type": "execute_result",
"execution_count": 10,
"data": {
"text/plain": "2"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "function fn_generator2()\n if false\n f(x, y) = x+y\n else\n f() = 0\n end\nend\n\ng = fn_generator2()",
"execution_count": 11,
"outputs": [
{
"output_type": "error",
"ename": "LoadError",
"evalue": "UndefVarError: f not defined",
"traceback": [
"UndefVarError: f not defined",
"",
"Stacktrace:",
" [1] fn_generator2()",
" @ Main .\\In[11]:5",
" [2] top-level scope",
" @ In[11]:9",
" [3] eval",
" @ .\\boot.jl:369 [inlined]",
" [4] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)",
" @ Base .\\loading.jl:1110"
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"@webio": {
"lastKernelId": null,
"lastCommId": null
},
"_draft": {
"nbviewer_url": "https://gist.github.com/4e8ea0fe9478044808c31c2140b5f4ca"
},
"gist": {
"id": "4e8ea0fe9478044808c31c2140b5f4ca",
"data": {
"description": "fn_generator",
"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