Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is ← in Scala?

Tags:

scala

I googled "←" and searched for it on here, unable to find anything. But I found this source code for a chess game. View it here. Example of a code block that has copious usage of this symbol:

 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)
like image 639
user5187721 Avatar asked Aug 04 '15 02:08

user5187721


2 Answers

The Scala spec says on page 4 that (unicode \u2190) is reserved as is its ascii equivalent <- which as others are also pointing out, is an iterator for a for loop.

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

In scala you can bracket a complex expression and treat it as a single line with value provided by the end of the expression. What this is doing is creating a large nested for loop where each run through the loop increments the iterator at the end of the loop until, when it recycles, iterates the earlier iterator.

Here's an example using the scala shell and both syntaxes:

This is how most people write a for loop

Note that the | symbol is a line continuation not typed by me but rather inserted by 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)

But you can also use that funny arrow unicode symbol and it does the same thing:

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)

You might notice that some of the expressions in that complex for {} block you posted are assignments, not iterations. That's allowed, and doesn't break the chain of iteration. Here's a simpler example:

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)
like image 189
Paul Avatar answered Oct 22 '22 15:10

Paul


I'm not an Scala programmer. But according to this Scala accepts the unicode character ← which is equivalent to the operator <-. You can turn "=>" into "⇒", "->" into "→" and "<-" into "←".

like image 39
Andres Avatar answered Oct 22 '22 14:10

Andres