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

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.

主站蜘蛛池模板: 宣化县| 绥化市| 平罗县| 绵竹市| 三亚市| 浙江省| 沛县| 闻喜县| 叶城县| 河池市| 绥德县| 广饶县| 加查县| 四平市| 常宁市| 历史| 和龙市| 谷城县| 新乐市| 丰县| 惠安县| 乐业县| 申扎县| 阜康市| 汶上县| 郧西县| 旺苍县| 涪陵区| 山阳县| 巫溪县| 黔江区| 新郑市| 绥棱县| 靖远县| 崇仁县| 伊金霍洛旗| 攀枝花市| 吴堡县| 赣州市| 色达县| 荥阳市|