Elixir
  • My Elixir journey
  • Why functional programming?
  • Exhort
    • 🗓️Day 22
    • 🗓️Day 21
    • 🗓️Day 20
    • 🗓️Day 19
    • 🗓️Day 18
    • 🗓️Day 17
    • 🗓️Day 16
    • 🗓️Day 15
    • 🗓️Day 14
  • Bits and pieces
    • Clean mix dependencies
    • Run tests automatically on save
    • Run tests and stop on first failure
    • How to remove Tailwind from a Phoenix project
Powered by GitBook
On this page
  • Exercises
  • Captain's Log
  • Overall progress
  1. Exhort

Day 18

Thursday, 25 August 2022 - Day 18 of Exhort August 2022 - Exercism (My Elixir Journey)

PreviousDay 19NextDay 17

Last updated 2 years ago

Exercises

Captain's Log

|

Solution

Expand to see code (spoiler alert)
defmodule CaptainsLog do
  @planetary_classes ["D", "H", "J", "K", "L", "M", "N", "R", "T", "Y"]

  @spec random_planet_class() :: String.t()
  def random_planet_class(), do: Enum.random(@planetary_classes)

  @spec random_ship_registry_number() :: String.t()
  def random_ship_registry_number(), do: "NCC-#{Enum.random(1000..9999)}"

  @spec random_stardate() :: float
  def random_stardate(), do: 41000.0 + (42000.0 - 41000.0) * :rand.uniform()

  @spec format_stardate(Float.t()) :: String.t()
  def format_stardate(stardate), do: :io_lib.format("~.1f", [stardate]) |> List.to_string()
end

Notes

Part of this exercise is showing you how to use Erlang libraries and functions for functionality that doesn't exist in Elixir. However, the Erlang documentation for those functions is not as digestible as Elixir's documentation.

Overall progress

🗓️
Exercise on Exercism
View my solution
View gist on GitHub
Captain's Log
Progress
An image showing my progress on Exercism. It's 20.5% as of Thursday, 25 August 2022.