Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us
Notesclub

Dates and Time

dates_and_time.livemd

Dates and Time

Extracting a list of lists

grid = [
  [1, 2, 3],
  [2, 3, 4],
  [4, 5, 6]
]

[head | _tail] = grid

row = Enum.at(grid, 0)
cell = Enum.at(row, 2)
grid
|> Enum.at(0)
|> Enum.at(0)
list = [1, 2, 3]
[head | _tail] = list
head
Enum.at(list, 0)

Appending Elements To A List

# Prepending
element = 1
list = [2, 3]
[element | list]
## Appending
element = 3
list = [1, 2]
list ++ [element]
large_list = Enum.to_list(1..10_000_000)
[0 | large_list]
large_list ++ [10_000_001]

Conversion

My machine encounteres an error when comparing DateTime difference in minutes. Report this to Livebook in an issue.

DateTime.diff(~U[2020-01-01 12:00:00Z], ~U[2020-01-01 12:30:00Z], :minutes)

Time

map = %{key: 1}
map[:key]
conversions = %{seconds: 1, minutes: 60, hours: 60 * 60, days: 60 * 60 * 24}
unit = :days
conversions[unit]
defmodule TimeConverter do
  # Using a module attribute to share value between functions
  # We access the value of the key using map[key] notation.
  # i.e. @conversion[:seconds] -> 1
  # i.e. @conversion[:hours] -> 60 * 60 -> 3600
  @conversions %{seconds: 1, minutes: 60, hours: 60 * 60, days: 60 * 60 * 24}

  # Multiply amount by conversion unit to get result.
  # i.e. 2 :minutes -> 2 * 60 seconds -> 120 seconds
  def to_seconds(amount, unit) do
    @conversions[unit] * amount
  end

  # Divide amount by conversion unit to get result.
  # i.e. 120 seconds -> 120 / 60 -> 2 minutes
  def from_seconds(amount, unit) do
    amount / @conversions[unit]
  end
end

TimeConverter.from_seconds(120, :minutes)