- 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.
推薦閱讀
- Web應(yīng)用系統(tǒng)開(kāi)發(fā)實(shí)踐(C#)
- Mobile Application Development:JavaScript Frameworks
- 數(shù)據(jù)結(jié)構(gòu)習(xí)題精解(C語(yǔ)言實(shí)現(xiàn)+微課視頻)
- Instant QlikView 11 Application Development
- Easy Web Development with WaveMaker
- Java性能權(quán)威指南(第2版)
- 深度強(qiáng)化學(xué)習(xí)算法與實(shí)踐:基于PyTorch的實(shí)現(xiàn)
- Android 應(yīng)用案例開(kāi)發(fā)大全(第3版)
- Mastering ServiceNow(Second Edition)
- ASP.NET開(kāi)發(fā)與應(yīng)用教程
- Python計(jì)算機(jī)視覺(jué)和自然語(yǔ)言處理
- JavaScript編程精解(原書(shū)第2版)
- 零基礎(chǔ)學(xué)Java第2版
- Flask開(kāi)發(fā)Web搜索引擎入門(mén)與實(shí)戰(zhàn)
- MongoDB Administrator’s Guide