Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Good programmer big data tutorials Scala series of high-order functions

Shulou Source: shulou.com Published: 2022-06-02 22:22:28 10月05日 Update

Good programmer big data tutorial Scala series of high-order functions 1.1. Concept if the incoming parameter of a function is a function or the return value is a function, then the function is a higher-order function.

1.2. The parameter passed in is the function Scala, which is a first-class citizen, just like a number. You can not only call, but also store the function in a variable, pass it into the function as an argument, or as the return value of the function.

1.3. In Scala, you don't need to name every function, just as you don't have to name every number, the function that assigns a function to a variable is called an anonymous function.

It's okay

1.4. The input parameter is the method (implicit conversion method to function) in Scala, the method and the function are different, the most essential difference is that the function can be passed to the method as a parameter

Case class WeeklyWeatherForecast (temperatures: Seq [Double]) {

Private def convertCtoF (temp: Double) = temp * 1.8 + 32

/ / method convertCtoF is passed as a parameter

Def forecastInFahrenheit: Seq [Double] = temperatures.map (convertCtoF)

}

1.5. Return value is function / / return value is function type: (String, String) = > String

Def urlBuilder (ssl: Boolean, domainName: String): (String, String) = > String = {

Val schema = if (ssl) "https://" else" http://"

(endpoint: String, query: String) = > s "$schema$domainName/$endpoint?$query"

}

Val domainName = "www.example.com"

Def getURL = urlBuilder (ssl=true, domainName)

Val endpoint = "users"

Val query = "id=1"

Val url = getURL (endpoint, query) / / "https://www.example.com/users?id=1": String

Tags: Functions parameters methods higher order variables numbers tutorials data programs programmers citizens essentials concepts types Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn MySQL Microsoft Shulou Tech Info Shulou Technology