0

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)

4 Answers 4

3

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)
Sign up to request clarification or add additional context in comments.

3 Comments

Remove is not a function, it's a method. That distinction is pretty fundamental in Scala. (E.g. functions are objects, methods aren't.)
@Jörg W Mittag yikes! You're right! Fixed my answer, thanks a lot for the catch!
No worries! It's common amongst experienced Scalarazzi to just call everything a "function", because a) everybody knows it isn't, and b) it's usually clear from the context. Heck, even the Scala Language Specification does this! But when dealing with a newbie, I find that correct terminology is vital. (For example, I hate all the Ruby tutorials that call instance methods of the class object's singleton class "class methods" or even worse "static methods", because they really aren't.)
2

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)

Comments

1

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; }

Comments

1

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)

2 Comments

Use of 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... :)
thx, I'll try the FUnction1 a bit later, for now I took out the lines with the offending returns. I don't have the REPL installed here, so I was trying ideone.com, and it wasn't working -- now I know why

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.