Current section
Files
Jump to
Current section
Files
resources/example.ipynb
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# IElixir - Elixir kernel for Jupyter Project\n",
"\n",
"<img src=\"logo.png\" title=\"Hosted by imgur.com\" style=\"margin: 0 0;\"/>\n",
"\n",
"---\n",
"\n",
"## Google Summer of Code 2015\n",
"> Developed by [Piotr Przetacznik](https://twitter.com/pprzetacznik)\n",
"\n",
"> Mentored by [José Valim](https://twitter.com/josevalim)\n",
"\n",
"---\n",
"## References\n",
"\n",
"* [Elixir language](http://elixir-lang.org/)\n",
"* [Jupyter Project](https://jupyter.org/)\n",
"* [IElixir sources](https://github.com/pprzetacznik/IElixir)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Getting Started\n",
"\n",
"### Basic Types\n",
"\n",
"<pre>\n",
"1 # integer\n",
"0x1F # integer\n",
"1.0 # float\n",
"true # boolean\n",
":atom # atom / symbol\n",
"\"elixir\" # string\n",
"[1, 2, 3] # list\n",
"{1, 2, 3} # tuple\n",
"</pre>\n",
"\n",
"### Basic arithmetic"
]
},
{
"cell_type": "code",
"execution_count": 155,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 155,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1 + 2"
]
},
{
"cell_type": "code",
"execution_count": 156,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"25"
]
},
"execution_count": 156,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"5 * 5"
]
},
{
"cell_type": "code",
"execution_count": 157,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5.0"
]
},
"execution_count": 157,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"10 / 2"
]
},
{
"cell_type": "code",
"execution_count": 158,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 158,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"div(10, 2)"
]
},
{
"cell_type": "code",
"execution_count": 159,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 159,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"div 10, 2"
]
},
{
"cell_type": "code",
"execution_count": 160,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 160,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rem 10, 3"
]
},
{
"cell_type": "code",
"execution_count": 161,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 161,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"0b1010"
]
},
{
"cell_type": "code",
"execution_count": 162,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"511"
]
},
"execution_count": 162,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"0o777"
]
},
{
"cell_type": "code",
"execution_count": 163,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"31"
]
},
"execution_count": 163,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"0x1F"
]
},
{
"cell_type": "code",
"execution_count": 164,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.0"
]
},
"execution_count": 164,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1.0"
]
},
{
"cell_type": "code",
"execution_count": 165,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.0e-10"
]
},
"execution_count": 165,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1.0e-10"
]
},
{
"cell_type": "code",
"execution_count": 166,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 166,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"round 3.58"
]
},
{
"cell_type": "code",
"execution_count": 167,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 167,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"trunc 3.58"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Booleans"
]
},
{
"cell_type": "code",
"execution_count": 168,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 168,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"true"
]
},
{
"cell_type": "code",
"execution_count": 169,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"false"
]
},
"execution_count": 169,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"true == false"
]
},
{
"cell_type": "code",
"execution_count": 170,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 170,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_boolean(true)"
]
},
{
"cell_type": "code",
"execution_count": 171,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"false"
]
},
"execution_count": 171,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_boolean(1)"
]
},
{
"cell_type": "code",
"execution_count": 172,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 172,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_integer(5)"
]
},
{
"cell_type": "code",
"execution_count": 173,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"false"
]
},
"execution_count": 173,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_float(5)"
]
},
{
"cell_type": "code",
"execution_count": 174,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"false"
]
},
"execution_count": 174,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_number(\"5.0\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Atoms"
]
},
{
"cell_type": "code",
"execution_count": 175,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
":hello"
]
},
"execution_count": 175,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
":hello"
]
},
{
"cell_type": "code",
"execution_count": 176,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"false"
]
},
"execution_count": 176,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
":hello == :world"
]
},
{
"cell_type": "code",
"execution_count": 177,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 177,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"true == :true"
]
},
{
"cell_type": "code",
"execution_count": 178,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 178,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_atom(false)"
]
},
{
"cell_type": "code",
"execution_count": 179,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 179,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_boolean(:false)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Strings"
]
},
{
"cell_type": "code",
"execution_count": 180,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"hellö\""
]
},
"execution_count": 180,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"hellö\""
]
},
{
"cell_type": "code",
"execution_count": 181,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"hellö world\""
]
},
"execution_count": 181,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"hellö #{:world}\""
]
},
{
"cell_type": "code",
"execution_count": 182,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello\n",
"world\n"
]
},
{
"data": {
"text/plain": [
":ok"
]
},
"execution_count": 182,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"IO.puts \"hello\\nworld\""
]
},
{
"cell_type": "code",
"execution_count": 183,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 183,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_binary(\"hellö\")"
]
},
{
"cell_type": "code",
"execution_count": 184,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 184,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"byte_size(\"hellö\")"
]
},
{
"cell_type": "code",
"execution_count": 185,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 185,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"String.length(\"hellö\")"
]
},
{
"cell_type": "code",
"execution_count": 186,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"HELLÖ\""
]
},
"execution_count": 186,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"String.upcase(\"hellö\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Anonymous functions"
]
},
{
"cell_type": "code",
"execution_count": 187,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"#Function<12.99386804/2 in :erl_eval.expr/5>"
]
},
"execution_count": 187,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"add = fn a, b -> a + b end"
]
},
{
"cell_type": "code",
"execution_count": 188,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 188,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_function(add)"
]
},
{
"cell_type": "code",
"execution_count": 189,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 189,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_function(add, 2)"
]
},
{
"cell_type": "code",
"execution_count": 190,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"false"
]
},
"execution_count": 190,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_function(add, 1)"
]
},
{
"cell_type": "code",
"execution_count": 191,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 191,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"add.(1, 2)"
]
},
{
"cell_type": "code",
"execution_count": 192,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"#Function<6.99386804/1 in :erl_eval.expr/5>"
]
},
"execution_count": 192,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"add_two = fn a -> add.(a, 2) end"
]
},
{
"cell_type": "code",
"execution_count": 193,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 193,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"add_two.(2)"
]
},
{
"cell_type": "code",
"execution_count": 194,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[33mwarning: \u001b[0mvariable \"x\" is unused\n",
"\n",
"Note variables defined inside case, cond, fn, if and similar do not leak. If you want to conditionally override an existing variable \"x\", you will have to explicitly return the variable. For example:\n",
"\n",
" if some_condition? do\n",
" atom = :one\n",
" else\n",
" atom = :two\n",
" end\n",
"\n",
"should be written as\n",
"\n",
" atom =\n",
" if some_condition? do\n",
" :one\n",
" else\n",
" :two\n",
" end\n",
"\n",
"Unused variable found at:\n",
" nofile:2\n",
"\n"
]
},
{
"data": {
"text/plain": [
"42"
]
},
"execution_count": 194,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = 42\n",
"(fn -> x = 0 end).()\n",
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### (Linked) Lists"
]
},
{
"cell_type": "code",
"execution_count": 195,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, true, 3]"
]
},
"execution_count": 195,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = [1, 2, true, 3]"
]
},
{
"cell_type": "code",
"execution_count": 196,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 196,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"length [1, 2, 3]"
]
},
{
"cell_type": "code",
"execution_count": 197,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 4, 5, 6]"
]
},
"execution_count": 197,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[1, 2, 3] ++ [4, 5, 6]"
]
},
{
"cell_type": "code",
"execution_count": 198,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, true]"
]
},
"execution_count": 198,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[1, true, 2, false, 3, true] -- [true, false]"
]
},
{
"cell_type": "code",
"execution_count": 199,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 199,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"hd(a)"
]
},
{
"cell_type": "code",
"execution_count": 200,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, true, 3]"
]
},
"execution_count": 200,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tl(a)"
]
},
{
"cell_type": "code",
"execution_count": 201,
"metadata": {},
"outputs": [
{
"ename": "ArgumentError",
"evalue": "1",
"output_type": "error",
"traceback": [
"** (ArgumentError) \"argument error\""
]
}
],
"source": [
"hd []"
]
},
{
"cell_type": "code",
"execution_count": 201,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'\\v\\f\\r'"
]
},
"execution_count": 201,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[11, 12, 13]"
]
},
{
"cell_type": "code",
"execution_count": 202,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'hello'"
]
},
"execution_count": 202,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[104, 101, 108, 108, 111]"
]
},
{
"cell_type": "code",
"execution_count": 203,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"false"
]
},
"execution_count": 203,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'hello' == \"hello\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Tuples"
]
},
{
"cell_type": "code",
"execution_count": 204,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{:ok, \"hello\"}"
]
},
"execution_count": 204,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"{:ok, \"hello\"}"
]
},
{
"cell_type": "code",
"execution_count": 205,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 205,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tuple_size {:ok, \"hello\"}"
]
},
{
"cell_type": "code",
"execution_count": 206,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{:ok, \"hello\"}"
]
},
"execution_count": 206,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tuple = {:ok, \"hello\"}"
]
},
{
"cell_type": "code",
"execution_count": 207,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"hello\""
]
},
"execution_count": 207,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"elem(tuple, 1)"
]
},
{
"cell_type": "code",
"execution_count": 208,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 208,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tuple_size(tuple)"
]
},
{
"cell_type": "code",
"execution_count": 209,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{:ok, \"world\"}"
]
},
"execution_count": 209,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"put_elem(tuple, 1, \"world\")"
]
},
{
"cell_type": "code",
"execution_count": 210,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{:ok, \"hello\"}"
]
},
"execution_count": 210,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tuple"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Lists or tuples?"
]
},
{
"cell_type": "code",
"execution_count": 211,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3]"
]
},
"execution_count": 211,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list = [1|[2|[3|[]]]]"
]
},
{
"cell_type": "code",
"execution_count": 212,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 1, 2, 3]"
]
},
"execution_count": 212,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[0] ++ list"
]
},
{
"cell_type": "code",
"execution_count": 213,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 4]"
]
},
"execution_count": 213,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list ++ [4]"
]
},
{
"cell_type": "code",
"execution_count": 214,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{:ok, \"\\n Apache License\\n Version 2.0, January 2004\\n http://www.apache.org/licenses/\\n\\n TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION\\n\\n 1. Definitions.\\n\\n \\\"License\\\" shall mean the terms and conditions for use, reproduction,\\n and distribution as defined by Sections 1 through 9 of this document.\\n\\n \\\"Licensor\\\" shall mean the copyright owner or entity authorized by\\n the copyright owner that is granting the License.\\n\\n \\\"Legal Entity\\\" shall mean the union of the acting entity and all\\n other entities that control, are controlled by, or are under common\\n control with that entity. For the purposes of this definition,\\n \\\"control\\\" means (i) the power, direct or indirect, to cause the\\n direction or management of such entity, whether by contract or\\n otherwise, or (ii) ownership of fifty percent (50%) or more of the\\n outstanding shares, or (iii) beneficial ownership of such entity.\\n\\n \\\"You\\\" (or \\\"Your\\\") shall mean an individual or Legal Entity\\n exercising permissions granted by this License.\\n\\n \\\"Source\\\" form shall mean the preferred form for making modifications,\\n including but not limited to software source code, documentation\\n source, and configuration files.\\n\\n \\\"Object\\\" form shall mean any form resulting from mechanical\\n transformation or translation of a Source form, including but\\n not limited to compiled object code, generated documentation,\\n and conversions to other media types.\\n\\n \\\"Work\\\" shall mean the work of authorship, whether in Source or\\n Object form, made available under the License, as indicated by a\\n copyright notice that is included in or attached to the work\\n (an example is provided in the Appendix below).\\n\\n \\\"Derivative Works\\\" shall mean any work, whether in Source or Object\\n form, that is based on (or derived from) the Work and for which the\\n editorial revisions, annotations, elaborations, or other modifications\\n represent, as a whole, an original work of authorship. For the purposes\\n of this License, Derivative Works shall not include works that remain\\n separable from, or merely link (or bind by name) to the interfaces of,\\n the Work and Derivative Works thereof.\\n\\n \\\"Contribution\\\" shall mean any work of authorship, including\\n the original version of the Work and any modifications or additions\\n to that Work or Derivative Works thereof, that is intentionally\\n submitted to Licensor for inclusion in the Work by the copyright owner\\n or by an individual or Legal Entity authorized to submit on behalf of\\n the copyright owner. For the purposes of this definition, \\\"submitted\\\"\\n means any form of electronic, verbal, or written communication sent\\n to the Licensor or its representatives, including but not limited to\\n communication on electronic mailing lists, source code control systems,\\n and issue tracking systems that are managed by, or on behalf of, the\\n Licensor for the purpose of discussing and improving the Work, but\\n excluding communication that is conspicuously marked or otherwise\\n designated in writing by the copyright owner as \\\"Not a Contribution.\\\"\\n\\n \\\"Contributor\\\" shall mean Licensor and any individual or Legal Entity\\n on behalf of whom a Contribution has been received by Licensor and\\n subsequently incorporated within the Work.\\n\\n 2. Grant of Copyright License. Subject to the terms and conditions of\\n this License, each Contributor hereby grants to You a perpetual,\\n worldwide, non-exclusive, no-charge, royalty-free, irrevocable\\n copyright license to reproduce, prepare Derivative Works of,\\n publicly display, publicly perform, sublicense, and distribute the\\n Work and such Derivative Works in Source or Object form.\\n\\n 3. Grant of Patent License. Subject to the terms and conditions of\\n this License, each Contributor hereby grants to You a perpetual,\\n worldwide, non-exclusive, no-\" <> ...}"
]
},
"execution_count": 214,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"File.read(\"LICENSE\")"
]
},
{
"cell_type": "code",
"execution_count": 215,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{:error, :enoent}"
]
},
"execution_count": 215,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"File.read(\"path/to/unknown/file\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Other examples"
]
},
{
"cell_type": "code",
"execution_count": 216,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"31"
]
},
"execution_count": 216,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"0x1F"
]
},
{
"cell_type": "code",
"execution_count": 217,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"175\n"
]
},
{
"data": {
"text/plain": [
":ok"
]
},
"execution_count": 217,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = 25\n",
"b = 150\n",
"IO.puts(a+b)"
]
},
{
"cell_type": "code",
"execution_count": 218,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[33mwarning: \u001b[0mredefining module Math (current version defined in memory)\n",
" nofile:1\n",
"\n"
]
},
{
"data": {
"text/plain": [
"{:module, Math, <<70, 79, 82, 49, 0, 0, 4, 20, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 124, 0, 0, 0, 14, 11, 69, 108, 105, 120, 105, 114, 46, 77, 97, 116, 104, 8, 95, 95, 105, 110, 102, 111, 95, 95, 7, 99, ...>>, {:sum, 2}}"
]
},
"execution_count": 218,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"defmodule Math do\n",
" def sum(a, b) do\n",
" a + b\n",
" end\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 219,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 219,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Math.sum(1, 2)"
]
},
{
"cell_type": "code",
"execution_count": 220,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 220,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import ExUnit.CaptureIO\n",
"capture_io(fn -> IO.write \"john\" end) == \"john\""
]
},
{
"cell_type": "code",
"execution_count": 221,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"97"
]
},
"execution_count": 221,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"?a"
]
},
{
"cell_type": "code",
"execution_count": 222,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 222,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"<<98>> == <<?b>>"
]
},
{
"cell_type": "code",
"execution_count": 223,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 223,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"<<?g, ?o, ?\\n>> == \"go\n",
"\""
]
},
{
"cell_type": "code",
"execution_count": 224,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{\"head\", \"body\"}"
]
},
"execution_count": 224,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"{hlen, blen} = {4, 4}\n",
"<<header :: binary-size(hlen), body :: binary-size(blen)>> = \"headbody\"\n",
"{header, body}"
]
},
{
"cell_type": "code",
"execution_count": 225,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[0m\n",
"\u001b[7m\u001b[33m IEx.Helpers \u001b[0m\n",
"\u001b[0m\n",
"Welcome to Interactive Elixir. You are currently seeing the documentation for\n",
"the module \u001b[36mIEx.Helpers\u001b[0m which provides many helpers to make Elixir's shell more\n",
"joyful to work with.\n",
"\u001b[0m\n",
"This message was triggered by invoking the helper \u001b[36mh()\u001b[0m, usually referred to as\n",
"\u001b[36mh/0\u001b[0m (since it expects 0 arguments).\n",
"\u001b[0m\n",
"You can use the \u001b[36mh/1\u001b[0m function to invoke the documentation for any Elixir module\n",
"or function:\n",
"\u001b[0m\n",
"\u001b[36m iex> h(Enum)\n",
" iex> h(Enum.map)\n",
" iex> h(Enum.reverse/1)\u001b[0m\n",
"\u001b[0m\n",
"You can also use the \u001b[36mi/1\u001b[0m function to introspect any value you have in the\n",
"shell:\n",
"\u001b[0m\n",
"\u001b[36m iex> i(\"hello\")\u001b[0m\n",
"\u001b[0m\n",
"There are many other helpers available, here are some examples:\n",
"\u001b[0m\n",
" • \u001b[36mb/1\u001b[0m - prints callbacks info and docs for a given module\n",
" • \u001b[36mc/1\u001b[0m - compiles a file\n",
" • \u001b[36mc/2\u001b[0m - compiles a file and writes bytecode to the given path\n",
" • \u001b[36mcd/1\u001b[0m - changes the current directory\n",
" • \u001b[36mclear/0\u001b[0m - clears the screen\n",
" • \u001b[36mexports/1\u001b[0m - shows all exports (functions + macros) in a module\n",
" • \u001b[36mflush/0\u001b[0m - flushes all messages sent to the shell\n",
" • \u001b[36mh/0\u001b[0m - prints this help message\n",
" • \u001b[36mh/1\u001b[0m - prints help for the given module, function or macro\n",
" • \u001b[36mi/0\u001b[0m - prints information about the last value\n",
" • \u001b[36mi/1\u001b[0m - prints information about the given term\n",
" • \u001b[36mls/0\u001b[0m - lists the contents of the current directory\n",
" • \u001b[36mls/1\u001b[0m - lists the contents of the specified directory\n",
" • \u001b[36mopen/1\u001b[0m - opens the source for the given module or function in\n",
" your editor\n",
" • \u001b[36mpid/1\u001b[0m - creates a PID from a string\n",
" • \u001b[36mpid/3\u001b[0m - creates a PID with the 3 integer arguments passed\n",
" • \u001b[36mref/1\u001b[0m - creates a Reference from a string\n",
" • \u001b[36mref/4\u001b[0m - creates a Reference with the 4 integer arguments\n",
" passed\n",
" • \u001b[36mpwd/0\u001b[0m - prints the current working directory\n",
" • \u001b[36mr/1\u001b[0m - recompiles the given module's source file\n",
" • \u001b[36mrecompile/0\u001b[0m - recompiles the current project\n",
" • \u001b[36mruntime_info/0\u001b[0m - prints runtime info (versions, memory usage, stats)\n",
" • \u001b[36mv/0\u001b[0m - retrieves the last value from the history\n",
" • \u001b[36mv/1\u001b[0m - retrieves the nth value from the history\n",
"\u001b[0m\n",
"Help for all of those functions can be consulted directly from the command line\n",
"using the \u001b[36mh/1\u001b[0m helper itself. Try:\n",
"\u001b[0m\n",
"\u001b[36m iex> h(v/0)\u001b[0m\n",
"\u001b[0m\n",
"To list all IEx helpers available, which is effectively all exports (functions\n",
"and macros) in the \u001b[36mIEx.Helpers\u001b[0m module:\n",
"\u001b[0m\n",
"\u001b[36m iex> exports(IEx.Helpers)\u001b[0m\n",
"\u001b[0m\n",
"This module also includes helpers for debugging purposes, see \u001b[36mIEx.break!/4\u001b[0m for\n",
"more information.\n",
"\u001b[0m\n",
"To learn more about IEx as a whole, type \u001b[36mh(IEx)\u001b[0m.\n",
"\u001b[0m\n"
]
}
],
"source": [
"h()"
]
},
{
"cell_type": "code",
"execution_count": 226,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[33mwarning: \u001b[0mredefining module KV.Registry (current version defined in memory)\n",
" nofile:1\n",
"\n",
"\u001b[33mwarning: \u001b[0mHashDict.new/0 is deprecated. Use maps and the Map module instead\n",
" nofile:32\n",
"\n",
"\u001b[33mwarning: \u001b[0mHashDict.fetch/2 is deprecated. Use maps and the Map module instead\n",
" nofile:36\n",
"\n",
"\u001b[33mwarning: \u001b[0mHashDict.has_key?/2 is deprecated. Use maps and the Map module instead\n",
" nofile:40\n",
"\n",
"\u001b[33mwarning: \u001b[0mHashDict.put/3 is deprecated. Use maps and the Map module instead\n",
" nofile:44\n",
"\n"
]
},
{
"data": {
"text/plain": [
"{:module, KV.Registry, <<70, 79, 82, 49, 0, 0, 15, 96, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 1, 166, 0, 0, 0, 44, 18, 69, 108, 105, 120, 105, 114, 46, 75, 86, 46, 82, 101, 103, 105, 115, 116, 114, 121, 8, 95, 95, 105, ...>>, {:handle_cast, 2}}"
]
},
"execution_count": 226,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"defmodule KV.Registry do\n",
" use GenServer\n",
"\n",
" ## Client API\n",
"\n",
" @doc \"\"\"\n",
" Starts the registry.\n",
" \"\"\"\n",
" def start_link(opts \\\\ []) do\n",
" GenServer.start_link(__MODULE__, :ok, opts)\n",
" end\n",
"\n",
" @doc \"\"\"\n",
" Looks up the bucket pid for `name` stored in `server`.\n",
"\n",
" Returns `{:ok, pid}` if the bucket exists, `:error` otherwise.\n",
" \"\"\"\n",
" def lookup(server, name) do\n",
" GenServer.call(server, {:lookup, name})\n",
" end\n",
"\n",
" @doc \"\"\"\n",
" Ensures there is a bucket associated to the given `name` in `server`.\n",
" \"\"\"\n",
" def create(server, name) do\n",
" GenServer.cast(server, {:create, name})\n",
" end\n",
"\n",
" ## Server Callbacks\n",
"\n",
" def init(:ok) do\n",
" {:ok, HashDict.new}\n",
" end\n",
"\n",
" def handle_call({:lookup, name}, _from, names) do\n",
" {:reply, HashDict.fetch(names, name), names}\n",
" end\n",
"\n",
" def handle_cast({:create, name}, names) do\n",
" if HashDict.has_key?(names, name) do\n",
" {:noreply, names}\n",
" else\n",
" {:ok, bucket} = KV.Bucket.start_link()\n",
" {:noreply, HashDict.put(names, name, bucket)}\n",
" end\n",
" end\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 227,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"nil"
]
},
"execution_count": 227,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ExUnit.start()"
]
},
{
"cell_type": "code",
"execution_count": 228,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[33mwarning: \u001b[0mredefining module KV.RegistryTest (current version defined in memory)\n",
" nofile:1\n",
"\n"
]
},
{
"data": {
"text/plain": [
"{:module, KV.RegistryTest, <<70, 79, 82, 49, 0, 0, 15, 88, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 1, 232, 0, 0, 0, 47, 22, 69, 108, 105, 120, 105, 114, 46, 75, 86, 46, 82, 101, 103, 105, 115, 116, 114, 121, 84, 101, 115, 116, ...>>, {:\"test spawns buckets\", 1}}"
]
},
"execution_count": 228,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"defmodule KV.RegistryTest do\n",
" use ExUnit.Case, async: true\n",
"\n",
" setup do\n",
" {:ok, registry} = KV.Registry.start_link\n",
" {:ok, registry: registry}\n",
" end\n",
"\n",
" test \"spawns buckets\", %{registry: registry} do\n",
" assert KV.Registry.lookup(registry, \"shopping\") == :error\n",
"\n",
" KV.Registry.create(registry, \"shopping\")\n",
" assert {:ok, bucket} = KV.Registry.lookup(registry, \"shopping\")\n",
"\n",
" KV.Bucket.put(bucket, \"milk\", 1)\n",
" assert KV.Bucket.get(bucket, \"milk\") == 1\n",
" end\n",
"end"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## IElixir magic commands\n",
"\n",
"Get output of previous cell."
]
},
{
"cell_type": "code",
"execution_count": 229,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{:module, KV.RegistryTest, <<70, 79, 82, 49, 0, 0, 15, 88, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 1, 232, 0, 0, 0, 47, 22, 69, 108, 105, 120, 105, 114, 46, 75, 86, 46, 82, 101, 103, 105, 115, 116, 114, 121, 84, 101, 115, 116, ...>>, {:\"test spawns buckets\", 1}}"
]
},
"execution_count": 229,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ans"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also access output of any cell using it's number."
]
},
{
"cell_type": "code",
"execution_count": 230,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"97"
]
},
"execution_count": 230,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"out[142]"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Elixir",
"language": "Elixir",
"name": "ielixir"
},
"language_info": {
"codemirror_mode": "elixir",
"file_extension": "ex",
"mimetype": "text/x-elixir",
"name": "elixir",
"nbconvert_exporter": "",
"pygments_lexer": "elixir",
"version": "1.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 1
}