Developed by Piotr Przetacznik
Mentored by José Valim
In [1]:
1 + 2
Out[1]:
In [2]:
5 * 5
Out[2]:
In [3]:
10 / 2
Out[3]:
In [4]:
div(10, 2)
Out[4]:
In [5]:
div 10, 2
Out[5]:
In [6]:
rem 10, 3
Out[6]:
In [7]:
0b1010
Out[7]:
In [8]:
0o777
Out[8]:
In [9]:
0x1F
Out[9]:
In [10]:
1.0
Out[10]:
In [11]:
1.0e-10
Out[11]:
In [12]:
round 3.58
Out[12]:
In [13]:
trunc 3.58
Out[13]:
In [14]:
true
Out[14]:
In [15]:
true == false
Out[15]:
In [16]:
is_boolean(true)
Out[16]:
In [17]:
is_boolean(1)
Out[17]:
In [18]:
is_integer(5)
Out[18]:
In [19]:
is_float(5)
Out[19]:
In [20]:
is_number("5.0")
Out[20]:
In [21]:
:hello
Out[21]:
In [22]:
:hello == :world
Out[22]:
In [23]:
true == :true
Out[23]:
In [24]:
is_atom(false)
Out[24]:
In [25]:
is_boolean(:false)
Out[25]:
In [26]:
"hellö"
Out[26]:
In [27]:
"hellö #{:world}"
Out[27]:
In [28]:
IO.puts "hello\nworld"
Out[28]:
In [29]:
is_binary("hellö")
Out[29]:
In [30]:
byte_size("hellö")
Out[30]:
In [31]:
String.length("hellö")
Out[31]:
In [32]:
String.upcase("hellö")
Out[32]:
In [33]:
add = fn a, b -> a + b end
Out[33]:
In [34]:
is_function(add)
Out[34]:
In [35]:
is_function(add, 2)
Out[35]:
In [36]:
is_function(add, 1)
Out[36]:
In [37]:
add.(1, 2)
Out[37]:
In [38]:
add_two = fn a -> add.(a, 2) end
Out[38]:
In [39]:
add_two.(2)
Out[39]:
In [40]:
x = 42
(fn -> x = 0 end).()
x
Out[40]:
In [41]:
a = [1, 2, true, 3]
Out[41]:
In [42]:
length [1, 2, 3]
Out[42]:
In [43]:
[1, 2, 3] ++ [4, 5, 6]
Out[43]:
In [44]:
[1, true, 2, false, 3, true] -- [true, false]
Out[44]:
In [45]:
hd(a)
Out[45]:
In [46]:
tl(a)
Out[46]:
In [47]:
hd []
In [47]:
[11, 12, 13]
Out[47]:
In [48]:
[104, 101, 108, 108, 111]
Out[48]:
In [49]:
'hello' == "hello"
Out[49]:
In [50]:
{:ok, "hello"}
Out[50]:
In [51]:
tuple_size {:ok, "hello"}
Out[51]:
In [52]:
tuple = {:ok, "hello"}
Out[52]:
In [53]:
elem(tuple, 1)
Out[53]:
In [54]:
tuple_size(tuple)
Out[54]:
In [55]:
put_elem(tuple, 1, "world")
Out[55]:
In [56]:
tuple
Out[56]:
In [57]:
list = [1|[2|[3|[]]]]
Out[57]:
In [58]:
[0] ++ list
Out[58]:
In [59]:
list ++ [4]
Out[59]:
In [60]:
File.read("LICENSE")
Out[60]:
In [61]:
File.read("path/to/unknown/file")
Out[61]:
In [62]:
0x1F
Out[62]:
In [63]:
a = 25
b = 150
IO.puts(a+b)
Out[63]:
In [64]:
defmodule Math do
def sum(a, b) do
a + b
end
end
Out[64]:
In [65]:
Math.sum(1, 2)
Out[65]:
In [66]:
import ExUnit.CaptureIO
capture_io(fn -> IO.write "john" end) == "john"
Out[66]:
In [67]:
?a
Out[67]:
In [68]:
<<98>> == <<?b>>
Out[68]:
In [69]:
<<?g, ?o, ?\n>> == "go
"
Out[69]:
In [70]:
{hlen, blen} = {4, 4}
<<header :: binary-size(hlen), body :: binary-size(blen)>> = "headbody"
{header, body}
Out[70]:
In [71]:
h()
In [72]:
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[72]:
In [73]:
ExUnit.start()
Out[73]:
In [74]:
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[74]: