我经常需要匹配应该具有相同构造函数的元组元组.最后,笼子_,_总是炙手可热.这当然是脆弱的,添加到该类型的任何其他构造函数将完全编译.我目前的想法是连接第一个但不是第二个参数的匹配.但是,还有其他选择吗?
例如,
type data = | States of int array
| Chars of (char list) array
let median a b = match a,b with
| States xs, States ys ->
assert( (Array.length xs) = (Array.length ys) );
States (Array.init (Array.length xs) (fun i -> xs.(i) lor ys.(i)))
| Chars xs, Chars ys ->
assert( (Array.length xs) = (Array.length ys) );
let union c1 c2 = (List.filter (fun x -> not (List.mem x c2)) c1) @ c2 in
Chars …Run Code Online (Sandbox Code Playgroud) 我有一个模式匹配例程,它根据用于请求命令的URL从std :: map查找值.URL映射表填充了以下值:
// Assume this->commands_ is defined elsewhere as std::map<std::string, int>
// Providing a number of URL examples to give an idea of the structure of
// the URLs
this->commands_["/session"] = 1;
this->commands_["/session/:sessionid/url"] = 2;
this->commands_["/session/:sessionid/back"] = 3;
this->commands_["/session/:sessionid/forward"] = 4;
this->commands_["/session/:sessionid/element"] = 5;
this->commands_["/session/:sessionid/element/:id/text"] = 6;
this->commands_["/session/:sessionid/element/:id/value"] = 7;
Run Code Online (Sandbox Code Playgroud)
每个URL模式中的标记(由前面的':'指定)被查找例程的调用中的实际值替换(例如"/session/1234-8a0f/element/5bed-6789/text"),但是我将需要保留的命名参数.上例中的命名标记列表并非详尽无遗,并且上面列出的位置可能还有其他命名标记.请注意,令牌值是十六进制编码的数字.
目前,我正在迭代映射的键,用正则表达式值替换替换标记,并使用std :: tr1正则表达式类对请求的值执行正则表达式匹配,将匹配的标记名称和值捕获到向量中.代码在功能上与此等效(代码比通常为清晰起见而更冗长):
// Assume "using namespace std;" has been declared,
// and appropriate headers #included.
int Server::LookupCommand(const string& uri,
vector<string>* names,
vector<string>* values) {
int …Run Code Online (Sandbox Code Playgroud) 看看这个F#/ OCaml代码:
type AllPossible =
| A of int
| B of int*int
| ...
| Z of ...
let foo x =
....
match x with
| A(value) | B(value,_) -> (* LINE 1 *)
(* do something with the first (or only, in the case of A) value *)
...
(* now do something that is different in the case of B *)
let possibleData =
match x with
| A(a) -> bar1(a)
| B(a,b) -> bar2(a+b)
| …Run Code Online (Sandbox Code Playgroud) 模式匹配一个Vec<T>可以通过使用来完成&v[..]或v.as_slice().
let x = vec![1, 2, 3];
match &x[..] {
[] => println!("empty"),
[_] => println!("one"),
[..] => println!("many"),
}
Run Code Online (Sandbox Code Playgroud)
如果我有一个包含Vec我要匹配的字段的枚举,我需要在外部匹配臂内创建一个嵌套匹配:
enum Test {
Many(Vec<u8>),
Text(String),
}
fn main() {
let x = Test::Many(vec![1, 2, 3]);
match x {
Test::Text(s) => println!("{}", s),
Test::Many(v) => match &v[..] {
[] => println!("empty"),
[_] => println!("one"),
[..] => println!("many"),
}
}
}
Run Code Online (Sandbox Code Playgroud)
我希望能够做的是直接匹配Vec以下示例中的as:
match x {
Test::Text(s) => println!("{}", s),
Test::Many([]) => …Run Code Online (Sandbox Code Playgroud) 我是Python编程的初学者,我对构建代码有疑问.
假设我有以下数据:
150 z Brazil
160 a Toys R Us
Run Code Online (Sandbox Code Playgroud)
我想代码,这样如果我们看到的模式bbb \t (one digit or character not a) \t,我会更换bbb \t digit \t有bbb_$d$_.(这里,\t表示选项卡并bbb指示一个数字).
因此,输出将是150_$z$_Brazil,并且输出为160 a Toys R Us作为数字后不会获得160是a.
我的问题是,我如何编码,以便选择一个数字或不是a?
作为Haskell的初学者,我发现很难在视觉上识别出一些模式匹配的例子.
似乎在let绑定中有时模式匹配发生在lhs上,当调用一个函数并且值绑定到rhs上的方程中的变量时,如下面的代码示例所示:
let boot x y z = x * y + z in boot 3 4 2
Run Code Online (Sandbox Code Playgroud)
然而,有时函数将在rhs中运行,并且该函数的返回值将被绑定到等式的lhs中的值,如"了解你的Haskell"中State Monad的定义:
instance Monad (State s) where
return x = State $ \s -> (x,s)
(State h) >>= f = State $ \s -> let (a, newState) = h s
(State g) = f a
in g newState
Run Code Online (Sandbox Code Playgroud)
函数h使用lambda参数s运行,返回值绑定到(a,newState).
对于一个新的Haskell程序员来说,这有点令人困惑.我可以想象你可能会遇到的情况:
let f a b = g c d in ...
Run Code Online (Sandbox Code Playgroud)
函数g将返回一个函数,两个参数作为返回值.在这种情况下,"fa b"是否需要用括号括起来进行模式匹配?我试图找到模式匹配如何发生的明确解释.
我已经阅读了大部分"了解你是一个很好的Haskell",以及"真实世界Haskell"和"Haskell的温和介绍"的片段,我还没有找到一个明确的解释,如何确定模式匹配是否应该在函数的参数或函数的返回值上发生.任何帮助清除这一点将不胜感激.
Swift是一个非常实用的语言,函数式语言都是关于表达式而不是语句,这就是开关模式匹配让我感到困惑的原因.
所有的例子都是这样的:
switch x {
case > 0:
print("positive")
case < 0:
print("negative")
case 0:
print("zero")
}
Run Code Online (Sandbox Code Playgroud)
但我想做这样的事情:
let result = switch x {
case > 0:
"positive"
case < 0:
"negative"
case 0:
"zero"
}
Run Code Online (Sandbox Code Playgroud)
目前我能看到的唯一方法是:
var result: String?
switch x {
case > 0:
result = "positive"
case < 0:
result = "negative"
case 0:
result = "zero"
}
if let s = result {
//...
}
Run Code Online (Sandbox Code Playgroud)
这显然不像基于'表达式'的switch语句那样优雅.是否有任何工作或替代方案或苹果需要做些什么来增强语言?
expression functional-programming pattern-matching switch-statement swift
我在PostgreSQL(9.5.1)中有以下查询:
select e.id, (select count(id) from imgitem ii where ii.tabid = e.id and ii.tab = 'esp') as imgs,
e.ano, e.mes, e.dia, cast(cast(e.ano as varchar(4))||'-'||right('0'||cast(e.mes as varchar(2)),2)||'-'|| right('0'||cast(e.dia as varchar(2)),2) as varchar(10)) as data,
pl.pltag, e.inpa, e.det, d.ano anodet, coalesce(p.abrev,'')||' ('||coalesce(p.prenome,'')||')' determinador, d.tax, coalesce(v.val,v.valf)||' '||vu.unit as altura,
coalesce(v1.val,v1.valf)||' '||vu1.unit as DAP, d.fam, tf.nome família, d.gen, tg.nome gênero, d.sp, ts.nome espécie, d.inf, e.loc, l.nome localidade, e.lat, e.lon
from esp e
left join det d on e.det = d.id
left join tax tf …Run Code Online (Sandbox Code Playgroud) postgresql performance pattern-matching query-performance postgresql-performance
我一直在教自己OCaml,并一直在尝试查看Pervasives模块和其他模块,以找到一个包含类似C/C++函数的函数的模块,isdigit并isalpha尝试清理我的代码.
作为检查信件的条件语句之一如下:
if token > 64 && token < 91 || token > 96 && token < 123 then (* Do something * )
Run Code Online (Sandbox Code Playgroud)
是否OCaml有一个等同于isdigit和的模块isalpha?
这是我的代码:
tell :: (Show a) => [a] -> String
tell [] = "The list is empty"
tell (x:[]) = "The list has one element: " ++ show x
tell (x:y:[]) = "The list has two elements: " ++ show x ++ " and " ++ show y
tell (x:y:_) = "Other"
Run Code Online (Sandbox Code Playgroud)
在最后一行中,为什么不能更改(x:y:_)为[x, y, _]?
全局含义是_什么?
pattern-matching ×10
ocaml ×3
haskell ×2
c++ ×1
clause ×1
enums ×1
expression ×1
f# ×1
function ×1
module ×1
performance ×1
postgresql ×1
python ×1
regex ×1
rust ×1
swift ×1
tr1 ×1