标签: pattern-matching

为什么Scala 2.10在匹配单例类型时会给出"匹配可能不是详尽无援"的警告?

在Scala 2.10.0-M4中

object X
def f(e: Either[Int, X.type]) = e match {
  case Left(i) => i
  case Right(X) => 0
}
Run Code Online (Sandbox Code Playgroud)

得到:

warning: match may not be exhaustive.
It would fail on the following input: Right(<not X>)
Run Code Online (Sandbox Code Playgroud)

它是否正确?当然这场比赛实际上是详尽无遗的.

(同时,回到Scala 2.9.X我们得到了

error: pattern type is incompatible with expected type;
 found   : object X
 required: X.type
           case Right(X) => 0
Run Code Online (Sandbox Code Playgroud)

这可能是一个错误.)

singleton scala pattern-matching non-exhaustive-patterns scala-2.10

2
推荐指数
1
解决办法
1588
查看次数

Scala匹配错误与unapply

我正在尝试http://www.scala-lang.org/node/112上的代码,我得到一个匹配错误,看起来不应该抛出一个.

这是原始代码:

object Twice {                              
  def apply(x: Int): Int = x * 2
  def unapply(z: Int): Option[Int] = if (z%2 == 0) Some(z/2) else None
}

object TwiceTest extends Application {
  val x = Twice(21)
  x match { case Twice(n) => Console.println(n) } // prints 21
}
Run Code Online (Sandbox Code Playgroud)

我刚刚添加了几行来测试当我传递奇数时会发生什么:

object TwiceTest extends Application {
  val x = Twice(21)
  x match { case Twice(n) => Console.println(n) } // prints 21
  val y = 21
  y match { case Twice(n) => …
Run Code Online (Sandbox Code Playgroud)

scala pattern-matching unapply

2
推荐指数
1
解决办法
1626
查看次数

Erlang二进制模式匹配失败

为什么会出现badmatch错误?我无法弄清楚为什么会失败:

<<IpAddr, ":*:*">> = <<"2a01:e34:ee8b:c080:a542:ffaf:*:*">>.
Run Code Online (Sandbox Code Playgroud)

binary erlang pattern-matching binaries

2
推荐指数
2
解决办法
1602
查看次数

Perl多次模式匹配和一次性替换

09/27/2009 19:48:00 Departure Location
Run Code Online (Sandbox Code Playgroud)

我试图匹配并替换文本文件中的给定行.日期和时间之后的文本长度可以变化.我正在逐行读取文件,我需要将最终输出打印为 -

Date=> 09/27/2009
Time=> 19:48:00 
Text=> Departure Location
Run Code Online (Sandbox Code Playgroud)

我试图在一次通过中进行如下替换 -

if($line =~ m/(\d+)\/(\d+)\/(\d+)\h{1}(\d+):(\d+):(\d+)/){

    $line =~ s/(\[a-zA-Z])/\nText=> $1/;
    $line =~ s/(\d+)\/(\d+)\/(\d+)/\nDate=> $1\/$2\/$3/;
    $line =~ s/\h{1}(\d+):(\d+):(\d+)/\nTime=> $1\:$2\:$3/;

    print FH "$line\n";

}
Run Code Online (Sandbox Code Playgroud)

但我得到的只是 -

Date=> 09/27/2009
Time=> 19:48:10 Departure Location
Run Code Online (Sandbox Code Playgroud)

我知道匹配时存在问题,Text但我无法修复它.我仍然是Perl的初学者.任何帮助表示赞赏.谢谢!

regex perl pattern-matching

2
推荐指数
1
解决办法
4239
查看次数

带加权滤波器的字符串模式识别算法?

是否有模式识别算法,我可以指定匹配或丢失某些参数的权重?例如,假设我有3个字符串:

str1 = Samsung 11.6" 64GB Slate PC Tablet with Wi-Fi - Black  
str2 = Samsung Series 7 XE700T1A-A05US 11.6-Inch Slate (64 GB, Win 7 Pro)
str3 = Samsung Series 7 XE700T1A-A03US 11.6-Inch Slate (128 GB SSD, Win 7 HP)
Run Code Online (Sandbox Code Playgroud)

我想将str2与str1匹配,因为它们具有相等的GB,即使传统的字符串距离会说str2更接近str3.实际上,我希望能够处理大量具有不同权重的参数.

任何指向正确方向的指针都将受到赞赏.

algorithm pattern-recognition machine-learning pattern-matching

2
推荐指数
1
解决办法
2848
查看次数

将字符串与存储的变量列表进行匹配

使用JS/jQuery,最好的方法是什么?

我有5个变量:

var fruit1 = "apple";
var fruit2 = "mango";
var fruit3 = "orange";
var fruit4 = "banana";
var fruit5 = "melon";
Run Code Online (Sandbox Code Playgroud)

然后我有一个单击某个水果的列表元素:

<li id="banana">Banana</li>
Run Code Online (Sandbox Code Playgroud)

我在脚本中得到了它的ID:

$("li").on("click", function() {
    var selectedfruit = $(this).attr("id");
});
Run Code Online (Sandbox Code Playgroud)

如何selectedfruit与变量列表匹配,以便返回fruit4我可以执行的操作?

次要问题,我应该将变量列表放在数组中吗?

非常感谢.

编辑:我很抱歉,但我犯了一个大错

我需要selectedfruit使用变量名来验证,而不是变量内容.

所以,标记将是这样的:

<li id="fruit4">Mystery fruit</li>

variables jquery pattern-matching

2
推荐指数
1
解决办法
268
查看次数

如何表达独立的数据值组合的模式匹配?

data Foo = Bar1
         | Bar2 Foo Foo
         | Bar3 Foo
         | Bar4 Foo Foo Foo
Run Code Online (Sandbox Code Playgroud)

现在,假设有人构建了一个Foo树,我想检查Foo值的参数是否有效.构造函数参数的规则是:

  • Bar2 Bar1 Foo
  • Bar3 (Bar2|Bar3|Bar4)
  • Bar4 Bar1 Bar1 (Bar1|Bar4)

我知道值的构造函数,只想检查立即参数,没有任何递归.像这样:

bar2 Bar1 Bar1      = True
bar2 Bar1 (Bar2 {}) = True
bar2 Bar1 (Bar3 _)  = True
bar2 Bar1 (Bar4 {}) = True
bar2 _ _ = False
Run Code Online (Sandbox Code Playgroud)

例如Bar4:

bar4 Bar1 Bar1 Bar1      = True
bar4 Bar1 Bar1 (Bar4 {}) = True
bar4 _ _ _ = False
Run Code Online (Sandbox Code Playgroud)

我怎样才能最简洁地表达这些条件?列出所有组合在某些情况下有点太多了.据我所知,不存在用于模式匹配的"OR"-syntax.

UPDATE

我改编了Daniel的解决方案并且已经解决了这个问题: …

haskell pattern-matching generic-programming gadt

2
推荐指数
1
解决办法
148
查看次数

Scala模式匹配将int与列表进行比较

我正在尝试编写一个递归程序,用于将int与列表中的每个值进行比较.问题是我一直收到无法访问的错误,我真的不知道为什么.我的代码是

def isIn(x : Int, l : List[Int]) : Boolean = l match {
  case Nil => false
  case x => true
  case h :: t => isIn(x, t)
}
Run Code Online (Sandbox Code Playgroud)

我真的不明白为什么这不起作用.或者我想,我想知道如何将x与头部用例进行比较.

scala pattern-matching

2
推荐指数
1
解决办法
527
查看次数

封装GADT模式匹配

在这段代码中有重复的片段:

insert x (AATree t) = case insert' x t of
    Same t -> AATree t
    Inc t  -> AATree t

insertBlack :: (Ord a) => a -> AANode Black (Succ n) a -> AnyColor (Succ n) a
insertBlack x (Black l y r)
    | x < y     = case insert' x l of
          Same l' -> AnyColor $ Black l' y r
          Inc  l' -> AnyColor $ skew l' y r
    | otherwise = case insert' x …
Run Code Online (Sandbox Code Playgroud)

refactoring haskell pattern-matching gadt

2
推荐指数
1
解决办法
208
查看次数

模式匹配scala中列表的语法说明

我正在阅读这篇博文,我无法理解代码的一部分.

object O {
  def maximum(x: List[Int]): Int = x match {
  case Nil => error("maximum undefined for empty list")
  case x :: y :: ys => maximum((if(x > y) x else y) :: ys)
  case x :: _ => x
 }
}
Run Code Online (Sandbox Code Playgroud)

请解释一下代码 maximum((if(x > y) x else y) :: ys)

if条件如何成为方法的一部分maximum?
我明白,如果条件不是一个参数.

scala pattern-matching

2
推荐指数
1
解决办法
182
查看次数