Some operator questions

I am new to scala, so sorry if this is easy, but it was hard for me to find the answer.

It's hard for me to understand what <- does, and what () => Unit does. My understanding is that β†’ is sometimes used in foreach, and that => is used in maps. Trying google "scala" <- "did not prove to be very fruitful. I found http://jim-mcbeath.blogspot.com/2008/12/scala-operator-cheat-sheet.html , but it was not as useful as it looks at first sight.

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) 
+2
source share
3 answers

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

<- used for understanding as a kind of assignment operator. they are a little specific for understanding, because they are internally transformed. In your case, it will be converted as numbers.foreach(i => println(i + ": came out of this crazy thing!"))

<- in the understanding, it means that we will iterate over each element of the numbers list and be passed to number .

+5
source

'<-' can be treated as 'in', therefore

 for(number <- numbers){ ... } 

can be translated into English, as for each number in do numbers

'<-' has a double with different semantics: '->'. It's just that it's just replacing the comma in the tuples: (a, b) is equivalent to (a-> b) or just a-> b. The meaning after this character is that 'a' maps to 'b'. So this is often used in the definition of Maps:

  Map("a" -> 1,"aba" -> 3) Map("London" -> "Britain", "Paris" -> "France") 

Here you can think of matching as a projection (or not) with some function (for example, "line length", "capital").

The best explanation is here .

Last, but not least, is '=>', which is also a map, but with common semantics. '=>' is used universally in anonymous expressions:

 scala> List(1,2,3,4).map(current => current+1) res5: List[Int] = List(2, 3, 4, 5) 

What for each element of the element is the current element of the list with the plus one function

 List(1,2,3,4).map(c => c%2 match { | case 0 => "even" | case 1 => "odd" | } | ) res6: List[java.lang.String] = List(odd, even, odd, even) 

Display current item with math template provided

+4
source

In method

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

the method is called tweener , the parameter is arbitrarily called method , and the type is method () => Unit , which is the type of function, as you can say from => .

Unit is a return type, similar to void in Java, and does not represent any interesting value. For example, the return type of print is Unit . () is an empty parameter list.

Vaguely, () also used to represent an instance of Unit called a unit value, which can only take the value of a Unit . But this is not what it means in the function type () => Unit , just as you cannot have the function type 42 => Unit .

Back to your example, tweener performs a function of type () => Unit . operateOnList is a method, but it is partially used by the compiler to turn it into a function value. You can turn methods into your functions as follows:

 scala> def m = println("hi") m: Unit scala> m _ res17: () => Unit = <function0> 

operateOnList can be turned into the desired function type because its parameter list is empty () , and its return type is the implicitness of Unit .

As a side note, if operateOnList was defined without an empty parameter list (as is customary, but more often when the return type is not Unit), you will need to manually partially apply it, otherwise its value will be instead:

 def f1() {} def f2 {} def g(f: () => Unit) {} g(f1) // OK g(f2) // error, since we're passing f2 result (), // rather than partial function () => Unit g(f2 _) // OK 
+3
source

Source: https://habr.com/ru/post/1384802/


All Articles