Skip to content

Latest commit

 

History

History
18 lines (16 loc) · 479 Bytes

elixir_receive_do.md

File metadata and controls

18 lines (16 loc) · 479 Bytes

Elixir Receive Do

Receive do receives a message and processes it. In this example it calculates the area of a shape with dimensions specified. To process multiple messages it recursively calls the area_loop() function.

defmodule Geometry do
  def area_loop do
    receive do
      {:rectangle, w, h} ->
        IO.puts("Area = #{w * h}")
        area_loop()
      {:circle, r} ->
        IO.puts("Area = #{3.14 * r * r}")
        area_loop()
    end
  end
end