is one of the cornerstones of an equational style of definition; more often than not it leads to a cleaner and more readily understandable definition than a style based on conditional equations. It also simplifies the process of reasoning formally about functions.
do def talk(:bob), do: "Hi, uncle Bob!" def talk(42), do: "Oh! the answer to life the universe and everything" def talk(n) when is_number(n), do: "You're an ordinary number" def talk(_), do: "I don't really know what to tell you" end
IO, only: [puts: 1] def new, do: spawn &start/0 def start do receive do {:answer, name} -> puts talk(name) _ -> puts "I don't know how to handle that" after 2000 -> puts "[#{inspect(self)}] Tell me something to do" end start end def talk(:bob), do: "Hi, uncle Bob!" def talk(42), do: "Oh! the answer to life the universe and everything" def talk(n) when is_number(n), do: "You're an ordinary number" def talk(_), do: "I don't really know what to tell you" end
# Examples run concurrently test "talking to bob" do assert Speaker.talk(:bob) == "Hi, uncle Bob!" End test "talking to a stranger" do assert Speaker.talk(:voldemort) == "I don't really know what to tell you" end end Dimitris Zorbas - Athens Ruby Meetup#26
Meetup#26 • Failures are embraced and managed. Let it crash! • Failure is isolated in process level. No exceptions. • Failing fast is revealing. The system stays up. • Write Offensive Code