lec03 generic servers

This commit is contained in:
SowinskiBraeden committed 2026-09-22 12:21:27 -07:00
1 parent a579cf11a1
commit 4abfd6b848
23 files changed
+502

No files matched your search

+4
View File
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
+26
View File
@@ -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/
+21
View File
@@ -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>.
+18
View File
@@ -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
+41
View File
@@ -0,0 +1,41 @@
defmodule Counter.Worker do
use GenServer # use builtint Elixir Generic Server
### Client API ###
def start(n \\ 0) do
GenServer.start(__MODULE__, n)
end
def inc(pid, amt \\ 1) do
GenServer.cast(pid, {:inc, amt})
end
def dec(pid, amt \\ 1) do
GenServer.cast(pid, {:dec, amt})
end
def value(pid) do
GenServer.call(pid, :value)
end
### Server Impl ###
@impl true # annotation that signature agrees with callback
def init(arg) do # arg comes from 2nd arg of GenServer.start
{:ok, arg}
end
@impl true
def handle_call(:value, _from, state) do
{:reply, state, state} # {:reply, actual_reply, new_state}
end
@impl true
def handle_cast({:inc, amt}, state) do
{:noreply, state + amt} # {:noreply, new_state}
end
@impl true
def handle_cast({:dec, amt}, state) do
{:noreply, state - amt} # {:noreply, new_state}
end
end
+28
View File
@@ -0,0 +1,28 @@
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]
]
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
+1
View File
@@ -0,0 +1 @@
ExUnit.start()