- Mastering Elixir
- André Albuquerque Daniel Caixinha
- 138字
- 2021-08-05 10:42:49
case
case accepts an expression, and one or more patterns, which will match against the return value of the expression. These patterns may include guard clauses. These patterns are matched (from top to bottom), and will run the code associated with the first expression that matches. Here is a simple example:
iex> case Enum.random(1..10) do
...> 2 -> "The lucky ball was 2"
...> 7 -> "The lucky ball was 7"
...> _ -> "The lucky ball was not 2 nor 7"
...> end
"The lucky ball was not 2 nor 7"
Note that your output may differ when running this example, as we're matching against Enum.random/1. In here, the default condition is represented by using _ in the pattern, which will match anything. Although a bit more condensed, the case construct is similar to a multi-clause function.
推薦閱讀
- 騰訊iOS測試實踐
- PHP 編程從入門到實踐
- C#程序設計
- Arduino家居安全系統構建實戰
- UVM實戰
- Service Mesh實戰:基于Linkerd和Kubernetes的微服務實踐
- Scala Data Analysis Cookbook
- Practical Microservices
- C# 7.0本質論
- C Primer Plus(第6版)中文版【最新修訂版】
- Building UIs with Wijmo
- Learning ROS for Robotics Programming
- HTML5 and CSS3:Building Responsive Websites
- Implementing DevOps with Ansible 2
- LabVIEW案例實戰