Elixir: Module: cond construct
In the Elixir module above, the cond
construct evaluates each expression for the first clause which evaluates to a truthy value, based on the velocity
value’s range, then converts a numerical float value from the case
construct to an atom which describes velocity range.
- 0 to
:stable
- 1–5 to
:slow
- 6–10 to
:moving
- 11–20 to
:fast
- 21+ to
:speedy
In the cond
construct, :stable
, :slow
, :moving
, :fast
and :speedy
are atoms. In the case
construct, are the atoms :earth
, :moon
and :mars
.
To run the module, it first needs to be compiled (see interaction below). Then, if the Earth distance value is 20, then Drop.fall_velocity(:earth,20)
could be entered at the Elixir interactive shell prompt (IEx):
iex(1)> c("drop.ex")
[Drop]
iex(2)> Drop.fall_velocity(:earth, 20)
:fast
iex(3)> Drop.fall_velocity(:earth, 21)
:speedy