Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- defmodule Calculator do
- use GenServer
- def init(val) do
- {:ok, val}
- end
- def handle_cast({:add, num}, state) do
- {:noreply, state + num}
- end
- def handle_cast({:sub, num}, state) do
- {:noreply, state - num}
- end
- def handle_cast({:mult, num}, state) do
- {:noreply, state * num}
- end
- def handle_cast({:div, num}, state) do
- {:noreply, div(state, num)}
- end
- def handle_call(:get_result, _ , state) do
- {:reply, state, state}
- end
- def start(num \\ 0) do
- GenServer.start(Calculator, num)
- end
- def add(pid, num) do
- GenServer.cast(pid, {:add, num})
- GenServer.call(pid, :get_result)
- end
- def sub(pid, num) do
- GenServer.cast(pid, {:sub, num})
- GenServer.call(pid, :get_result)
- end
- def mult(pid, num) do
- GenServer.cast(pid, {:mult, num})
- GenServer.call(pid, :get_result)
- end
- def divi(pid, num) do
- GenServer.cast(pid, {:div, num})
- GenServer.call(pid, :get_result)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement