我今天第一次尝试 Rust(编写 XML 标记器),自然不明白一切:
我有一个带有可以采用枚举值的字段的结构:
enum State { Outside, InATag(~str) }
struct Tokenizer { state: State }
Run Code Online (Sandbox Code Playgroud)
在 a 中impl Tokenizer,我想匹配当前状态,并在某些情况下更改它,但是这总是会use of moved value出错。
H 访问和/或声明状态字段,以便我可以匹配它并在匹配分支内更改其值?
抱歉造成混淆,我的意思是更改 Tokenizer 的 state 字段,而不是 state 的 String 字段!
match self.state {
InATag(name) => self.state = Outside,
Outside => ()
}
Run Code Online (Sandbox Code Playgroud) 我想知道是否有办法在Octave/matlab中进行模式匹配?我知道Maple 10有命令这样做但不确定我需要在Octave/Matlab中做什么.因此,如果一个数字是12341234123412341234模式匹配将是1234.我试图找到重复生成整个字符串的最短模式.
请注意:数字(仅使用数字)不会这么简单.另外,我不会提前知道这个模式(这就是我想要找到的).请参阅下面的Maple 10示例,该示例显示模式未提前知道,但命令找到模式.
Maple 10模式匹配示例:
ns:=convert(12341234123412341234,string);
ns := "12341234123412341234"
StringTools:-PrimitiveRoot(ns);
"1234"
Run Code Online (Sandbox Code Playgroud)
我如何在Octave/Matlab中执行此操作?Ps:我正在使用Octave 3.8.1
我来自C#,发现自己爱上了F#模式匹配语法,因为它比C#更简单switch,更有用.我喜欢尽可能地使用它,是否有表现或任何其他缺点,以这种例子中的奇怪方式使用它?
match 0 with
|_ when a<b -> a
|_ -> b
Run Code Online (Sandbox Code Playgroud) 我正在尝试仅为块计算HOG功能.我hog.cpp在下面探讨过opencv/module/gpu/src/.下面是我只更改了计算机块功能的代码.
void cv::gpu::HOGDescriptor::getDescriptors(const GpuMat& img, Size win_stride, GpuMat& descriptors, int descr_format)
{
CV_Assert(win_stride.width % block_stride.width == 0 && win_stride.height % block_stride.height == 0);
computeBlockHistograms(img);
// give block back
/*
const size_t block_hist_size = getBlockHistogramSize();
Size blocks_per_win = numPartsWithin(win_size, block_size, block_stride);
Size wins_per_img = numPartsWithin(img.size(), win_size, win_stride);
descriptors.create(wins_per_img.area(), static_cast<int>(blocks_per_win.area() * block_hist_size), CV_32F); */
switch (descr_format)
{
case DESCR_FORMAT_ROW_BY_ROW:
hog::extract_descrs_by_rows(win_size.height, win_size.width, block_stride.height, block_stride.width,
win_stride.height, win_stride.width, img.rows, img.cols, block_hists.ptr<float>(), descriptors);
break;
case DESCR_FORMAT_COL_BY_COL:
hog::extract_descrs_by_cols(win_size.height, win_size.width, block_stride.height, block_stride.width,
win_stride.height, win_stride.width, …Run Code Online (Sandbox Code Playgroud) c++ opencv pattern-matching object-detection computer-vision
在Scala 2.10,编译器警告给定的代码:
private def getStrFromOpt[T](opt: Option[T]): String = opt match {
case Some(s: String) => s
case Some(i: Int) => i.toString()
case Some(l: Long) => l.toString()
case Some(m: Map[String, Int]) => m map ({ case (k, v) =>
"(" + k + ", " + v + ")" }) mkString ("(", ", ", ")")
case _ => ""
}
Run Code Online (Sandbox Code Playgroud)
随着消息non-variable type argument String in type pattern Map[String,Int] is unchecked since it is eliminated by erasure: case Some(m: …
假设我们已经定义了一个存在类型:
type T = (X => X, X) forSome { type X }
Run Code Online (Sandbox Code Playgroud)
然后定义了一个类型列表List[T]:
val list = List[T](
((x: Int) => x * x, 42),
((_: String).toUpperCase, "foo")
)
Run Code Online (Sandbox Code Playgroud)
list.map{ x => x._1(x._2) }
Run Code Online (Sandbox Code Playgroud)
但是,为什么以下工作?:
list.map{ case x => x._1(x._2) }
Run Code Online (Sandbox Code Playgroud)
请注意,两个链接问题的答案都假定模式匹配中需要一个类型变量,但它也可以在没有类型变量的情况下工作。问题的重点更多是为什么{ case x => ... }工作?.
types scala type-inference pattern-matching existential-type
考虑这个 GHCi 会话:
$ ghci
GHCi, version 8.6.5: http://www.haskell.org/ghc/ :? for help
Prelude> :set -Wincomplete-patterns -Wincomplete-uni-patterns
Prelude> foo t | (_, _) <- t = "Foo"
Prelude> bar t | (_, _) <- id t = "Foo"
Prelude> baz x | Just _ <- x = "Yes" | Nothing <- x = "No"
Prelude> qux x | Just _ <- id x = "Yes" | Nothing <- id x = "No"
<interactive>:3:1: warning: [-Wincomplete-patterns]
Pattern match(es) are non-exhaustive
In an …Run Code Online (Sandbox Code Playgroud) 这是我的代码的简化版本。
我怎样才能避免打电话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) 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 数字的不同方法以及从文本中提取这些数字的组合使我的问题很难解决。
希望你能帮我解决这个问题!