在Scala中是什么?

use*_*721 9 scala

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

 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)
Run Code Online (Sandbox Code Playgroud)

Pau*_*aul 17

斯卡拉规范说4页上?(UNICODE \u2190)保留为是其对应的ASCII码<-这是其他人也指出,是一个for循环迭代器.

for(x <- 1 to 5)  println(i)
Run Code Online (Sandbox Code Playgroud)

在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)
Run Code Online (Sandbox Code Playgroud)

但你也可以使用那个有趣的箭头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)
Run Code Online (Sandbox Code Playgroud)

您可能会注意到,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)
Run Code Online (Sandbox Code Playgroud)


And*_*res 2

我不是 Scala 程序员。但根据这个Scala 接受 unicode 字符 \xe2\x86\x90 ,它相当于运算符<-。您可以将“=>”转换为“\xe2\x87\x92”,将“->”转换为“\xe2\x86\x92”,将“<-”转换为“\xe2\x86\x90”。

\n