Scala中高级的函数编程

Scala中高级的函数编程


文章目录

  • Scala中高级的函数编程
    • 写在前面
    • 高级函数编程
      • 函数作为值
      • 函数作为参数
      • 函数作为返回值
      • 匿名函数
      • 控制抽象
      • 闭包
      • 函数柯里化
      • 递归函数
      • 惰性函数


写在前面

  • 操作系统:Windows10
  • JDK版本:jdk1.8
  • Maven版本:Maven-3.5.4
  • Scala版本:Scala-2.12.11
  • IDE工具:IntelliJ IDEA 2019.2.3

所谓的高阶函数,其实就是将函数当成一个类型来使用,而不是当成特定的语法结构。

高级函数编程

函数作为值

核心代码:

val b = fun1 _
val c : ()=>Unit = fun1

object ScalaFunction {
    def main(args: Array[String]): Unit = {
        def fun1(): String = {
            "whybigdata"
        }
        val a = fun1
        val b = fun1 _
        val c : ()=>Unit = fun1
        println(a)
        println(b)
    }
}
  • 运行结果

whybigdata
Main$$$Lambda$98/0x0000000800c14040@97e1986

  • 解释说明

注意:val b = fun1 _ 中的 _是指代码编写者确定指向的是fun1这个函数,而不是说代码编写者忘记了为其传送参数

函数作为参数

核心代码:

Int => Int

fun22(fun2)

object ScalaFunction {
    def main(args: Array[String]): Unit = {
        def fun2(i:Int): Int = {
            i * 2
        }
        def fun22(f : Int => Int): Int = {
            f(10)
        }
        println(fun22(fun2))
    }
}

运行结果为:20

函数作为返回值

核心代码:

object ScalaFunction {
    def main(args: Array[String]): Unit = {
        def fun3(i:Int): Int = {
            i * 2
        }
        def fun33( ) = {
            fun3 _
        }
        println(fun33()(10))
    }
}

运行结果为:20

匿名函数

核心代码:

object ScalaFunction {
    def main(args: Array[String]): Unit = {
        def fun3(x : Int): Int = {
            x * 2
        }
        def fun33() = {
            fun3 _
        }
        println(fun33()(10))
    }
}

运行结果为:20

  • 注意事项:

任何使用def定义的都被认为是 方法 而非是函数

方法和函数的类型是不一样的:

  • 方法类型:(x : Int)Int
  • 函数类型:Int => Int

控制抽象

  • 理解:

简单理解为:将一系列语句组成既不带参数也没有返回值的函数

  • 核心代码:

op: => Unit

object ScalaFunction {
    def main(args: Array[String]): Unit = {
        def fun7(op: => Unit) = {
            op
        }
        fun7{
            println("whybigdata")
        }  
    }
}

运行结果为:whybigdata

闭包

  • 解释

闭包是一个函数,返回值依赖于声明在函数外部的一个或多个变量。

闭包通常来讲可以简单的认为是可以访问一个函数里面局部变量的另外一个函数。

核心代码:

object ScalaFunction {
    def main(args: Array[String]): Unit = {
        def fun5() = {
            val i = 20
            def fun55() = {
                i * 2
            }
            fun55 _
        }
        print(fun5()())
    }
}

运行结果为:40

函数柯里化

  • 解释

柯里化是指将原来接受两个参数的函数变成新的接受一个参数的函数的过程。新的函数返回一个以原有第二个参数为参数的函数

  • 核心代码

(i:Int)(j:Int)

object ScalaFunction {
    def main(args: Array[String]): Unit = {
        def fun6(i:Int)(j:Int) = {
            i * j
        }
    }
}

递归函数

核心代码:fun8(j-1)

object ScalaFunction {
    def main(args: Array[String]): Unit = {
        def fun8(j:Int):Int = {
            if ( j <= 1 ) {
                1
            } else {
                j * fun8(j-1)
            }
        }
        println(fun8(5))
    }
}

运行结果:120

惰性函数

  • 定义:

当函数返回值被声明为lazy时,函数的执行将被推迟,直到我们首次对此取值,该函数才会执行。这种函数我们称之为惰性函数

  • 核心代码:
object ScalaFunction {
    def main(args: Array[String]): Unit = {
        def fun9(): String = {
            println("function...")
            "whybigdata"
        }
        lazy val a = fun9()
        println("----------")
        println(a)
    }
}
  • 运行结果:
----------
function...
whybigdata

全文结束!

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值