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)
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.
s: String => s.length == 4 // a function that takes a String called s as input
// and returns a Boolean (true if the length of s is 4
// false otherwise)
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:
scala> val f = (s: String) => s.length==4 // assigning our function to f
f: String => Boolean = <function1>
scala> f("abcd") // ...and using it
res1: Boolean = true
and you can pass them as parameters to other functions or methods:
scala> val thrill = List("foo", "bar", "baz", "bazz")
thrill: List[java.lang.String] = List(foo, bar, baz, bazz)
scala> thrill.remove(s => s.length == 4)
warning: there were 1 deprecation warnings; re-run with -deprecation for detail
res2: List[java.lang.String] = List(foo, bar, baz)
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
scala> thrill.filterNot(s => s.length == 4)
res3: List[java.lang.String] = List(foo, bar, baz)
The expression
s => s.length == 4
represents a function which takes a string (I'm guessing) and returns a boolean value based on whether it has length four
val f = s => s.length == 4
println(f("five")) //prints "true"
println(f("six")) //prints "false"
the s => part just declares the parameter names for the function (in this case, just one: s)
This says "run the function s => s.length == 4 over all items in thrill, and remove any where the function returns true." Logically, that function must take a single item of the type contained in thrill as a parameter, and it must return a boolean (true or false).
The scala syntax s => ... indicates a lambda function- a kind of shorthand function where in many cases the function's parameter and return types are inferred. For example in this case the compiler is smart enough to know that if thrill contains Strings, s must be a string. Likewise, it can confirm in a statically typed way that the return value (s.length == 4) is a boolean, and satisfies the requirement for a boolean return value.
In short: think of it as a function defined as boolean f(String s) { return s.length == 4; }
I believe all of the following are the same as each other (although I'm a Scala newb too, feel free to offer corrections)
thrill.remove((s:String) => s.length == 4)
thrill.remove(s => s.length == 4)
thrill.remove(_.length == 4)
return here is wrong (try it in the REPL and you get error: return outside method definition). You might like instead to include defining the function without the sugar using the Function1 trait... :)