nick34992
7/1/2017 - 7:08 PM

Elixir: Module: cond, case, when, atom, :math.sqrt()

Elixir: Module: cond, case, when, atom, :math.sqrt()

defmodule Drop do

  def fall_velocity(planemo, distance) when distance >= 0 do
    # iex(1)> Drop.fall_velocity(:earth, 20)
    # :fast
    gravity = case planemo do
     :earth -> 9.8
     :moon  -> 1.6
     :mars  -> 3.71
    end

    velocity = :math.sqrt(2 * gravity * distance)

    cond do
      velocity == 0 -> :stable
      velocity < 5 -> :slow
      velocity >= 5 and velocity < 10 -> :moving
      velocity >= 10 and velocity < 20 -> :fast
      velocity > 20 -> :speedy
    end
  end

end