I am beginning Scala and I don’t understand what the s => part is/does. Could someone explain me that?
thrill.remove(s => s.length == 4)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s a way of specifying a function. A function is something that takes one or more parameters as input and gives back an output. One of the ways you can specify a function is with the => symbol.
In scala you can use functions like you use integer or strings or any other kind of basic data types.
You can assign them to variables:
and you can pass them as parameters to other functions or methods:
here you’re saying to the remove method: “apply this function s => s.length==4 to each element in the list, and remove all elements where that function returns true”
By the way, notice that remove is deprecated. The suggested alternative is filterNot