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

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.

主站蜘蛛池模板: 濮阳市| 林州市| 永安市| 金湖县| 高台县| 瑞昌市| 贡山| 花垣县| 康平县| 沙田区| 河南省| 桂林市| 比如县| 大名县| 桦川县| 石首市| 上蔡县| 安阳县| 霍邱县| 乌拉特中旗| 彭山县| 独山县| 普安县| 长宁区| 常州市| 同江市| 壶关县| 丹棱县| 清远市| 陆川县| 新蔡县| 进贤县| 衡水市| 弥渡县| 增城市| 深泽县| 台湾省| 镇江市| 林周县| 阿图什市| 奉节县|