所以我正在编写一个简单的方法来总结列表中前 3 个或更少的整数,但我对匹配模式感到困惑。
我目前有
let sums l = match l with
| [] -> 0
| (h1::h2::h3::_) -> h1+h2+h3
| [h1;h2;h3] -> h1+h2+h3
| [h1;h2] -> h1+h2
| [h1] -> h1
Run Code Online (Sandbox Code Playgroud)
这是否涵盖了所有情况?另外,为什么对于 3 个或更多元素我不能写类似 [h1;h2;h3;_] 的东西?
抱歉,如果这些问题看起来太简单,我刚刚开始学习 OCaml,我在网上找不到类似的东西。
我使用的是 scala 版本 2.12.3,当我在控制台代码中测试某些模式匹配时:
val d: Any = Map("1" -> "2", "3" -> 4)
d match {
case map: Map[String, Any] => println(map)
case _ => println("should not be here")
}
Run Code Online (Sandbox Code Playgroud)
我收到一些警告,例如<console>:14: warning: non-variable type argument String in type pattern scala.collection.immutable.Map[String,Any] (the underlying of Map[String,Any]) is unchecked since it is eliminated by erasure.
我在谷歌上搜索了这个警告,几乎所有的答案都被告知,scala运行时会在进行模式匹配时删除类型,并且回答这个问题似乎是合理的,但是当我使用以下代码时:
val e = Map("1" -> "2", "3" -> 4)
e match {
case map: Map[String, Any] => println(map)
case _ => println("should not be …Run Code Online (Sandbox Code Playgroud) 在《Elixir in Action》一书中,其中一个示例的一个函数颠覆了我对模式匹配的理解:
def add_entry(
%TodoList{entries: entries, auto_id: auto_id} = todo_list,
entry
) do
entry = Map.put(entry, :id, auto_id)
new_entries = HashDict.put(entries, auto_id, entry)
%TodoList{todo_list |
entries: new_entries,
auto_id: auto_id + 1
}
end
Run Code Online (Sandbox Code Playgroud)
第一个参数,%TodoList{entries: entries, auto_id: auto_id} = todo_list本书解释说“......此外,你将整个实例保存在一个todo_list变量中”
这让我很困惑,因为我认为变量绑定在“=”模式匹配运算符的左侧。有人可以帮助解释第一个参数发生了什么以及如何在函数体内使用传入值吗?
这是我的代码的简化版本。
我怎样才能避免打电话asInstanceOf(因为这是设计不佳的解决方案的味道)?
sealed trait Location
final case class Single(bucket: String) extends Location
final case class Multi(buckets: Seq[String]) extends Location
@SuppressWarnings(Array("org.wartremover.warts.AsInstanceOf"))
class Log[L <: Location](location: L, path: String) { // I prefer composition over inheritance
// I don't want to pass location to this method because it's a property of the object
// It's a separated function because there is another caller
private def getSinglePath()(implicit ev: L <:< Single): String = s"fs://${location.bucket}/$path"
def getPaths(): Seq[String] =
location match { …Run Code Online (Sandbox Code Playgroud) I've recently faced with some redos attack issues.
Explain in simple steps:
Regex denial of services: it means the attacker can put some malicious/crafted inputs to bring your server down by making it impossible to stop to finding the correct pattern, so it takes your whole CPU, and finally causing internal server error.
e.g. if you have a pattern like ^((ab)*)+$, and the attacker put a malicious input like abababababababababababababababababa it will cause a catastrophic error:

The …
Haskell 中的模式匹配是否可以用于以这种方式解构数字:
f (n + 1) = n
Run Code Online (Sandbox Code Playgroud)
我期望前体为:f 6 = 5,f 5 = 4 等。
我在这里找到了这种模式匹配的用法:https : //wiki.haskell.org/Continuation
facCPS :: a -> (a -> r) -> r
facCPS 0 k = k 1
facCPS n'@(n + 1) k = facCPS n $ \ret -> k (n' * ret)
facCPS 4 (+ 2) :: Integral a => a
Run Code Online (Sandbox Code Playgroud)
但在我的 ghci 中它不起作用:
Prelude> f (n + 1) = n :4:12: 错误:模式解析错误:n + 1
也许需要添加一些选项?我可以以某种方式使用模式匹配吗?
我想用 Python 从文本中提取 IBAN 号码。这里的挑战是,IBAN 本身可以以多种方式编写,数字之间有空格,我发现很难将其转换为有用的正则表达式模式。
我写了一个演示版本,它试图从文本中匹配所有德国和奥地利的 IBAN 号码。
^DE([0-9a-zA-Z]\s?){20}$
Run Code Online (Sandbox Code Playgroud)
我在stackoverflow上看到过类似的问题。但是,编写 IBAN 数字的不同方法以及从文本中提取这些数字的组合使我的问题很难解决。
希望你能帮我解决这个问题!
我已经开始学习 Scala。
我很惊讶下一个代码编译:
object Hello extends App {
def isOne(num: Int) = num match {
case 1 => "hello"
}
}
Run Code Online (Sandbox Code Playgroud)
例如,你不能在 Rust 中做类似的事情。
为什么 Scala 编译器不强制我为 提供默认值case?
我会说它有点不安全。
是否有任何 Scala linter 或其他东西?也许一些标志?
scala compiler-errors pattern-matching non-exhaustive-patterns
我试图将我的思想从 Java 背景(和一点 Scala 2)迁移到 Scala 3,并实现一种版本,PartialFunction.orElse但使用联合类型作为输入参数而不是交叉类型:
trait F[A, B] extends (A => B) {
def orElse[A1, B1](other: F[A1, B1]): F[A | A1, B | B1] = {
val self = this
new F[A | A1, B | B1] {
override def apply(v: A | A1): B | B1 = {
helper(v)
}
transparent inline def helper(v: A | A1): Any = {
inline v match {
case _: A => self.apply(v.asInstanceOf[A])
case _: A1 => other.apply(v.asInstanceOf[A1])
} …Run Code Online (Sandbox Code Playgroud) 如果我有一个像这样的 if 语句,我无法在正文中if (currentShape is ITable table || currentShape is AutoShape autoShape)使用tableor ,因为我收到CS0165 编译器错误。autoShape
对于带有失败的 switch 语句也是如此:
void Foo(object o)
{
switch (o)
{
case int i:
case string s:
case Guid g:
string bar = i?.ToString() ?? s?.ToString() ?? g.ToString(); // cannot use i, s, or g.
break;
}
}
Run Code Online (Sandbox Code Playgroud)
我明白为什么,但我想知道,这是模式匹配的限制,即您不能在复合if语句中使用它,或者是否有正确的方法来构造语句,以便我可以使用任一变量(例如,通过将它们初始化为null 所以我至少可以做一个空检查)?