官术网_书友最值得收藏!

  • 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.

主站蜘蛛池模板: 腾冲县| 临沭县| 澄城县| 金门县| 年辖:市辖区| 靖江市| 海盐县| 敦化市| 巴彦淖尔市| 霍州市| 板桥市| 屯门区| 塔城市| 万源市| 永嘉县| 井陉县| 山阳县| 浏阳市| 望江县| 陈巴尔虎旗| 布尔津县| 乐业县| 肇庆市| 方正县| 堆龙德庆县| 那坡县| 舟曲县| 德庆县| 宜良县| 无锡市| 龙州县| 西吉县| 北票市| 张家口市| 玉溪市| 陇南市| 离岛区| 繁峙县| 四平市| 内江市| 新邵县|