码迷,mamicode.com
首页 > 编程语言 > 详细

Scala语言学习笔记(4)

时间:2017-12-21 12:04:16      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:https   build   ble   private   boolean   学习笔记   double   math   poi   

高阶函数

// 高阶函数 map
val salaries = Seq(20000, 70000, 40000)
val doubleSalary = (x: Int) => x * 2
val newSalaries = salaries.map(doubleSalary) // List(40000, 140000, 80000)
val newSalaries = salaries.map(x => x * 2) // List(40000, 140000, 80000)
val newSalaries = salaries.map(_ * 2)
// 将方法强制转换为函数
case class WeeklyWeatherForecast(temperatures: Seq[Double]) {
 private def convertCtoF(temp: Double) = temp * 1.8 + 32
  def forecastInFahrenheit: Double = temperatures.map(convertCtoF) // <-- passing the method convertCtoF
}
// 接收函数参数的函数
object SalaryRaiser {
 private def promotion(salaries: List[Double], promotionFunction: Double => Double): List[Double] =
    salaries.map(promotionFunction)
  def smallPromotion(salaries: List[Double]): List[Double] =
    promotion(salaries, salary => salary * 1.1)
  def bigPromotion(salaries: List[Double]): List[Double] =
    promotion(salaries, salary => salary * math.log(salary))
  def hugePromotion(salaries: List[Double]): List[Double] =
    promotion(salaries, salary => salary * salary)
}
// 返回函数的函数
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

Scala语言学习笔记(4)

标签:https   build   ble   private   boolean   学习笔记   double   math   poi   

原文地址:http://www.cnblogs.com/zwvista/p/8078411.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!