从性能的角度来看,我想知道匹配的模式顺序是否影响函数的效率,或者它更多地与预期的不同匹配的比例有关(即,如果一个模式发生的方式比其他模式更多,它应该更早出现).
(* a function to return the smaller of two int options, or None if both are None. If exactly one argument is None, return the other. *)
let min_option (x: int option) (y: int option) : int option =
match x, y with
None, None -> None
| Some x, None -> Some x
| None, Some y -> Some y
| Some x, Some y -> if x < y then Some x else Some y
let () …Run Code Online (Sandbox Code Playgroud) 试图为链表实现一个超级简单的计数方法,但是当我尝试匹配我的case对象的模式时,我得到一个错误.
这是代码:
trait LinkedList[+A] {
def count = 0
}
case object Leaf extends LinkedList[Nothing]
case class Node[A](head: A, tail: LinkedList[A]) extends LinkedList[A] {
override def count: Int = this match {
case Node(_, t) => 1 + t.count
case Leaf => 0
}
}
Run Code Online (Sandbox Code Playgroud)
这是错误:
scala> :load LinkedList.scala
Loading LinkedList.scala...
defined trait LinkedList
defined module Leaf
<console>:17: error: pattern type is incompatible with expected type;
found : Leaf.type
required: Node[A]
Note: if you intended to match against the class, …Run Code Online (Sandbox Code Playgroud) 为什么以下尝试在列表推导中进行模式匹配不起作用?
示例:在术语数据类型中同时替换原子.
数据类型:
data Term a
= Atom a
| Compound (Term a) (Term a)
deriving Show
Run Code Online (Sandbox Code Playgroud)
原子的替换算法(选择第一个匹配的替换,如果有的话,忽略其余的):
subs :: [(Term a, Term a)] -> Term a -> Term a
subs subList term = case term of
atom@(Atom x) -> let substitutions =
[ s | s@(Atom x, _) <- subList ]
in if null substitutions
then atom
else snd . head $ substitutions
(Compound t1 t2) -> Compound (subs subList t1) (subs subList t2)
Run Code Online (Sandbox Code Playgroud)
一些测试数据:
subList = [((Atom 'a'), …Run Code Online (Sandbox Code Playgroud) 是否可以String使用静态str值匹配Rust中的结构?这是一个最小的例子:
struct SomeStruct {
a: String,
}
fn main() {
let s = SomeStruct {
a: "Test".to_string(),
};
match s {
SomeStruct { a: "Test" } => {
println!("Match");
}
}
}
Run Code Online (Sandbox Code Playgroud)
这将无法编译,因为静态str引用无法与String成员匹配.它可以在没有解构的情况下工作a,然后在匹配中添加嵌套的if语句吗?
这些是无效的电子邮件。
email@domain.web
.email @ domain.com
我已在以下网站中检查了以上电子邮件,所有这些返回均无效。 http://isemail.info/关于 http://sqa.fyicenter.com/Online_Test_Tools/Email_Address_Format_Validator.php。
然而,android.util.Patterns.EMAIL_ADDRESS模式验证了两者。
是否有错误或我缺少什么?
有人可以解释这三个查询之间的性能差异吗?
concat() 功能:
explain analyze
select * from person
where (concat(last_name, ' ', first_name, ' ', middle_name) like '%???%');
Seq Scan on person (cost=0.00..4.86 rows=1 width=15293) (actual time=0.032..0.140 rows=6 loops=1)
Filter: (pg_catalog.concat(last_name, ' ', first_name, ' ', middle_name) ~~ '%???%'::text)
Total runtime: 0.178 ms
Run Code Online (Sandbox Code Playgroud)
SQL标准串联||:
explain analyze
select * from person
where ((last_name || ' ' || first_name || ' ' || middle_name) like '%???%');
Seq Scan on person (cost=0.00..5.28 rows=1 width=15293) (actual time=0.023..0.080 rows=6 loops=1)
Filter: ((((((last_name)::text …Run Code Online (Sandbox Code Playgroud) sql postgresql concatenation pattern-matching postgresql-performance
我对java中的scala相当新,也是模式匹配的新手.我试图解决的一个问题是何时使用它以及它的成本/收益是什么.例如这个
def myThing(a: Int): Int = a match {
case a: Int if a > 0 => a
case _ => myThing(a + 1)
}
Run Code Online (Sandbox Code Playgroud)
与此相同(除非我真的误解了一些东西)
def myThing(a: Int): Int = {
if (a > 0) a
else myThing(a + 1)
}
Run Code Online (Sandbox Code Playgroud)
所以我的实际问题是: 但它们是否以同样的方式运行?我的模式匹配示例尾递归吗?如果没有,那么为什么不在第二个例子中呢?
还有其他我应该担心的事情,比如资源吗?或者我应该总是尝试使用模式匹配?
我一直在寻找这些答案,但没有找到任何"最佳实践"!
编辑:我知道使用的例子有点做作 - 我刚刚添加它以清楚它下面的问题 - 谢谢!
哪里request是HttpRequestMessage来自System.Net.Http,我试图使用模式匹配来确定哪种方法是用来做请求.
这是一个人为的例子,它证明了我的问题:
let m = match request.Method with
| HttpMethod.Get -> "GET"
| HttpMethod.Post -> "POST"
Run Code Online (Sandbox Code Playgroud)
这导致:
分析器错误:未定义字段,构造函数或成员"Get"
为什么这不起作用,我如何使用模式匹配或更合适的技术来实现相同的目标?
由于某种原因,此模式匹配中的小于运算符将不起作用.这是我唯一的错误,它让我疯狂.我可能错过了一些非常明显的东西,但感谢所有的帮助.
let CheckAccount account =
match account with
| {Balance < 10.00} -> Console.WriteLine("Balance is Low")
| {Balance >= 10.00 and <= 100.00} -> Console.WriteLine("Balance is OK")
| {Balance > 100.00} -> Console.WriteLine("Balance is High")
Run Code Online (Sandbox Code Playgroud)
这是类型:
type Account = {AccountNumber:string
mutable Balance:float}
member this.Withdraw(amnt:float) =
if amnt > this.Balance then
Console.WriteLine("Unable to withdraw. The Amount you wish to withdraw is greater than your current balance.")
else
this.Balance <- this.Balance - amnt
Console.WriteLine("You have withdrawn £" + amnt.ToString() + ". Your …Run Code Online (Sandbox Code Playgroud) 我正在玩Scala的模式匹配.到目前为止我所知道的是,一个名为operator的提取器是左关联的,一个名为方法或类型的提取器是右关联的.
我目前的方法看起来像这样:
object Foo {
def unapply(tokens: Seq[String]): Option[(Seq[String], String)] = // do something
}
// ...
object Main extends App {
Seq("hi", "bye") match {
case xs Foo foo2 Foo foo1 => // do something with result
case _ => // handle error
}
}
Run Code Online (Sandbox Code Playgroud)
这有点不愉快,因为它需要我反向编写我的匹配或由于正确的关联性而反向匹配它们.我更愿意,如果我能写这样的东西:
object Foo {
def unapply(tokens: Seq[String]): Option[(String, Seq[String])] = // do something
}
// ...
object Main extends App {
Seq("hi", "bye") match {
case foo1 Foo foo2 xs => // do something …Run Code Online (Sandbox Code Playgroud) pattern-matching ×10
scala ×3
f# ×2
android ×1
case-class ×1
haskell ×1
ocaml ×1
postgresql ×1
recursion ×1
rust ×1
sql ×1