是否可以匹配String字段?我不能使这个代码工作.
struct Foo {
x: int,
y: int,
str: String
}
pub fn main() {
let a = Foo { x: 1, y: 2 , str: "Hi".to_string()};
match a {
Foo { x: x, y: y, str: "Hi".to_string() } => println!("Found {}, {}", x, y),
_ => {}
}
}
Run Code Online (Sandbox Code Playgroud)
给出了这个错误:
<anon>:10:36: 10:37 error: expected one of `,` or `...`, found `.`
<anon>:10 Foo { x: x, y: y, str: "Hi".to_string() } => println!("Found {}, {}", x, y),
^
Run Code Online (Sandbox Code Playgroud) 我写了这个scala代码:
scala> val lists = List(1, 2, 3) :: List.empty :: List(5, 2) :: Nil
lists: List[List[Int]] = List(List(1, 2, 3), List(), List(5, 2))
scala> val x = for {
| list @ head :: _ <- lists
| } yield list.size
x: List[Int] = List(3, 2)
Run Code Online (Sandbox Code Playgroud)
这可行,但输出是一个很大的混乱.这份清单是如何反转的?
我希望输出list @ head意味着List(1, 2) :: 3
但是List(3,2)的输出有点不明原因.
我有一个内部案例类,特别是来自这个问题的事件,并且想要匹配它,包括外部对象:
class Player {
var _life = 20
def life = _life
def gainLife(life: Int) = execute(GainLife(life))
case class GainLife(life: Int) extends Event {
def execute() = _life += life
}
}
Run Code Online (Sandbox Code Playgroud)
我可以轻松编写一个替换特定玩家生活事件的效果(部分功能):
//gain twice as much life
def effect(player: Player): ReplacementEffect = {
case player.GainLife(x) => player.GainLife(x * 2)
}
Run Code Online (Sandbox Code Playgroud)
但是,对于其他玩家我不能这样做.我最接近的是:
//only you gain life
def effect2(player: Player): ReplacementEffect = {
case evt: Player#GainLife => player.GainLife(evt.life)
}
Run Code Online (Sandbox Code Playgroud)
但是1)这甚至取代了你自己的生活费用新的生活费用,2)我不能引用最初在功能中获得生命的玩家和3)我错过了直接匹配life这种方式.
这可以使用与路径无关的类型表示
case Player.GainLife(_player, life) if _player != …Run Code Online (Sandbox Code Playgroud) 对于erlang中的模式匹配字符串,哪种方法更好?
receiveString("someString") -> %doStuff;
receiveString("differentString") -> %doOtherStuff.
Run Code Online (Sandbox Code Playgroud)
要么
receiveString(A) when A == "someString" -> %doStuff;
receiveString(A) when A == "differentString" -> %doOtherStuff.
Run Code Online (Sandbox Code Playgroud)
此外,是否可以有多个when条款?
请参阅上次编辑.
为新手问题道歉.我正在尝试使用Akka.net在F#中实现一些东西.我对F#很新,我只使用了Scala的Akka.基本上我试图在Scala中实现一些非常简单的东西,即让Actor根据它接收的消息类型做不同的事情.
我的代码如下,它是对akka.net网站上提升的hello world示例的略微修改.我相信我的代码的第一个问题是它确实记录模式匹配而不是类型模式匹配,但是我无法编写没有编译错误的类型匹配...任何帮助将不胜感激.谢谢.
open Akka.FSharp
open Actors
open Akka
open Akka.Actor
type Entries = { Entries: List<string>}
let system = ActorSystem.Create "MySystem"
let feedBrowser = spawn system "feedBrowser" <| fun mailbox ->
let rec loop() = actor {
let! msg = mailbox.Receive()
match msg with
| { Entries = entries} -> printf "%A" entries
| _ -> printf "unmatched message %A" msg
return! loop()}
loop()
[<EntryPoint>]
let main argv =
feedBrowser <! "abc" // this should not blow up …Run Code Online (Sandbox Code Playgroud) 假设我的数据集如下:
John NA kaira
carry John NA
maya Sam maya
leo paty leo
tinker NA tinker
fabo leo maya
Run Code Online (Sandbox Code Playgroud)
我有另一个数据集:
John 1
carry 2
maya 3
leo 4
tinker 5
fabo 6
sam 7
paty 8
kaira 9
Run Code Online (Sandbox Code Playgroud)
我想将上表(df2)的值与第一个表(df1)匹配,所以我的最终表(df)如下所示:
1 NA 9
2 1 NA
3 7 3
4 8 4
5 NA 5
6 4 3
Run Code Online (Sandbox Code Playgroud) 鉴于我可以创建一个具有自我类型的特征并限制实现并提供对这样的成员的访问:
trait A { def a:String }
trait B { def b:String }
trait C {
self: A with B =>
lazy val c:String = a+b
}
Run Code Online (Sandbox Code Playgroud)
为什么我不能将这个概念带入模式匹配.我知道下面的方法不起作用,但是它只是缺少一些语法糖还是有限制可以防止这种情况发生?
case class AB( a:String, b:String ) extends A with B
val ab = AB("a","b")
ab match {
case ab@(A & B) => ab.a + ab.b
}
Run Code Online (Sandbox Code Playgroud)
这些不那么优雅的版本有什么用呢
ab match {
case a:A => a match {
case b:B => a.a + b.b
}
}
def checkInstanceOf(obj:AnyRef) =
obj.isInstanceOf[A] && obj.isInstanceOf[B]
ab match …Run Code Online (Sandbox Code Playgroud) 我有很多的模式匹配代码如下所示:如果有些Foo比赛,一Some的Bar返回,否则None,有大量的Foos长构造函数Bars.就像是:
y match {
case Foo1(z) => Some(Bar1(z))
case Foo2(z) => Some(Bar2(z))
case Foo3(z) => Some(Bar3(z))
case Foo4(z) => Some(Bar4(z))
case _ => None
}
Run Code Online (Sandbox Code Playgroud)
在实际代码中,箭头右侧的构造函数更复杂,并且有更多情况.
现在,为了摆脱重复的选项构造函数(Some),我可以做到:
Try(
y match {
case Foo1(z) => Bar1(z)
case Foo2(z) => Bar2(z)
case Foo3(z) => Bar3(z)
case Foo4(z) => Bar4(z)
}
).toOption
Run Code Online (Sandbox Code Playgroud)
这对我来说看起来更清晰,从语义角度看它是合理的,因为case _它实际上是一种不应该发生的情况,因此将其建模为异常似乎是合理的.请注意,重复Somes是我的错误,而不是最后一次case.
我的问题是,对于我不知道的后一种方法是否存在某些(例如性能)惩罚.
我试图找到一个矩阵的转置,我知道我的代码到目前为止是不正确的但是我更关心的是为什么当我用transpose测试它时[[4;3];[1;5];[6;7]]它会进入第一个匹配的情况,这是一个空列表?
let rec transpose = function
| [] -> failwith "Error, no matrix supplied"
| [[]] -> []
| [[x]] -> [[x]]
| x::xs -> let temp = List.head x :: (List.map (fun n -> List.headn) xs)
temp :: transpose(xs);;
Run Code Online (Sandbox Code Playgroud) 我有一些字符串:
[POS Purchase]
POS Signature Purchase International SKYPE COMMUNICATIO, LUXEMBOURG, LUX
Run Code Online (Sandbox Code Playgroud)
要么:
ATM Cash Withdrawal. Surcharge: -3.0 BNEAIR INT DP LSL4 2, BNE AIRPORT, AUS
Run Code Online (Sandbox Code Playgroud)
我希望得到任何字符但小写字母的字符串的结尾.对于上面的两个例子,答案应该是:
SKYPE COMMUNICATIO, LUXEMBOURG, LUX
Run Code Online (Sandbox Code Playgroud)
和
BNEAIR INT DP LSL4 2, BNE AIRPORT, AUS
Run Code Online (Sandbox Code Playgroud)
如何通过正则表达式实现此目的?