user5187721
user5187721

Reputation: 189

What is ← in 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)

Upvotes: 18

Views: 14428

Answers (4)

Xavier Guihot
Xavier Guihot

Reputation: 61716

Note that starting Scala 2.13, the symbol is deprecated:

Deprecate unicode arrows , and #7540)

It used to be equivalent to <-.


Same goes for (equivalent of =>) and (equivalent of ->).

Upvotes: 0

Paul
Paul

Reputation: 27443

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)

Upvotes: 22

Theodore Norvell
Theodore Norvell

Reputation: 16241

It is the same as <-, which is usually used in for expressions.

Upvotes: 0

Andres
Andres

Reputation: 6200

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 "←".

Upvotes: 2

Related Questions