美文网首页
8. 高阶函数练习

8. 高阶函数练习

作者: 逸章 | 来源:发表于2020-09-21 23:22 被阅读0次
import scala.math._

//Technically, the _ turns the ceil method into a function. In Scala, you cannot manipulate methods, only functions
val fun: (Double) => Double = ceil  //The _ behind the ceil function indicates that you really meant the function, and you didn’t just forget to supply the arguments.
fun(3.14)

//但是,The _ suffix is not necessary when you use a method name in a context where a function is expected
val f: (Double) => Double = ceil // No underscore needed,但是val f = ceil 则会报错
f(3.14)
Array(1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8).map(f)

//-------------------------------------------------------注意,下面说的方法和ceil的不同
//The ceil method is a method of the scala.math package object. If you
//have a method from a class, the syntax for turning it into a function is slightly
//different
//A function (String, Int) => Char
val f2 = (_:String)charAt(_:Int)
f2("hello",1)

//--上面的也可以用下面这种方法
val f3: (String, Int) => Char = _.charAt(_)
val threeNumAdd:(Double, Double, Double) => Double = _+_+_
threeNumAdd(1,2,3)

//--------------------------------------------------匿名函数
val triple = (x:Double) => x * 3
val tripleMe : Double => Double = x => x * 3
Array(3.14, 1.42, 2.0).map((x:Double)=> 3 * x)

//注意省去点号的写法:
//This is more common when a method is used in infix notation (without the dot).
Array(3.14, 1.42, 3.0) map ( (x: Double) => 3 * x )
Array(3.14, 1.42, 2.0).map( (x: Double) => 3 * x )
//注意上面两个自理大括号可以换成花括号{}

//---------------------------------Functions with Function Parameters或者返回一个函数
def valueAtOneQuarter(f: (Double) => Double) = f(0.25)
valueAtOneQuarter(ceil) // 1.0
valueAtOneQuarter(sqrt _)

def mulBy(factor : Double) = (x : Double) => factor * x

//-------------------------------函数参数类型推导
//If a parameter occurs only once on the right-hand side of the =>,you can replace it with an underscore:
valueAtOneQuarter(3 * _) //得到0.75

//只有当参数类型可以推到的时候方可省略类型:
//val fun1 = 3 * _ // Error: Can’t infer types
val fun2 = 3 * (_: Double) // OK
val fun3: (Double) => Double = 3 * _ // OK because we specified the type for fun
(1 to 9).map("*" * _).foreach(println(_))

(1 to 9).reduceLeft(_ * _)
//(...((1 * 2) * 3) * ... * 9)  前面省略号表示省略的括号

"Mary had a little lamb".split(" ").sortWith(_.length < _.length)

柯里化:

def mulOneAtATime(x: Int)(y: Int) = x * y
mulOneAtATime(3)(4)

控制抽象:比如我们自己定义一个until功能:

Scala programmers can build control abstractions: functions
that look like language keywords

def until(condition: => Boolean)(block: => Unit) {
    if (!condition) {
      block
      until(condition)(block)
    }
  }

var x = 10
    until(x == 0) {
      x -= 1
      println(x)
    }

注意上面until函数的定义也使用了currying(注意两个参数分开写的)

相关文章

  • 8. 高阶函数练习

    柯里化: 控制抽象:比如我们自己定义一个until功能: Scala programmers can build ...

  • Rust语言编程实例100题-059

    Rust语言编程实例100题-059 题目:Rust高阶函数练习。高阶函数是指以函数为参数或者返回值的函数,是函数...

  • Rust语言编程实例100题-060

    Rust语言编程实例100题-060 题目:Rust高阶函数练习。高阶函数是指以函数为参数或者返回值的函数,是函数...

  • python学习(三)函数式编程

    高阶函数 函数也是变量,函数参数为函数的函数,称作高阶函数 自定义高阶函数 内建高阶函数 map/reducema...

  • 11.Lambda和高阶函数(Lambda and Higher

    高阶函数 kotlin_Lambda,高阶函数 *swift_高阶函数

  • 😍

    1.var let const2.箭头函数 3.闭包4.this5.高阶函数 6.数据结构 7.模板字符串 8.解...

  • Python | 高阶函数基本应用及Decorator装饰器

    一、高阶函数 理解什么是高阶函数?以及高阶函数的基本应用方法 ▲ 高阶函数 在了解什么是高阶函数之前,我们来看几个...

  • 学习笔记

    高阶函数 高阶函数只是将函数作为参数或返回值的函数 练习题map 请把用户输入的不规范的英文名字,变为首字母大写,...

  • 四、函数进阶

    一. 高阶函数 参数类型包含函数类型或返回值类型为函数类型的函数为高阶函数。 常见的高阶函数 高阶函数的调用 二....

  • Kotlin 高阶函数

    什么是高阶函数 将函数作为参数或者返回值的,称高阶函数。 定义高阶函数 action是一个高阶函数,(Int) -...

网友评论

      本文标题:8. 高阶函数练习

      本文链接:https://www.haomeiwen.com/subject/mhzgyktx.html