Skip to content

Instantly share code, notes, and snippets.

@petros
Last active August 23, 2022 16:58
Show Gist options
  • Save petros/5f009188ff3aeebbba688136c729f14b to your computer and use it in GitHub Desktop.
Save petros/5f009188ff3aeebbba688136c729f14b to your computer and use it in GitHub Desktop.
Newsletter - Elixir - Exercism
defmodule Newsletter do
@spec read_emails(path :: String.t()) :: list(String.t())
def read_emails(path) do
{:ok, contents} = File.read(path)
String.split(contents, "\n", trim: true)
end
@spec open_log(path :: String.t()) :: pid()
def open_log(path) do
File.open!(path, [:write])
end
@spec log_sent_email(pid(), email :: String.t()) :: :ok
def log_sent_email(pid, email) do
IO.puts(pid, email)
end
@spec close_log(pid()) :: :ok
def close_log(pid) do
File.close(pid)
end
@spec send_newsletter(String.t(), String.t(), fun()) :: :ok
def send_newsletter(emails_path, log_path, send_fun) do
pid = open_log(log_path)
read_emails(emails_path)
|> Enum.each(fn email -> if :ok == send_fun.(email), do: log_sent_email(pid, email) end)
close_log(pid)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment