在Scala中是什么?

时间:2015-08-04 02:03:27

标签: scala

我用Google搜索"←"并在这里搜索它,无法找到任何东西。但是我找到了这个国际象棋游戏的源代码。查看here。大量使用此符号的代码块示例:

 for {
      storedFen ← GameRepo initialFen game
      fen = storedFen orElse (aiVariant match {
        case v@Horde => v.initialFen.some
        case _       => none
      })
      uciMoves ← uciMemo get game
      moveResult ← move(uciMoves.toList, fen, level, aiVariant)
      uciMove ← (UciMove(moveResult.move) toValid s"${game.id} wrong bestmove: $moveResult").future
      result ← game.toChess(uciMove.orig, uciMove.dest, uciMove.promotion).future
      (c, move) = result
      progress = game.update(c, move)
      _ ← (GameRepo save progress) >>- uciMemo.add(game, uciMove.uci)
    } yield PlayResult(progress, move)

4 个答案:

答案 0 :(得分:15)

{4}在第4页上说(unicode \u2190)是保留的,因为它的ascii等价<-正如其他人指出的那样,是一个迭代器for for for循环。

for(x <- 1 to 5)  println(i)

在scala中,您可以将复杂表达式括起来,并将其视为单行,其值由表达式的末尾提供。这样做是创建一个大的嵌套for循环,其中每个循环遍历循环在循环结束时递增迭代器,直到它循环,迭代早期的迭代器。

以下是使用scala shell和两种语法的示例:

这是大多数人编写for循环的方式

请注意,|符号是我未输入的续行,而是由scala shell插入

scala> for { 
     | x<-1 to 5
     | y<-2 to 6
     | } println (x,y)
(1,2)
(1,3)
(1,4)
(1,5)
(1,6)
(2,2)
(2,3)
...
(5,5)
(5,6)

但你也可以使用那个有趣的箭头unicode符号,它也做同样的事情:

scala> for {
     | x ← 1 to 5 
     | y ← 2 to 6 
     | } println (x,y)
(1,2)
(1,3)
(1,4)
(1,5)
(1,6)
(2,2)
(2,3)
...
(5,5)
(5,6)

您可能会注意到,您发布的复杂for {}块中的某些表达式是赋值,而不是迭代。这是允许的,并没有打破迭代链。这是一个更简单的例子:

scala> for {
     | x ← 1 to 3
     | y = x*x 
     | z ← 1 to 4
     | } println (x,y,z)
(1,1,1)
(1,1,2)
(1,1,3)
(1,1,4)
(2,4,1)
(2,4,2)
(2,4,3)
(2,4,4)
(3,9,1)
(3,9,2)
(3,9,3)
(3,9,4)

答案 1 :(得分:2)

我不是Scala程序员。但根据this,Scala接受了unicode字符←,它等同于运算符<-。你可以转过&#34; =&gt;&#34;进入&#34;⇒&#34;,&#34; - &gt;&#34;进入&#34;→&#34;和&#34;&lt; - &#34;进入&#34;←&#34;。

答案 2 :(得分:0)

<-相同,for通常用于sortProperties: ['firstname:asc'], // or just 'firstname', or 'firstname:desc' sortedEmployee: Ember.computed.sort('employees', 'sortProperties') 表达式。

答案 3 :(得分:0)

请注意,不赞成使用Scala 2.13符号

  

Deprecate unicode arrows , and #7540

它曾经等同于<-


(相当于=>)和(相当于->)也一样。