" is used in Scala val list = List("abc", "...">

Need help understanding scala code snippet

Possible duplicate:
Can anyone explain how the symbol "=>" is used in Scala

val list = List("abc", "cde", "fg") list.count (s => s.length == 3) 

The above code snippet returns the number of string elements in list that are 3 in length. But I can not understand this fragment, since it is difficult for me to understand the use of the => operator in this context. Any explanation would be really helpful.

0
source share
3 answers

Yes, Scala can be quite difficult to understand. I will do my best to explain this, although I may also be wrong.

The List.count method takes as a parameter a block code that returns a boolean value.

Blocks are just small pieces of code and can be created in a variety of ways, for example, by including code in { }

Scala docs describe this as

 def count (p : (A) => Boolean) : Int 

so count takes a parameter p , which is a block that takes an argument of type A and returns a Boolean

So in this example:

 s => s.length == 3 

is block code. Blocks usually follow the format

 [arguments] => [Code to execute] 

So, in this case, s is the input to the block, and s.length == 3 is the code that should return a boolean value. You can name the arguments that you like as long as they are in the correct order.

When using a method that iterates over a collection, such as count , map , each , etc., the argument passed will be the current element in the collection that iterates.

If you want to know more about this, you should familiarize yourself with the Coursera course, which is launched by my Martin Odersky (creator of scala) and will tell you in detail the details: https://www.coursera.org/course/progfun

+2
source

(I didn’t see that you posted another (reformulated) question. Here is what I answered your first)

More than passing values ​​/ names, => used to define a function literal, which is an alternative syntax used to define a function.

Sample time. Say you have a function that accepts another function. The collections are full of them, but we will choose filter . filter , when used in a collection (e.g. List), will output any element that forces the function you provide to return false.

 val people = List("Bill Nye", "Mister Rogers", "Mohandas Karamchand Gandhi", "Jesus", "Superman", "The newspaper guy") // Let only grab people who have short names (less than 10 characters) val shortNamedPeople = people.filter(<a function>) 

We could pass the real function from another place ( def isShortName(name: String): Boolean , maybe), but it would be better to just put it right there. Alas, we can, with functional literals.

 val shortNamedPeople = people.filter( name => name.length < 10 ) 

We have created a function here that takes a String (since people is of type List[String] ) and returns a boolean. Pretty cool, right?

This syntax is used in many contexts. Say you want to write a function that takes another function. This other function should take in String and return another string.

 def myFunction(f: String => Int): Int = { val myString = "Hello!" f(myString) } // And let use it. First way: def anotherFunction(a: String): Int = { a.length } myFunction(anotherFunction) // Second way: myFunction((a: String) => a.length) 

Here is what literals are. Returning to by-name and by-value , there is a trick in which you can force the parameter to not be evaluated until you want it. Classic example:

 def logger(message: String) = { if(loggingActivated) println(message) } 

This looks good, but message actually evaluated when the logger called. What if message takes time to evaluate? For example, logger(veryLongProcess()) , where veryLongProcess() returns a string. Oops? Not really. We can use our knowledge of function literals to make veryLongProcess() not be called until it is needed.

 def logger(message: => String) = { if(loggingActivated) println(message) } logger(veryLongProcess()) // Fixed! 

logger now accepts a function that takes no parameters (hence bare => on the left). You can still use it as before, but now message is only evaluated when it is used (in println ).

+2
source

It just defines the method, you can also see it like this:

 def isValid(s:String) = s.length ==3 list.count(isValid) 

So, when you use the count function, you give it a condition, which is a function as an argument.

+1
source

All Articles