Elixir-Pattern matching
模式匹配可以用来解构复杂的数据结构:
=
等号操作符
- 解构时要求
=
左侧与右侧的数据类型相同 - 解构tuple,要求
=
左右的tuple中的元素个数相同# tuple 解构的常用场景 iex(21)> {:ok, result} = {:ok, [1, 2, 3]} {:ok, [1, 2, 3]} iex(22)> iex(10)> {a, b, c} = {:a, 1, "hello"} {:a, 1, "hello"} iex(11)> a :a iex(12)> b 1 iex(13)> c "hello" iex(14)> iex(15)> {a, b, c, d} = {:a, 1, "hello"} ** (MatchError) no match of right hand side value: {:a, 1, "hello"}iex(15)> {a, b, c, d} = {:a, 1, "hello", :d, :e} ** (MatchError) no match of right hand side value: {:a, 1, "hello", :d, :e}
- 解构list,与tuple相同,
=
左右两侧的数据长度相同iex(1)> [a, b, c] = [:a, 2, "hello"] [:a, 2, "hello"] iex(2)> a :a iex(3)> b 2 iex(4)> c "hello" iex(5)> # 匹配list的head和tail iex(5)> [head | tail] = [1, 2, 3, 4] [1, 2, 3, 4] iex(6)> head 1 iex(7)> tail [2, 3, 4] iex(8)>
^
pin操作符
使用了 ^
pin 操作符,在进行模式匹配时,只是进行比较,不会在给此变量重新付值
iex(1)> x = 1
1
iex(2)> x = 2
2
iex(3)> ^x = 3
** (MatchError) no match of right hand side value: 3iex(3)> ^x = 2
2
iex(4)> iex(8)> {^x, a} = {2, "a"}
{2, "a"}
iex(9)> a
"a"
iex(10)> x
2
iex(11)> {^x, a} = {"x", "a"}
** (MatchError) no match of right hand side value: {"x", "a"}
注意⚠️:模式匹配时,=
等式左侧不能是函数。
iex(2)> length([1, 2, 3]) = 3
** (CompileError) iex:2: cannot invoke remote function :erlang.length/1 inside a match
iex(2)> 3 = length([1, 2, 3])
3
iex(3)>
iex(12)> sum = fn a, b -> a + b end
#Function<41.3316493/2 in :erl_eval.expr/6>
iex(13)> sum.(1, 2) = 3
** (CompileError) iex:13: invalid pattern in match, anonymous call is not allowed in matches
Elixir-Pattern matching相关推荐
- CodeForces - 1476E Pattern Matching(字典树+拓扑)
题目链接:点击查看 题目大意:给出 nnn 个模式串和 mmm 个匹配串,题目要求输出一种模式串的排列方式,使得 mmm 个模式串从头开始匹配的话,可以匹配到相应的模式串 模式串的长度不超过 444, ...
- C#9.0 终于来了,带你一起解读 nint 和 Pattern matching 两大新特性玩法
一:背景 1. 讲故事 上一篇C#9.0 终于来了,您还学的动吗? 带上VS一起解读吧!跟大家聊到了Target-typed new 和 Lambda discard parameters,看博客园和 ...
- E. Pattern Matching(题意理解+拓扑排序)
E. Pattern Matching 首先p[mtj]p[mt_j]p[mtj]必须能够匹配所给字符sjs_jsj,然后把所有能够匹配的sjs_jsj的其他模板串也找出来,这些必须放在p[mt ...
- 9.7. Pattern Matching
9.7. Pattern Matching 9.7. 模式匹配 There are three separate approaches to pattern matching provided by ...
- PEP 634 – Structural Pattern Matching: Specification
PEP 634 – Structural Pattern Matching: Specification PEP 634 – 结构化模式匹配:规范 PEP: 634 Title: Structural ...
- 论文翻译——Multi-Constrained Graph Pattern Matching in Large-Scale Contextual Social Graphs
文章目录 Abstract 附加 Introduction Background 附加 Problem 附加 Contribuitions 附加 Related Work (1) 附加 (2) 附加 ...
- Python 3.10 的新功能:模式匹配 Pattern Matching
简介 2021 年 3 月 2 日的时候,Guido 发推说 3.10.0a6 出来了,文档也已经有了,4 月 5 日会释出 a7,5 月 3 日出 b1. 推文中还特别提到「With Pattern ...
- 模式匹配Pattern Matching
目录 1.模式匹配(pattern matching)的概念 2. 制造模式匹配的测试串 3. 模式匹配蛮力算法(Brute-Force,也成Naive朴素算法) 3.1 Version 1 3.2 ...
- 面试题 16.18. Pattern Matching LCCI
Title 你有两个字符串,即pattern和value. pattern字符串由字母"a"和"b"组成,用于描述字符串中的模式.例如,字符串"cat ...
- 函数式编程之-模式匹配(Pattern matching)
编者:C# 7.0也加入了模式匹配,来源于F#. 模式匹配在F#是非常普遍的,用来对某个值进行分支匹配或流程控制. 模式匹配的基本用法 模式匹配通过match...with表达式来完成,一个完整的模式 ...
最新文章
- matlab画复变函数,科学网—复数复变函数的Matlab计算与绘图 - 周铁戈的博文
- Java 2程序设计,Java2程序设计
- centos7 安装 python3
- 7、Power Map—实例:添加二维数据表以及批注
- 数组名和指针(这里为指向数组首元素的指针)区别?
- 如何更改 C# Record 构造函数的行为
- python去除图像光照不均匀_低光照环境下图像增强相关
- 以云战“疫”,这次阿里云又让人们惊了……
- 使shell用结构化命令
- 移动端ios和安卓input问题
- SAP License:雾里看花系列——SAP顾问应该脱离”保姆”的角色
- 库存出现负数 mysql_前台支付商品成功后,sku表库存,购买数量会重复执行减操作好几次了,导致库存为负数...
- 如何把JSON对象转为map对象
- 平衡二叉树的调整整理
- Contrastive Search Decoding——一种对比搜索解码文本生成算法
- 2 简单使用原形工具Axure RP
- 公司股权等比稀释是什么意思
- Kali Linux 秘籍 第九章 无线攻击
- Google Earth Engine——从Python中的经纬度坐标获取Modis正弦图块网格位置
- SOA面向服务的架构设计
热门文章
- 运动员和教练(接口实现)
- POJ2010 Moo University - Financial Aid
- 用python画小猪佩奇动画片全集_用python画小猪佩奇(非原创)
- 从家里到阿里,学弟求职的一年
- 安装Python module
- 设计类网站的“联系我们”页面设计盘点
- java zlib1.dll,zlib1.2.11静态编译
- v-show和v-if有什么区别?使用场景分别是什么?
- QTextEdit设置可输入字符字节限制
- 在线教育大数据营销平台实战(搭建篇)