【问题标题】:scala newbie having troubles with tuples and closuresscala 新手遇到元组和闭包问题
【发布时间】:2012-04-16 09:45:43
【问题描述】:

我有一个元组列表,我想遍历并获取每个元素的值。

这是代码:

scala> val myTuples = Seq((1, "name1"), (2, "name2"))
myTuples: Seq[(Int, java.lang.String)] = List((1,name1), (2,name2))

scala> myTuples.map{ println _ }
(1,name1)
(2,name2)
res32: Seq[Unit] = List((), ())

到目前为止,还不错,但是

scala> myTuples.map{ println _._1 }
<console>:1: error: ';' expected but '.' found.
    myTuples.map{ println _._1 }

我也试过了:

scala> myTuples.map{ println(_._1) }
<console>:35: error: missing parameter type for expanded function ((x$1) => x$1._1)
          myTuples.map{ println(_._1) }

scala> myTuples.map{ val (id, name) = _ }
<console>:1: error: unbound placeholder parameter
       myTuples.map{ val (id, name) = _ }

scala> myTuples.map{ x => println x }
<console>:35: error: type mismatch;
 found   : Unit
 required: ?{val x: ?}
Note that implicit conversions are not applicable because they are ambiguous:
 both method any2Ensuring in object Predef of type [A](x: A)Ensuring[A]
 and method any2ArrowAssoc in object Predef of type [A](x: A)ArrowAssoc[A]
 are possible conversion functions from Unit to ?{val x: ?}
              myTuples.map{ x => println x }

声明变量并使用括号可以解决问题,但我想知道为什么其他选项不起作用

这些都很好

myTuples.map{ x => println("id: %s, name: %s".format(x._1, x._2)) }

scala> myTuples.map{ x => println("id: %s, name: %s".format(x._1, x._2)) }
id: 1, name: name1
id: 2, name: name2
res21: Seq[Unit] = List((), ())

scala> myTuples.map{ x => val(id, name) = x; println("id: %s, name: %s".format(id, name)) }
id: 1, name: name1
id: 2, name: name2
res22: Seq[Unit] = List((), ())

scala> myTuples.map{ x => println(x._1) }

在我使用 scala 的第一步中,发生在我身上的是,坚持一点点你得到你想要的,但你不确定为什么你尝试的第一个选项不起作用......

【问题讨论】:

  • FWIW, map 用于转换集合的内容。由于您不关心结果,因此foreach 是首选方法。
  • 不错的提示,也让开发者的意图更加清晰

标签: scala closures tuples


【解决方案1】:

对于不起作用的选项:

scala> myTuples.map{ println _._1 }

简短回答:在 Scala 中,您总是需要在 println 的参数周围使用括号。 长答案: Scala 仅推断中缀方法的括号,这意味着 object method argument 形式的代码被解释为 object.method(argument)。没有指定对象,因此不会推断出括号。您可以通过以下方式直接查看:

scala> println "Boom!"
<console>:1: error: ';' expected but string literal found.
       println "Boom!"
               ^

接下来,myTuples.map{ println(_._1) }我并不清楚为什么这不起作用,因为这应该等效于 myTuples.map{ x =&gt; println(x._1) },它有效。 正如the answers to this question 所示,占位符/部分应用的方法语法适用于尽可能小的范围。所以等效代码是myTuples.map { println(x =&gt; x._1) }。由于 scala 没有足够的信息来推断 x 的类型,因此您会收到“缺少参数类型”错误。

关于myTuples.map{ val (id, name) = _ },占位符用于匿名函数,而这里你正在初始化val

那么对于myTuples.map{ x =&gt; println x },你也缺少括号。

最后,为您工作的选项myTuples.map{ x =&gt; println("id: %s, name: %s".format(id, name)) },实际上并没有工作(看看它打印出来的数据)。我猜你是否已经在 REPL 中定义了 idname,并且这些是正在打印的值。你的工作解决方案现在可以正常工作了。

我做你想做的事情的解决方案是:

myTuples foreach {
  case (id, name) => printf("id: %s, name: %s\n", id, name)
}

myTuples foreach {
  x => printf("id: %s, name: %s\n", x._1, x._2)
}

【讨论】:

  • 非常感谢,很好的回答,你说得对,我给出的解决方案不起作用,这样做:myTuples.map{ x => println("id: %s, name: % s".format(x._1, x._2)) } 还有这个:myTuples.map{ x => val(id, name) = x; println("id: %s, name: %s".format(id, name)) }
  • myTuples.map{ println(_._1) } 等价于myTuples.map{ println(x =&gt; x._1) }。为什么会是别的?
  • @DanielC.Sobral “为什么会是别的?”显然是因为我误解了 _ 如何工作的规则。答案已更新。
【解决方案2】:

我想你想要这样的东西:

myTuples map { case(id, name) => println(id+": "+name) }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-08
    • 2012-07-24
    • 1970-01-01
    相关资源
    最近更新 更多