你好我试图模式匹配的外卡模式,但我需要"抢"了wild card.我正在尝试解析String到list的Maybe Int.
INPUT {123,aa,55,66}
OUTPUT [Just 123,Nothing,Just 55.4,Just 66.3]
所以我把以下方法放在一起:
方法
readDataRaw::String->[Maybe Int]
readDataRaw (x:xs)=go [] [](x:xs) where
go _ ls [] = ls
go small big (x:xs)= case x of
'}' -> big
',' -> go [] ((readMaybe small::Maybe Int):big) xs
c@(_) -> go [] c:small big xs
Run Code Online (Sandbox Code Playgroud)
错误
parse error on input `->'
|
66 | c@(_) -> go [] c:small big xs
| ^^
Run Code Online (Sandbox Code Playgroud)
如何-> …
假设一种模式:
pattern P :: [Int]
pattern P <- a:_
Run Code Online (Sandbox Code Playgroud)
我可以以某种方式使用a该功能f吗?
f :: [Int] -> Int
f P = a
Run Code Online (Sandbox Code Playgroud)
上面的代码生成错误Not in scope: 'a'.
以下没有编译:
use std::any::Any;
pub trait CloneBox: Any {
fn clone_box(&self) -> Box<dyn CloneBox>;
}
impl<T> CloneBox for T
where
T: Any + Clone,
{
fn clone_box(&self) -> Box<dyn CloneBox> {
Box::new(self.clone())
}
}
struct Foo(Box<dyn CloneBox>);
impl Clone for Foo {
fn clone(&self) -> Self {
let Foo(b) = self;
Foo(b.clone_box())
}
}
Run Code Online (Sandbox Code Playgroud)
错误信息:
error[E0495]: cannot infer an appropriate lifetime for pattern due to conflicting requirements
--> src/lib.rs:20:17
|
20 | let Foo(b) = self;
| ^
|
note: …Run Code Online (Sandbox Code Playgroud) 遇到这个例子:
buildEntry ws@(w:_) = (w, length ws)
Run Code Online (Sandbox Code Playgroud)
使用ws@(w:_)代替的好处是什么:
buildEntry' (w:ws) = (w, length (w:ws))
Run Code Online (Sandbox Code Playgroud)
由于我只是一个初学者,因此我认为第二个示例的可读性也更好。
因此,在其中一个地方,我们variable match case发表了如此宏伟的声明。
包含近150个不同的case语句
看起来很恐怖。
我想将其分解为较小的函数,可以将匹配项分成10个部分,然后将数字case语句减少到15个左右。这很好。
目前看起来像这样
massiveCaseVariable match {
case "one" => 1
case "two" => 2
case "three" => 3
case "four" => 4
case "five" => 5
case "six" => 6
}
Run Code Online (Sandbox Code Playgroud)
但是我不想这样
massiveCaseVariable match {
case "one" || "two" || "three" => firstCategory(massiveCaseVariable)
case "four" || "five" || "six" => secondCategory(massiveCaseVariable)
}
def firstCategory(caseVariable: String): Int =
caseVariable match {
case "one" => 1
case "two" => 2
case "three" …Run Code Online (Sandbox Code Playgroud) I need to write a function that takes a List such as ("1","2","3") and return every other element in that list into a new list using pattern matching. What would be the correct case statement to get the head element of the list and then find every other element.
def everyOther[A](list: List[A]): List[A] =
list match {
case _ => Nil
case x::xs =>
}
Run Code Online (Sandbox Code Playgroud)
It should return a new list of every 2nd element starting from the head element
我正在阅读“第2部分急躁的scala”第14.4节,对上下文感到困惑:
您可以匹配表达式的类型,例如:
obj match {
case x:Int => x
case s:String => Integer.parseInt(s)
case _:BigInt => Int.MaxValue
case _ => 0
}
Run Code Online (Sandbox Code Playgroud)
与类型匹配时,必须提供变量名称。否则,您匹配对象:
obj match {
case _:BigInt => Int.MaxValue // Matches any object of type BigInt
case BigInt => -1 // Matches the BigInt object of type Class
}
Run Code Online (Sandbox Code Playgroud)
我很困惑的是,如果我进行如下测试,那么如何理解obj哪个请求是expression:
val x = 121
val obj : Any = x
obj == 121 // true
obj match {
case x:Int => x
case s:String => …Run Code Online (Sandbox Code Playgroud) 我试图了解如何在Erlang中对单个元素vs列表进行模式匹配:
guarded(T) when T>5 ; T<3 -> 3+T;
guarded([X,Y]) when X>3,Y>3 ->{X+1,Y+1};
guarded([X,_|[T,_]]) when X rem 2==0, T rem 2 =/= 1-> [T,X];
guarded(_)->"something else".
guarded([1,2,3]). -> goes into case 1 , how can i make sure it doesn't (and goes to last case)
Run Code Online (Sandbox Code Playgroud)
**异常错误:在函数main:guarded / 1中计算算术表达式时发生错误
当我想在单个元素上进行模式匹配时,我需要在哪里放置第一个大小写。我的意思是我也想拥有一个适用于单个元素和通配符模式(最后一个表达式)的情况。
我有以下正则表达式,我想在Scala 2.13中进行模式匹配。
正则表达式:
\/brokers\/ids\/\d{1,}$
Run Code Online (Sandbox Code Playgroud)
以下字符串将被验证:
scala> ("echo dump" #| "nc localhost 32773" #| "grep brokers").!!
res2: String =
" /brokers/ids/1
"
Run Code Online (Sandbox Code Playgroud)
我如何在Scala 2.13中做到这一点?
为什么Rust有ref关键字?可以
match value.try_thing() {
&Some(ref e) => do_stuff(e),
// ...
}
Run Code Online (Sandbox Code Playgroud)
不能由
match value.try_thing() {
&Some(e) => do_stuff(&e),
// ...
}
Run Code Online (Sandbox Code Playgroud) pattern-matching ×10
scala ×4
haskell ×3
rust ×2
erlang ×1
expression ×1
lifetime ×1
match ×1
refactoring ×1
scala-2.13 ×1
wildcard ×1