Skip to content

Instantly share code, notes, and snippets.

@aharpole
aharpole / update_config.ex
Created March 9, 2019 00:09
new genserver update_config
GenServer.update_config(pid, fn config -> Keyword.update(config, :increment_by, 2) end)
@aharpole
aharpole / handle_cast_map.ex
Created March 9, 2019 00:08
handle cast map
def handle_cast(:increment, %{value: value, increment_by: increment_by}) do
{:ok, %{value: value + increment_by}}
end
@aharpole
aharpole / refactored_genserver.ex
Created March 9, 2019 00:07
refactored simple genserver
defmodule IncrementableValue do
use GenServer
defstruct [:value, :increment_by]
@type t :: %__MODULE__{
value: integer,
increment_by: integer
}
@aharpole
aharpole / handle_cast.ex
Created March 9, 2019 00:06
new handle cast
def handle_cast(:increment, state) do
new_state = Map.put(state, :value, &(&1 + state.increment_by))
{:noreply, state}
end
@aharpole
aharpole / handle_call.ex
Created March 9, 2019 00:06
new handle call
def handle_call(:get_data, _, %{value: value} = state) do
{:reply, value, state}
end
@aharpole
aharpole / new_init.ex
Created March 9, 2019 00:06
new init for elixir blog post
def init(%{increment_by: increment_by}) do
{:ok, %__MODULE__{value: 1, increment_by: increment_by}}
end
@aharpole
aharpole / start_link.ex
Created March 9, 2019 00:05
new start_link
@aharpole
aharpole / type.ex
Created March 9, 2019 00:05
type for elixir blog post
@type t :: %__MODULE__{
value: integer,
increment_by: integer
}
@aharpole
aharpole / defstruct.ex
Created March 9, 2019 00:04
defstruct for map based state gen servers blog post
defstruct [:value, :increment_by]
@aharpole
aharpole / simple_genserver.ex
Created March 9, 2019 00:03
Simple GenServer with incrementing integer
defmodule IncrementableValue do
use GenServer
def start_link() do
GenServer.start_link(__MODULE__, [])
end
def init(_) do
{:ok, 1}
end