2008-03-11
Elite教程系列(二):模式匹配 Pattern Matching
关键字: elite aom apusic kingdee
模式匹配(Pattern Matching),这个已经不是什么新的概念了,很多语言都是,包括现在大家都在学习的erlang.今天我来讨论下elite中的模式匹配。
首先来个例子:
a,b 分别和 a ,[1,2,3] 匹配。
执行上面的代码输出
elite sample.xel
1
[1, 2, 3]
不过可惜的是elite似乎不支持 “|” 语法。
c 无法匹配后面全部的内容。
elite 还支持这样的匹配,很容易读懂
这个就不需要解释了,看下运行的结果就知道了
elite还支持基于属性匹配
输出
同时我们可以对象其他的属性赋值给局部变量
首先来个例子:
define [a,b] = ['1',[1,2,3]] a->print b->print
a,b 分别和 a ,[1,2,3] 匹配。
执行上面的代码输出
引用
elite sample.xel
1
[1, 2, 3]
不过可惜的是elite似乎不支持 “|” 语法。
define [a,b|c] = ['1',[1,2,3],'dd','cc',[1,2,3,4]]
c 无法匹配后面全部的内容。
elite 还支持这样的匹配,很容易读懂
define test(n){
: is Number => "$n is number"
: is String => "$n is string"
}
test(2)->print
test('test')->print
这个就不需要解释了,看下运行的结果就知道了
2 is number test is string
elite还支持基于属性匹配
class Person(name){}
define martin = Person.new('Martin')
define kevin = Person.new('Kevin')
martin.name->print
kevin.name->print
define test(p){
: {name:'Martin'} =>"Hello ${p.name}"
: {name:'Kevin'} =>"Welcome back,${p.name}"
}
martin->test->print
kevin->test->print
输出
Hello Martin Welcome back,Kevin
同时我们可以对象其他的属性赋值给局部变量
class Person(name,age){}
define martin = Person.new('Martin',26)
define test(p){
: {name:'Martin',age:x} =>"Hello ${p.name},your are $x years old"
}
martin->test->print
发表评论
- 浏览: 103318 次
- 性别:

- 来自: 上海

- 详细资料
搜索本博客
我的相册
2008-6-24 8-00-01.png
共 59 张
共 59 张
最近加入圈子
最新评论
-
无聊,无题
那么多数字无聊进去的……
-- by mmiwwcom -
无聊,无题
身份证号码?
-- by mmiwwcom -
Code Review&JRuby on Rai ...
我悄悄的看看
-- by mmiwwcom -
DM & Merb 快速体验
尝试安装的时候发现DM无法在win32的平台上通过gem安装?
-- by yy77 -
DM & Merb 快速体验
Raecoo 写道Merb的精简,是要看以什么样的角度去看待了 Merb的目标 ...
-- by neodoxy






评论排行榜