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

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.

主站蜘蛛池模板: 丹东市| 江安县| 工布江达县| 屯昌县| 米脂县| 镇安县| 措美县| 雷波县| 婺源县| 交城县| 佛山市| 松江区| 石嘴山市| 德保县| 无棣县| 东丽区| 郑州市| 南华县| 辽宁省| 格尔木市| 大新县| 灵寿县| 莱芜市| 墨江| 武汉市| 澄江县| 定陶县| 沁阳市| 正安县| 伊春市| 汕头市| 清镇市| 嵩明县| 吉木萨尔县| 乌拉特前旗| 广饶县| 南丹县| 保山市| 马尔康县| 怀集县| 嘉鱼县|