码:
case class Division(val number: Int) {
// def unapply(divider: Int): Option[(Int, Int)] = if (number % divider == 0) Some(number/divider, 0) else None
// def unapply(divider: Double): Boolean = number % divider.toInt == 0
def unapplySeq(x: Float): Option[Seq[Int]] = {
val seq = (3 to 10).map(i => i * x.toInt)
println(seq)
Some(seq)
}
}
val divisionOf15 = Division(15)
// val y = 5 match {
// case divisionOf15(z, w) => println(s"$z, $w")
// case _ => println(s"Not divisible")
// …Run Code Online (Sandbox Code Playgroud) 我试图在Swift中返回我自己实现的双链接列表的head元素.我的节点被声明为这样的枚举:
enum DLLNode<Element>{
indirect case Head(element: Element, next: DLLNode<Element>)
indirect case Node(prev: DLLNode<Element>, element: Element, next: DLLNode<Element>)
indirect case Tail(prev: DLLNode<Element>, element: Element)
}
Run Code Online (Sandbox Code Playgroud)
和列表实现如下:
struct DLList<Element> {
var head:DLLNode<Element>?
...
func getFirst()throws->Element{
if self.isEmpty(){
throw ListError.EmptyList(msg: "")
}
guard case let DLLNode<Element>.Head(element: e, next: _) = head
else{
throw ListError.UnknownError(msg: "")
}
return e
}
}
Run Code Online (Sandbox Code Playgroud)
但我正在接受"Invalid pattern"警卫声明.如果我省略DLLNode<Element>并保持它就像guard case let .Head(element: e, next: _) = head它给我"Enum case 'Head' not found in 'guard …
对于简单的情况,if let或者guard我没有看到优势,
if case let x? = someOptional where ... {
...
}
//I don't see the advantage over the original if let
if let x = someOptional where ... {
...
}
Run Code Online (Sandbox Code Playgroud)
对于for-case-let简化使用可选集合的情况,我真的希望swift可以更进一步:
for case let x? in optionalArray {
...
}
//Wouldn't it be better if we could just write
for let x? in optionalArray {
...
}
Run Code Online (Sandbox Code Playgroud)
谷歌一段时间之后我认为唯一有用的案例是" Swift 2模式匹配:解开多个选项 ":
switch (username, password) {
case let (username?, password?):
print("Success!")
case …Run Code Online (Sandbox Code Playgroud) 似乎应该有一个"更快"的方式来做到这一点 - 但我仍然围绕着在Swift中进行模式匹配的各种方式.
假设我有一个AnyObject数组,我想循环遍历它,如果该项是Int并且Int是5的倍数,那么我想将它打印出来.这是我最初的方法:
let myStuff: [AnyObject] = [5, "dog", 11, 15, "cat"]
for item in myStuff {
if let myInt = item as? Int where myInt % 5 == 0 {
print ("\(item)")
}
}
Run Code Online (Sandbox Code Playgroud)
老实说,这还不错......但是使用Swift的所有模式匹配语法,似乎我应该能够将逻辑合并到1行.到目前为止,我还没有找到一种有效的方法 - 但我希望能够做到这样的事情:
//This doesn't work, but I feel like something similar to this should
for item in myStuff where item is Int, item % 5 == 0 {
print ("\(item)")
}
Run Code Online (Sandbox Code Playgroud)
显然,这不是一个大问题 - 但对于我来说,理解斯威夫特的模式匹配更好一点是一种思考练习.
在Makefile中我有:
images/schematic.pdf:images/schematic.svg
Run Code Online (Sandbox Code Playgroud)inkscape -D -z --file=$^ --export-pdf=$@ --export-latex sed -i "s:schematic:images/schematic:g" $@_tex
这条规则的作用是:
.pdfPDF文件及其相应的.pdf_tex文本文件(请参阅此答案:https://tex.stackexchange.com/a/2107/104581).pdf_tex文件,以便它不会破坏"更高"目录中的乳胶编译(即.当.pdf_tex文件在时./images)我有很多这种形式的规则,即只有schematic改变的地方.我想用一个模式规则替换schematic的%.并%在配方中使用(在sed命令中).
但是规则:
images /%.pdf:images /%.svg
Run Code Online (Sandbox Code Playgroud)inkscape -D -z --file=$^ --export-pdf=$@ --export-latex sed -i "s:%:images/%:g" $@_tex
不起作用:%在食谱中按字面解释.
我还尝试用$%替换配方中的%,但这个变量似乎是空的.
在配方中添加一行来创建一个(make)变量,该变量将保存结果notdir(removeprefix($<))(使用此问题或调用bash,因为GNU Make中没有removeprefix).
switch语句中的这个错误的原因是什么?"h必须绑定在每个模式中"?
我基本上试图将h用作小时变量,确保它不是nil(因为小时最初是一个可选值,然后看它是否大于17).我知道我在某个地方做错了什么,但那个讨厌的小'必须在每个模式中绑定'错误是什么?
谢谢
let date = NSDate()
let calendar = Calendar.current
let components = calendar.dateComponents([.hour], from: date as Date)
let hour = components.hour
switch hour {
case let h, (h as Int) != nil, h >= 17:
return true
default:
return false
}
Run Code Online (Sandbox Code Playgroud) 此代码显示在Rust编程语言中:
#![feature(box_syntax, box_patterns)]
fn main() {
let b = Some(box 5);
match b {
Some(box n) if n < 0 => {
println!("Box contains negative number {}", n);
}
Some(box n) if n >= 0 => {
println!("Box contains non-negative number {}", n);
}
None => {
println!("No box");
}
_ => unreachable!(),
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我运行它时,会发生以下错误:
error[E0554]: #[feature] may not be used on the stable release channel
Run Code Online (Sandbox Code Playgroud)
我也试过了
fn main() {
let b = Some(box 5);
}
Run Code Online (Sandbox Code Playgroud)
error: …Run Code Online (Sandbox Code Playgroud) 我是NLP和Python的新手。我正在尝试使用Tregex工具和Python子进程库从StanfordCoreNLP的已解析树中提取名词短语的子集。特别是,我试图查找和提取与以下模式匹配的名词短语:'(NP [$ VP]> S)|(NP [$ VP]> S \ n)|(NP \ n [$ VP] > S)|(NP \ n [$ VP]> S \ n)'在Tregex语法中。
例如,下面是原始文本,保存在名为“ text”的字符串中:
text = ('Pusheen and Smitha walked along the beach. "I want to surf", said Smitha, the CEO of Tesla. However, she fell off the surfboard')
Run Code Online (Sandbox Code Playgroud)
使用Python包装器运行StanfordCoreNLP解析器后,我为这3个句子得到了以下3棵树:
output1['sentences'][0]['parse']
Out[58]: '(ROOT\n (S\n (NP (NNP Pusheen)\n (CC and)\n (NNP Smitha))\n (VP (VBD walked)\n (PP (IN along)\n (NP (DT the) (NN beach))))\n (. .)))'
output1['sentences'][1]['parse']
Out[59]: "(ROOT\n (SINV (`` ``)\n …Run Code Online (Sandbox Code Playgroud) 我刚刚开始学习Elixir,并坚持使用Integer的模式匹配.
我很清楚如何匹配二进制,但我找不到如何匹配整数,即从一个简单的整数提取高字节.我必须将Integer转换为二进制或者编写一个从Integer获取高字节的函数,但在库中找不到任何内容.
<<y1::size(8), y2::size(8), y3::size(8), y4::size(8) >> = t
Run Code Online (Sandbox Code Playgroud)
其中t是整数,你可能会猜到
** (MatchError) no match of right hand side value: 3232235521
Run Code Online (Sandbox Code Playgroud) 我发现为了模式匹配Futurefur Success/ Failure,我需要使用andThen(或onComplete,onSuccess...)而不能使用map.这是为什么?
我想做什么(简化,我匹配Success等等):
val f1 = Future(throw new Exception("Oops"))
f1 map { case Failure(e) => ??? }
Run Code Online (Sandbox Code Playgroud)
得到:
error: constructor cannot be instantiated to expected type;
found : scala.util.Failure[T]
required: Nothing
f1 map { case Failure(e) => ??? }
Run Code Online (Sandbox Code Playgroud)
我最终做了什么:
val f1 = Future(throw new Exception("Oops"))
f1 andThen { case Failure(e) => ??? }
Run Code Online (Sandbox Code Playgroud)
我想明白为什么map不能在这里使用.
pattern-matching ×10
swift ×4
scala ×2
boxing ×1
code-cleanup ×1
elixir ×1
enums ×1
future ×1
gnu-make ×1
makefile ×1
optional ×1
parsing ×1
python ×1
rust ×1
stanford-nlp ×1
subprocess ×1
swift2 ×1