Skip to content

Instantly share code, notes, and snippets.

@genkuroki
Last active May 13, 2021 23:18
Show Gist options
  • Save genkuroki/a291c163a3361be1a2c9796ae8c9e5b0 to your computer and use it in GitHub Desktop.
Save genkuroki/a291c163a3361be1a2c9796ae8c9e5b0 to your computer and use it in GitHub Desktop.
Chain.jl example
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "https://twitter.com/_dsudo/status/1392823818494693376"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "https://github.com/jkrumbiegel/Chain.jl"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "using Chain",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "@chain begin\n (1, 2, 3)\n ((a, b, c) = _; a + b + c)\n println\nend",
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"text": "6\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "@chain begin\n (1, 2, 3)\n +(_...)\n println\nend",
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": "6\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "@chain begin\n (1, 2, 3)\n sum\n println\nend",
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"text": "6\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": ":((1, 2, 3) |>\n(((a, b, c),) -> a + b + c) |>\nprintln) |> Base.remove_linenums!",
"execution_count": 5,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 5,
"data": {
"text/plain": ":(((1, 2, 3) |> (((a, b, c),)->begin\n a + b + c\n end)) |> println)"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "(1, 2, 3) |>\n(((a, b, c),) -> a + b + c) |>\nprintln",
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"text": "6\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "f((a, b, c)) = a + b + c\nt = (1, 2, 3)\nf(t)",
"execution_count": 7,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 7,
"data": {
"text/plain": "6"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "t = (1, 2, 3)\n(((a, b, c),) -> a + b + c)(t)",
"execution_count": 8,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 8,
"data": {
"text/plain": "6"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "@show VERSION\n(a = 0, b = 0, c = 0, x = 1, y = 2, z = 3) |>\n(((; x, y, z),) -> muladd(x, y, z)) |>\nprintln",
"execution_count": 9,
"outputs": [
{
"output_type": "stream",
"text": "VERSION = v\"1.7.0-DEV.1049\"\n5\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "@show VERSION\ng((; x, y, z)) = muladd(x, y, z)\nu = (a = 0, b = 0, c = 0, x = 1, y = 2, z = 3)\ng(u)",
"execution_count": 10,
"outputs": [
{
"output_type": "stream",
"text": "VERSION = v\"1.7.0-DEV.1049\"\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 10,
"data": {
"text/plain": "5"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "@show VERSION\nu = (a = 0, b = 0, c = 0, x = 1, y = 2, z = 3)\n(((; x, y, z),) -> muladd(x, y, z))(u)",
"execution_count": 11,
"outputs": [
{
"output_type": "stream",
"text": "VERSION = v\"1.7.0-DEV.1049\"\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 11,
"data": {
"text/plain": "5"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "Base.@kwdef struct Foo{A, B, C, X, Y, Z}\n a::A = 0\n b::B = 0\n c::C = 0\n x::X = 1\n y::Y = 2\n z::Z = 3\nend\n\nfoo = Foo()",
"execution_count": 12,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 12,
"data": {
"text/plain": "Foo{Int64, Int64, Int64, Int64, Int64, Int64}(0, 0, 0, 1, 2, 3)"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "dump(foo)",
"execution_count": 13,
"outputs": [
{
"output_type": "stream",
"text": "Foo{Int64, Int64, Int64, Int64, Int64, Int64}\n a: Int64 0\n b: Int64 0\n c: Int64 0\n x: Int64 1\n y: Int64 2\n z: Int64 3\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "@show VERSION\nh((; x, y, z)) = @show x y z muladd(x, y, z)\nh(foo)",
"execution_count": 14,
"outputs": [
{
"output_type": "stream",
"text": "VERSION = v\"1.7.0-DEV.1049\"\nx = 1\ny = 2\nz = 3\nmuladd(x, y, z) = 5\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 14,
"data": {
"text/plain": "5"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "@show VERSION\n(((; x, y, z),) -> muladd(x, y, z))(foo)",
"execution_count": 15,
"outputs": [
{
"output_type": "stream",
"text": "VERSION = v\"1.7.0-DEV.1049\"\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 15,
"data": {
"text/plain": "5"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"@webio": {
"lastKernelId": null,
"lastCommId": null
},
"_draft": {
"nbviewer_url": "https://gist.github.com/a291c163a3361be1a2c9796ae8c9e5b0"
},
"gist": {
"id": "a291c163a3361be1a2c9796ae8c9e5b0",
"data": {
"description": "Chain.jl example",
"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