It’s partially applied because the cos method requires one argument, which you have not yet supplied (more on this in Recipe 9.6). This is called a partially applied function. For instance, you can create a new function named c from the method using either of these approaches: (The scala package defines other similar traits, including Function0, Function2, and so on, up to Function22.) Assigning an existing function/method to a function variableĬontinuing our exploration, you can assign an existing method or function to a function variable. Under the covers, modFunction is an instance of the Function1 trait, which defines a function that takes one argument. The following function works just like the previous method:Īt a coding level, the obvious difference is that modMethod is a method defined in a class, whereas modFunction is a function that’s assigned to a variable. List: List = List(1, 2, 3, 4, 5, 6, 7, 8, 9)Īs noted, this is similar to the process of defining a function literal and assigning it to a variable. Val f: (Int) => Boolean = i => Īny of these methods can be passed into collection methods that expect a function that has one Int parameter and returns a Boolean, such as the filter method of a List: However, if you prefer to explicitly declare the return type of a function literal, or want to do so because your function is more complex, the following examples show different forms you can use to explicitly declare that your function returns a Boolean: As a human, it’s also easy to look at the code on the right side of the expression and see that it returns a Boolean, so I usually leave the explicit Boolean return type off the function declaration. In this case, the Scala compiler is smart enough to look at the body of the function and determine that it returns a Boolean value. I generally prefer the following approach, which implicitly infers that the following function’s return type is Boolean: You can declare a function literal in at least two different ways. Welcome to the world of functional programming. For instance, because the map method of a sequence is a generic method that takes an input parameter of type A and returns a type B, you can pass the double method into the map method of an Int sequence: You can now invoke double just like you’d call a method:īeyond just invoking double like this, you can also pass it to any method (or function) that takes a function parameter with its signature. The variable double is an instance, just like an instance of a String, Int, or other type, but in this case, it’s an instance of a function, known as a function value. You can now assign that function literal to a variable: In this case, the function transforms the Int value i to an Int value that is twice the value of i. The following Scala code defines a function literal that takes an Int parameter and returns a value that is twice the amount of the Int that is passed in:Īs mentioned in Recipe 9.1, you can think of the => symbol as a transformer. Use the syntax shown in Recipe 9.1 to define a function literal, and then assign that literal to a variable. You want to pass a Scala function around like a variable, just like you pass String, Int, and other variables around in an object-oriented programming language. This is Recipe 9.2, “How to use functions as variables (values) in Scala.” Problem This is an excerpt from the 1st Edition of the Scala Cookbook (partially modified for the internet). show more info on classes/objects in repl.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |