- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Scala 语言规范部分 6.19 说:
A for comprehension
for (p <- e) yield e0
is translated toe.map { case p => e0 }
所以...
scala> val l : List[Either[String, Int]] = List(Left("Bad"), Right(1))
l: List[Either[String,Int]] = List(Left(Bad), Right(1))
scala> for (Left(x) <- l) yield x
res5: List[String] = List(Bad)
到目前为止还不错:
scala> l.map { case Left(x) => x }
<console>:13: warning: match is not exhaustive!
missing combination Right
l.map { case Left(x) => x }
^
scala.MatchError: Right(1)
at $anonfun$1.apply(<console>:13)
at ...
为什么第二个版本不行?或者更确切地说,为什么第一个版本有效?
最佳答案
如果您在 for
中使用模式匹配 - 理解,编译器实际上会在应用之前插入一个带有 instanceOf
的对 filter
的调用 - 检查 map
。
编辑:
还在第 6.19 节中说:
A generator p <- e followed by a guard if g is translated to a single generator p <- e.withFilter((x1, ..., xn) => g ) where x1, ..., xn are the free variables of p.
生成器之前定义为:
Generator ::= Pattern1 ‘<-’ Expr [Guard]
检查字节码时,您会看到在调用 map
之前调用了 filter
。
关于Scala for comprehensions 和 partial map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3344404/
我编写了一个函数,使用 CFFI 将 numpy 数组传递给 C 代码。它利用缓冲协议(protocol)和内存 View 有效地传递数据而无需复制数据。但是,这意味着您需要传递 C 连续数组并确保使
如何使用 for-comprehension 返回我可以分配给有序 Map 的内容?这是我拥有的代码的简化: class Bar class Foo(val name: String, val bar
我正在使用 docjure并且它的 select-columns 函数需要一个列映射。我想获取所有列而无需手动指定。 如何将以下内容生成为惰性无限向量序列 [:A :B :C :D :E ... :A
下一个代码 def f(chars: List[Char]): List[List[Char]] = chars match { case Nil => List(Nil) cas
在我的代码中有以下两种方法: public void AllDivisors(int max) { lrel[int,list[int]] div = [ | int i "); } pu
在下面的代码中,在 for 里面理解,我可以使用元组取消引用来引用字符串和索引: val strings = List("a", "b", "c") for (stringWithIndex val
我有个问题。 我有一个看起来像这样的嵌套列表。 x= [[{'screen_name': 'BreitbartNews', 'name': 'Breitbart News', 'id
来自Python,我喜欢Coffeescript从Python和Perl借来的许多功能(范围/ slice ,理解,破坏性分配)。 Coffeescript中是否有任何语法糖可以模仿Python的en
这是我需要在 Chapel 中重新创建的类似 Python 的模式。 class Gambler { var luckyNumbers: [1..0] int; } var nums = [13,
我有这段代码和一些讨厌的嵌套检查...... 我很确定它可以用一个很好的理解重写,但我对如何混合模式匹配的东西有点困惑 // first tries to find the token in a he
我如何定义 lists:append具有列表理解功能? 我想要类似的东西 1> append([[1, 2, 3], [a, b], [4, 5, 6]]). [1,2,3,a,b,4,5,6] 最佳
我正在尝试创建一个简洁的结构,对基于 future 的业务逻辑进行理解。这是一个示例,其中包含基于异常处理的工作示例: (for { // find the user by id, findUse
例如,给定输入 n = 3 我想获得: [(1,),(2,),(3,),(1,2),(1,3),(2,3),(1,2,3)] 我尝试了类似 python 的语法: combs = [c
考虑这个 Map[String, Any] : val m1 = Map(("k1" -> "v1"), ("k2" -> 10)) 现在让我们写一个 for : scala> for ((a, b)
如何将此map / flatMap转换为,以进行理解,谢谢。 def compute2(maybeFoo: Option[Foo]): Option[Int] = maybeFoo
是否可以在 Scala 中专门研究 if 中的条件?在一个理解?我正在考虑以下方面: val collection: SomeGenericCollection[Int] = ... trait Co
我正在尝试使用 Julia 中的新 walkdir 函数获取文件列表。以下内容有效,但我希望结果是一个简单的文件列表。这可以通过数组理解来实现,而无需在创建数组后将其展平吗? files = [[
我在使用 Scalaz 和 Task/EitherT 的 Scala 中有以下代码片段: def myMethod(request: MyRequest) : EitherT[Task, Failur
我目前正在做 Wiki 上的 99 个 Haskell 问题。话虽如此,我正在做第 26 题,它需要一个程序,该程序可以用给定集合中的 n 个元素编写所有组合。我用很多函数完成了我的实现,程序看起来是
给定以下类型: sealed trait Pet { val name: String } case class Dog(override val name: String) extends Pe
我是一名优秀的程序员,十分优秀!