Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report