From the course: Introduction to Scala

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

The map function

The map function

- [Instructor] Let's look at the map function. So we're going to hop into the scala3-repl first, and I'm going to write out an option. This is going to be called middleName and it's going to be an option of type String. And I'm going to set this to Some of Muna. Great. So now we have an option of String, it's called middleName and it's a Some and the string is Muna. Let's say that I want to run a function on the value inside the Some. In this case, it's Muna. Let's say I want to capitalize every single character in that word. So this is where the map function really shines. So map takes one parameter. It takes a function. So the input to this function is word. And then the arrow signifies that we need to change this word in some way and we need to call toUpperCase on it. So let's try this out. Wonderful. It worked. So Muna went from being capitalized M and lowercase other characters but now every single…

Contents