- 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.
推薦閱讀
- Puppet 4 Essentials(Second Edition)
- 零基礎(chǔ)學(xué)Visual C++第3版
- Node.js 10實(shí)戰(zhàn)
- OpenDaylight Cookbook
- Hands-On Machine Learning with scikit:learn and Scientific Python Toolkits
- Learning RxJava
- 小程序開發(fā)原理與實(shí)戰(zhàn)
- Java編程技術(shù)與項(xiàng)目實(shí)戰(zhàn)(第2版)
- Python數(shù)據(jù)結(jié)構(gòu)與算法(視頻教學(xué)版)
- Hands-On Full Stack Development with Go
- Mastering Business Intelligence with MicroStrategy
- Getting Started with Eclipse Juno
- .NET 4.0面向?qū)ο缶幊搪劊簯?yīng)用篇
- 官方 Scratch 3.0 編程趣味卡:讓孩子們愛上編程(全彩)
- 軟件測試分析與實(shí)踐