Sha256: 284ca3c837698d6839c20ec8d7618bf34c9daa29cc118dbf95e3afd2d271fe46

Contents?: true

Size: 1.52 KB

Versions: 69

Compression:

Stored size: 1.52 KB

Contents

defmodule Clock do
  defstruct hour: 0, minute: 0
  @type t :: %Clock{hour: integer, minute: integer}
  @type t(hour, minute) :: %Clock{hour: hour, minute: minute}

  @doc """
  Returns a string representation of a clock:

      iex> Clock.new(8, 9) |> to_string
      "08:09"
  """
  @spec new(integer, integer) :: Clock.t()
  def new(hour, minute) do
    rollover(%Clock{hour: hour, minute: minute})
  end

  @doc """
  Adds two clock times:

      iex> Clock.new(10, 0) |> Clock.add(3) |> to_string
      "10:03"
  """
  @spec add(Clock.t(), integer) :: Clock.t()
  def add(%Clock{hour: hour, minute: minute}, add_minute) do
    new(hour, minute + add_minute)
  end

  defp rollover(%Clock{hour: hour, minute: minute}) do
    {carry_hour, minute} = roll_minute(minute)
    %Clock{hour: roll_hour(hour + carry_hour), minute: minute}
  end

  defp roll_hour(hour) when hour < 0 and rem(hour, 24) == 0, do: 0
  defp roll_hour(hour) when hour < 0, do: 24 + rem(hour, 24)
  defp roll_hour(hour) when hour >= 24, do: rem(hour, 24)
  defp roll_hour(hour), do: hour

  defp roll_minute(minute) when minute < 0 do
    {div(minute, 60) - 1, 60 + rem(minute, 60)}
  end

  defp roll_minute(minute) when minute >= 60 do
    {div(minute, 60), rem(minute, 60)}
  end

  defp roll_minute(minute), do: {0, minute}

  defimpl String.Chars, for: Clock do
    def to_string(%Clock{hour: hour, minute: minute}) do
      "#{format(hour)}:#{format(minute)}"
    end

    defp format(number) do
      number |> Integer.to_string() |> String.pad_leading(2, "0")
    end
  end
end

Version data entries

69 entries across 69 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/elixir/exercises/clock/example.exs
trackler-2.2.1.179 tracks/elixir/exercises/clock/example.exs
trackler-2.2.1.178 tracks/elixir/exercises/clock/example.exs
trackler-2.2.1.177 tracks/elixir/exercises/clock/example.exs
trackler-2.2.1.176 tracks/elixir/exercises/clock/example.exs
trackler-2.2.1.175 tracks/elixir/exercises/clock/example.exs
trackler-2.2.1.174 tracks/elixir/exercises/clock/example.exs
trackler-2.2.1.173 tracks/elixir/exercises/clock/example.exs
trackler-2.2.1.172 tracks/elixir/exercises/clock/example.exs
trackler-2.2.1.171 tracks/elixir/exercises/clock/example.exs
trackler-2.2.1.170 tracks/elixir/exercises/clock/example.exs
trackler-2.2.1.169 tracks/elixir/exercises/clock/example.exs
trackler-2.2.1.167 tracks/elixir/exercises/clock/example.exs
trackler-2.2.1.166 tracks/elixir/exercises/clock/example.exs
trackler-2.2.1.165 tracks/elixir/exercises/clock/example.exs
trackler-2.2.1.164 tracks/elixir/exercises/clock/example.exs
trackler-2.2.1.163 tracks/elixir/exercises/clock/example.exs
trackler-2.2.1.162 tracks/elixir/exercises/clock/example.exs
trackler-2.2.1.161 tracks/elixir/exercises/clock/example.exs
trackler-2.2.1.160 tracks/elixir/exercises/clock/example.exs