Building a Functional Core in Elixir

Do Fun Things with Big, Loud Worker-Bees

Trinity college library in Dublin
Trinity College Library, Dublin
defmodule Librarian.Core.FileBlock do
defstruct index: 0,
hash: ""
def new(index, hash) do
%__MODULE__{
index: index,
hash: hash
}
end
end
def find_items(pool, text) do
pool
|> all_items()
|> items_matching_text(text)
end
defp all_items(pool) do
pool.libraries
|> Map.values
|> List.foldr([], fn x, acc -> acc ++ x.media_items end)
end
defmodule Librarian.Core.Request do
defstruct approved: false,
destination_agent: "",
blocks_pending: [],
blocks_staged: [],
blocks_delivered: [],
media_item: nil
def new(media_item, agent) do
%__MODULE__{
blocks_pending: media_item.blocks,
media_item: media_item,
destination_agent: agent
}
end
end
def stage_block(request, block) do
request
|> move_block(block, :blocks_pending, :blocks_staged)
end
def deliver_block(request, block) do
request
|> move_block(block, :blocks_staged, :blocks_delivered)
end
defp move_block(request, block, field_from, field_to) do
request
|> remove_block_from(block, field_from)
|> add_block_to(block, field_to)
end
defp remove_block_from(request, block, field) do
new_list = Map.fetch!(request, field) |> List.delete(block)
Map.put(request, field, new_list)
end
defp add_block_to(request, block, field) do
new_list = Map.fetch!(request, field) ++ [block]
Map.put(request, field, new_list)
end

--

--

In relentless pursuit of elegant simplicity. Tinkerer, writer of tech, fantasy, and sci-fi. Converting napkin drawings into code for @CapitalOne

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kevin Hoffman

In relentless pursuit of elegant simplicity. Tinkerer, writer of tech, fantasy, and sci-fi. Converting napkin drawings into code for @CapitalOne