πŸ—“οΈDay 16

Tuesday, 23 August 2022 - Day 16 of Exhort August 2022 - Exercism (My Elixir Journey)

Exercises

Newsletter

Exercise on Exercism | View my solution

Solution

Expand to see code (spoiler alert)
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

View gist on GitHub

Notes

I had to use File.open! and I could have used IO.puts to avoid having to concatenate a \n.

Chessboard

Exercise on Exercism | View my solution

Solution

Expand to see code (spoiler alert)

Notes

I really love the shorthand: Enum.map(file_range(), &<<&1>>)

Remote Control Car

Exercise on Exercism | View my solution

Solution

Expand to see code (spoiler alert)

Notes

I didn't know about the @enforce_keys module attribute.

Overall progress

An image showing my progress on Exercism. It's 17.9% as of Tuesday, 23 August 2022.
Progress

Last updated