Developed by Piotr Przetacznik
Mentored by José Valim
In [155]:
1 + 2
Out[155]:
In [156]:
5 * 5
Out[156]:
In [157]:
10 / 2
Out[157]:
In [158]:
div(10, 2)
Out[158]:
In [159]:
div 10, 2
Out[159]:
In [160]:
rem 10, 3
Out[160]:
In [161]:
0b1010
Out[161]:
In [162]:
0o777
Out[162]:
In [163]:
0x1F
Out[163]:
In [164]:
1.0
Out[164]:
In [165]:
1.0e-10
Out[165]:
In [166]:
round 3.58
Out[166]:
In [167]:
trunc 3.58
Out[167]:
In [168]:
true
Out[168]:
In [169]:
true == false
Out[169]:
In [170]:
is_boolean(true)
Out[170]:
In [171]:
is_boolean(1)
Out[171]:
In [172]:
is_integer(5)
Out[172]:
In [173]:
is_float(5)
Out[173]:
In [174]:
is_number("5.0")
Out[174]:
In [175]:
:hello
Out[175]:
In [176]:
:hello == :world
Out[176]:
In [177]:
true == :true
Out[177]:
In [178]:
is_atom(false)
Out[178]:
In [179]:
is_boolean(:false)
Out[179]:
In [180]:
"hellö"
Out[180]:
In [181]:
"hellö #{:world}"
Out[181]:
In [182]:
IO.puts "hello\nworld"
Out[182]:
In [183]:
is_binary("hellö")
Out[183]:
In [184]:
byte_size("hellö")
Out[184]:
In [185]:
String.length("hellö")
Out[185]:
In [186]:
String.upcase("hellö")
Out[186]:
In [187]:
add = fn a, b -> a + b end
Out[187]:
In [188]:
is_function(add)
Out[188]:
In [189]:
is_function(add, 2)
Out[189]:
In [190]:
is_function(add, 1)
Out[190]:
In [191]:
add.(1, 2)
Out[191]:
In [192]:
add_two = fn a -> add.(a, 2) end
Out[192]:
In [193]:
add_two.(2)
Out[193]:
In [194]:
x = 42
(fn -> x = 0 end).()
x
Out[194]:
In [195]:
a = [1, 2, true, 3]
Out[195]:
In [196]:
length [1, 2, 3]
Out[196]:
In [197]:
[1, 2, 3] ++ [4, 5, 6]
Out[197]:
In [198]:
[1, true, 2, false, 3, true] -- [true, false]
Out[198]:
In [199]:
hd(a)
Out[199]:
In [200]:
tl(a)
Out[200]:
In [201]:
hd []
In [201]:
[11, 12, 13]
Out[201]:
In [202]:
[104, 101, 108, 108, 111]
Out[202]:
In [203]:
'hello' == "hello"
Out[203]:
In [204]:
{:ok, "hello"}
Out[204]:
In [205]:
tuple_size {:ok, "hello"}
Out[205]:
In [206]:
tuple = {:ok, "hello"}
Out[206]:
In [207]:
elem(tuple, 1)
Out[207]:
In [208]:
tuple_size(tuple)
Out[208]:
In [209]:
put_elem(tuple, 1, "world")
Out[209]:
In [210]:
tuple
Out[210]:
In [211]:
list = [1|[2|[3|[]]]]
Out[211]:
In [212]:
[0] ++ list
Out[212]:
In [213]:
list ++ [4]
Out[213]:
In [214]:
File.read("LICENSE")
Out[214]:
In [215]:
File.read("path/to/unknown/file")
Out[215]:
In [216]:
0x1F
Out[216]:
In [217]:
a = 25
b = 150
IO.puts(a+b)
Out[217]:
In [218]:
defmodule Math do
def sum(a, b) do
a + b
end
end
Out[218]:
In [219]:
Math.sum(1, 2)
Out[219]:
In [220]:
import ExUnit.CaptureIO
capture_io(fn -> IO.write "john" end) == "john"
Out[220]:
In [221]:
?a
Out[221]:
In [222]:
<<98>> == <<?b>>
Out[222]:
In [223]:
<<?g, ?o, ?\n>> == "go
"
Out[223]:
In [224]:
{hlen, blen} = {4, 4}
<<header :: binary-size(hlen), body :: binary-size(blen)>> = "headbody"
{header, body}
Out[224]:
In [225]:
h()
In [226]:
defmodule KV.Registry do
use GenServer
## Client API
@doc """
Starts the registry.
"""
def start_link(opts \\ []) do
GenServer.start_link(__MODULE__, :ok, opts)
end
@doc """
Looks up the bucket pid for `name` stored in `server`.
Returns `{:ok, pid}` if the bucket exists, `:error` otherwise.
"""
def lookup(server, name) do
GenServer.call(server, {:lookup, name})
end
@doc """
Ensures there is a bucket associated to the given `name` in `server`.
"""
def create(server, name) do
GenServer.cast(server, {:create, name})
end
## Server Callbacks
def init(:ok) do
{:ok, HashDict.new}
end
def handle_call({:lookup, name}, _from, names) do
{:reply, HashDict.fetch(names, name), names}
end
def handle_cast({:create, name}, names) do
if HashDict.has_key?(names, name) do
{:noreply, names}
else
{:ok, bucket} = KV.Bucket.start_link()
{:noreply, HashDict.put(names, name, bucket)}
end
end
end
Out[226]:
In [227]:
ExUnit.start()
Out[227]:
In [228]:
defmodule KV.RegistryTest do
use ExUnit.Case, async: true
setup do
{:ok, registry} = KV.Registry.start_link
{:ok, registry: registry}
end
test "spawns buckets", %{registry: registry} do
assert KV.Registry.lookup(registry, "shopping") == :error
KV.Registry.create(registry, "shopping")
assert {:ok, bucket} = KV.Registry.lookup(registry, "shopping")
KV.Bucket.put(bucket, "milk", 1)
assert KV.Bucket.get(bucket, "milk") == 1
end
end
Out[228]:
In [229]:
ans
Out[229]:
You can also access output of any cell using it's number.
In [230]:
out[142]
Out[230]: