lecture 04
This commit is contained in:
22 files changed
+428
No files matched your search
@@ -0,0 +1,4 @@
|
|||||||
|
# Used by "mix format"
|
||||||
|
[
|
||||||
|
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
|
||||||
|
]
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
# The directory Mix will write compiled artifacts to.
|
||||||
|
/_build/
|
||||||
|
|
||||||
|
# If you run "mix test --cover", coverage assets end up here.
|
||||||
|
/cover/
|
||||||
|
|
||||||
|
# The directory Mix downloads your dependencies sources to.
|
||||||
|
/deps/
|
||||||
|
|
||||||
|
# Where third-party dependencies like ExDoc output generated docs.
|
||||||
|
/doc/
|
||||||
|
|
||||||
|
# Ignore .fetch files in case you like to edit your project deps locally.
|
||||||
|
/.fetch
|
||||||
|
|
||||||
|
# If the VM crashes, it generates a dump, let's ignore it too.
|
||||||
|
erl_crash.dump
|
||||||
|
|
||||||
|
# Also ignore archive artifacts (built via "mix archive.build").
|
||||||
|
*.ez
|
||||||
|
|
||||||
|
# Ignore package tarball (built via "mix hex.build").
|
||||||
|
counter-*.tar
|
||||||
|
|
||||||
|
# Temporary files, for example, from tests.
|
||||||
|
/tmp/
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
# Counter
|
||||||
|
|
||||||
|
**TODO: Add description**
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
|
||||||
|
by adding `counter` to your list of dependencies in `mix.exs`:
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
def deps do
|
||||||
|
[
|
||||||
|
{:counter, "~> 0.1.0"}
|
||||||
|
]
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
|
||||||
|
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
|
||||||
|
be found at <https://hexdocs.pm/counter>.
|
||||||
|
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
ƒa
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
defmodule Counter do
|
||||||
|
@moduledoc """
|
||||||
|
Documentation for `Counter`.
|
||||||
|
"""
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Hello world.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
iex> Counter.hello()
|
||||||
|
:world
|
||||||
|
|
||||||
|
"""
|
||||||
|
def hello do
|
||||||
|
:world
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
defmodule Counter.Application do
|
||||||
|
# See https://hexdocs.pm/elixir/Application.html
|
||||||
|
# for more information on OTP Applications
|
||||||
|
@moduledoc false
|
||||||
|
|
||||||
|
use Application
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def start(_type, _args) do
|
||||||
|
children = [
|
||||||
|
# Starts a worker by calling: Counter.Worker.start_link(arg)
|
||||||
|
{Counter.Store, "counter.db"}, # we must start the store before the worker
|
||||||
|
{Counter.Worker, 0}
|
||||||
|
]
|
||||||
|
|
||||||
|
# See https://hexdocs.pm/elixir/Supervisor.html
|
||||||
|
# for other strategies and supported options
|
||||||
|
opts = [strategy: :one_for_one, name: Counter.Supervisor]
|
||||||
|
Supervisor.start_link(children, opts)
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
defmodule Counter.Store do
|
||||||
|
use GenServer
|
||||||
|
|
||||||
|
# client
|
||||||
|
def start_link(file) do
|
||||||
|
GenServer.start_link(__MODULE__, file, name: __MODULE__)
|
||||||
|
end
|
||||||
|
|
||||||
|
def put(value) do
|
||||||
|
GenServer.cast(__MODULE__, {:put, value})
|
||||||
|
end
|
||||||
|
|
||||||
|
def get() do
|
||||||
|
GenServer.call(__MODULE__, :get)
|
||||||
|
end
|
||||||
|
|
||||||
|
# server
|
||||||
|
@impl true
|
||||||
|
def init(file) do
|
||||||
|
{:ok, file}
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def handle_call(:get, _from, file) do
|
||||||
|
reply =
|
||||||
|
case File.read(file) do
|
||||||
|
{:ok, content} -> :erlang.binary_to_term(content)
|
||||||
|
_ -> nil
|
||||||
|
end
|
||||||
|
{:reply, reply, file}
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def handle_cast({:put, value}, file) do
|
||||||
|
File.write(file, :erlang.term_to_binary(value))
|
||||||
|
{:noreply, file}
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
defmodule Counter.Worker do
|
||||||
|
use GenServer
|
||||||
|
|
||||||
|
@store Counter.Store
|
||||||
|
|
||||||
|
# client
|
||||||
|
def start_link(n \\ 0) do
|
||||||
|
GenServer.start_link(__MODULE__, n, name: __MODULE__)
|
||||||
|
end
|
||||||
|
|
||||||
|
def inc(amt \\ 1) do
|
||||||
|
GenServer.cast(__MODULE__, {:inc, amt})
|
||||||
|
end
|
||||||
|
|
||||||
|
def value() do
|
||||||
|
GenServer.call(__MODULE__, :value)
|
||||||
|
end
|
||||||
|
|
||||||
|
# server
|
||||||
|
@impl true
|
||||||
|
def init(arg) do
|
||||||
|
{:ok, @store.get() || arg}
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def handle_call(:value, _from, state) do
|
||||||
|
{:reply, state, state}
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def handle_cast({:inc, amt}, state) do
|
||||||
|
{:noreply, state + amt}
|
||||||
|
end
|
||||||
|
|
||||||
|
# for demonstration only
|
||||||
|
@impl true
|
||||||
|
def handle_info(:reset, _state) do
|
||||||
|
{:noreply, 0}
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def terminate(_reason, state) do
|
||||||
|
@store.put(state)
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
defmodule Counter.MixProject do
|
||||||
|
use Mix.Project
|
||||||
|
|
||||||
|
def project do
|
||||||
|
[
|
||||||
|
app: :counter,
|
||||||
|
version: "0.1.0",
|
||||||
|
elixir: "~> 1.17",
|
||||||
|
start_permanent: Mix.env() == :prod,
|
||||||
|
deps: deps()
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
|
# Run "mix help compile.app" to learn about applications.
|
||||||
|
def application do
|
||||||
|
[
|
||||||
|
extra_applications: [:logger],
|
||||||
|
mod: {Counter.Application, []}
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
|
# Run "mix help deps" to learn about dependencies.
|
||||||
|
defp deps do
|
||||||
|
[
|
||||||
|
# {:dep_from_hexpm, "~> 0.3.0"},
|
||||||
|
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
|
||||||
|
]
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
defmodule CounterTest do
|
||||||
|
use ExUnit.Case
|
||||||
|
doctest Counter
|
||||||
|
|
||||||
|
test "greets the world" do
|
||||||
|
assert Counter.hello() == :world
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
ExUnit.start()
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
# Used by "mix format"
|
||||||
|
[
|
||||||
|
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
|
||||||
|
]
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
# The directory Mix will write compiled artifacts to.
|
||||||
|
/_build/
|
||||||
|
|
||||||
|
# If you run "mix test --cover", coverage assets end up here.
|
||||||
|
/cover/
|
||||||
|
|
||||||
|
# The directory Mix downloads your dependencies sources to.
|
||||||
|
/deps/
|
||||||
|
|
||||||
|
# Where third-party dependencies like ExDoc output generated docs.
|
||||||
|
/doc/
|
||||||
|
|
||||||
|
# Ignore .fetch files in case you like to edit your project deps locally.
|
||||||
|
/.fetch
|
||||||
|
|
||||||
|
# If the VM crashes, it generates a dump, let's ignore it too.
|
||||||
|
erl_crash.dump
|
||||||
|
|
||||||
|
# Also ignore archive artifacts (built via "mix archive.build").
|
||||||
|
*.ez
|
||||||
|
|
||||||
|
# Ignore package tarball (built via "mix hex.build").
|
||||||
|
counter-*.tar
|
||||||
|
|
||||||
|
# Temporary files, for example, from tests.
|
||||||
|
/tmp/
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
# Counter
|
||||||
|
|
||||||
|
**TODO: Add description**
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
|
||||||
|
by adding `counter` to your list of dependencies in `mix.exs`:
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
def deps do
|
||||||
|
[
|
||||||
|
{:counter, "~> 0.1.0"}
|
||||||
|
]
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
|
||||||
|
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
|
||||||
|
be found at <https://hexdocs.pm/counter>.
|
||||||
|
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
defmodule Counter.Application do
|
||||||
|
# See https://hexdocs.pm/elixir/Application.html
|
||||||
|
# for more information on OTP Applications
|
||||||
|
@moduledoc false
|
||||||
|
|
||||||
|
use Application
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def start(_type, _args) do
|
||||||
|
children = [
|
||||||
|
# Starts a worker by calling: Counter.Worker.start_link(arg)
|
||||||
|
Supervisor.child_spec({Counter.Worker, :c1}, id: :counter1),
|
||||||
|
Supervisor.child_spec({Counter.Worker, :c2}, id: :counter2)
|
||||||
|
]
|
||||||
|
|
||||||
|
# See https://hexdocs.pm/elixir/Supervisor.html
|
||||||
|
# for other strategies and supported options
|
||||||
|
opts = [strategy: :one_for_one, name: Counter.Supervisor]
|
||||||
|
Supervisor.start_link(children, opts)
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
defmodule Counter.Worker do
|
||||||
|
use GenServer
|
||||||
|
|
||||||
|
# client
|
||||||
|
def start_link(name) do
|
||||||
|
GenServer.start_link(__MODULE__, nil, name: name)
|
||||||
|
end
|
||||||
|
|
||||||
|
def inc(name, amt \\ 1) do
|
||||||
|
GenServer.cast(name, {:inc, amt})
|
||||||
|
end
|
||||||
|
|
||||||
|
def value(name) do
|
||||||
|
GenServer.call(name, :value)
|
||||||
|
end
|
||||||
|
|
||||||
|
# server impl
|
||||||
|
@impl true
|
||||||
|
def init(_arg) do
|
||||||
|
{:ok, 0}
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def handle_call(:value, _from, state) do
|
||||||
|
{:reply, state, state}
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def handle_cast({:inc, amt}, state) do
|
||||||
|
{:noreply, state + amt}
|
||||||
|
end
|
||||||
|
|
||||||
|
# for demonstration only
|
||||||
|
@impl true
|
||||||
|
def handle_info(:reset, _state) do
|
||||||
|
{:noreply, 0}
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
defmodule Counter.MixProject do
|
||||||
|
use Mix.Project
|
||||||
|
|
||||||
|
def project do
|
||||||
|
[
|
||||||
|
app: :counter,
|
||||||
|
version: "0.1.0",
|
||||||
|
elixir: "~> 1.17",
|
||||||
|
start_permanent: Mix.env() == :prod,
|
||||||
|
deps: deps()
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
|
# Run "mix help compile.app" to learn about applications.
|
||||||
|
def application do
|
||||||
|
[
|
||||||
|
extra_applications: [:logger],
|
||||||
|
mod: {Counter.Application, []}
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
|
# Run "mix help deps" to learn about dependencies.
|
||||||
|
defp deps do
|
||||||
|
[
|
||||||
|
# {:dep_from_hexpm, "~> 0.3.0"},
|
||||||
|
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
|
||||||
|
]
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
defmodule CounterTest do
|
||||||
|
use ExUnit.Case
|
||||||
|
doctest Counter
|
||||||
|
|
||||||
|
test "greets the world" do
|
||||||
|
assert Counter.hello() == :world
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
ExUnit.start()
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
defmodule Lec4 do
|
||||||
|
def create(n \\ 4) do
|
||||||
|
Enum.map(1..n, fn _ -> parent = self(); spawn(fn -> loop(parent) end) end)
|
||||||
|
end
|
||||||
|
|
||||||
|
def info(pid) when is_pid(pid) do
|
||||||
|
case Process.info(pid, [:trap_exit, :links]) do
|
||||||
|
nil -> {pid, :dead}
|
||||||
|
x -> {pid, x}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def info(pids) when is_list(pids) do
|
||||||
|
Enum.map(pids, &info/1)
|
||||||
|
end
|
||||||
|
|
||||||
|
def trap_exit(pid) do
|
||||||
|
send(pid, :trap_exit)
|
||||||
|
end
|
||||||
|
|
||||||
|
def link(pid, other) do
|
||||||
|
send(pid, {:link, other})
|
||||||
|
end
|
||||||
|
|
||||||
|
defp loop(parent) do
|
||||||
|
receive do
|
||||||
|
:trap_exit ->
|
||||||
|
Process.flag(:trap_exit, true)
|
||||||
|
{:link, other} ->
|
||||||
|
Process.link(other)
|
||||||
|
x ->
|
||||||
|
send(parent, x)
|
||||||
|
end
|
||||||
|
loop(parent)
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
# raise("hell") # runtime error
|
||||||
|
# exit(:die) # typtically exit with an atom
|
||||||
|
# throw(:what)
|
||||||
|
# throw(1) # can throw anything
|
||||||
|
|
||||||
|
try do
|
||||||
|
1/0
|
||||||
|
catch type, value ->
|
||||||
|
IO.inspect({type, value})
|
||||||
|
end
|
||||||
|
|
||||||
|
# 3 types of runime error
|
||||||
|
try do
|
||||||
|
raise("hell")
|
||||||
|
catch type, value ->
|
||||||
|
IO.inspect({type, value})
|
||||||
|
end
|
||||||
|
|
||||||
|
try do
|
||||||
|
throw :up
|
||||||
|
catch type, value ->
|
||||||
|
IO.inspect({type, value})
|
||||||
|
end
|
||||||
|
|
||||||
|
try do
|
||||||
|
exit(1)
|
||||||
|
catch type, value ->
|
||||||
|
IO.inspect({type, value})
|
||||||
|
after
|
||||||
|
IO.puts("done")
|
||||||
|
end
|
||||||
Reference in new issue
Block a user