Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

some operator questions

Tags:

scala

I'm new to scala so sorry if this is easy but I've had a hard time finding the answer.

I'm having a hard time understanding what <- does, and what ()=> Unit does. My understanding of these is that -> is sometimes used in foreach, and that => is used in maps. Trying to google "scala "<-" doesn't prove very fruitful. I found http://jim-mcbeath.blogspot.com/2008/12/scala-operator-cheat-sheet.html but it wasn't as helpful as it looks at first glance.

val numbers = List("one", "two", "three","four","five")
def operateOnList() {
  for(number <- numbers) {
    println(number + ": came out of this crazy thing!")
  }
}

def tweener(method: () => Unit) {
  method()
}

tweener(operateOnList)
like image 229
user1054583 Avatar asked Jan 29 '26 06:01

user1054583


1 Answers

() => Unit means that method is a function that takes no parameter and returns nothing (Unit).

<- is used in the for comprehension as an kind of assignation operator. for comprehension are a little bit specific because they are internally transformed. In your case, that would be transforms as numbers.foreach(i => println(i + ": came out of this crazy thing!"))

<- in the for comprehension means that we will iterate over each element of the numbers list and passed to number.

like image 191
David Avatar answered Jan 30 '26 20:01

David



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!