溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

4、Scala函數(shù)式基礎(chǔ)

發(fā)布時間:2020-08-10 15:24:43 來源:網(wǎng)絡(luò) 閱讀:245 作者:寬哥記事 欄目:大數(shù)據(jù)

一、內(nèi)置控制結(jié)構(gòu)

    1、if表達式

        val filenameNice = if (!args.isEmpty) args(0) else "default.txt"

    2、while循環(huán)

        不管是while還是do...while,我們都不能叫做表達式,而應(yīng)該叫做循環(huán),因為它們不會計算出一個新值。它們的值類型是Unit,在scala中Unit和()是相等的

        def gcdLoop(x: Long, y: Long): Long = {

          var a = x

          var b = y

          while (a != 0) {

            val temp = a

            a = b % a

            b = temp

          }

          b

        }


        do...while...表達式


        var line = ""

        do {

          line = scala.io.StdIn.readLine()

          println("Read: " + line)

        } while (line != "")


        在scala中給var重新賦值,永遠都是返回()(即Unit)

        () != ""將永遠返回true

        var tempLine = ""

        while ((tempLine = scala.io.StdIn.readLine()) != "") // This doesn't work!

          println("Read: " + tempLine)

            

        因為while循環(huán)不計算出值,所以while的代碼不那么的函數(shù)式,一般都會含有var變量,

        scala選擇保留while是因為有些場景使用while的代碼的可讀性還是很強的

        采用遞歸來消除掉var

        def gcd(x: Long, y: Long): Long =

          if (y == 0) x else gcd(y, x % y)

    3、for表達式

         用for表達式迭代一個任意集合

         val filesHere = (new java.io.File(".")).listFiles

        for (file <- filesHere)

          println(file)

        

        for表達式中的過濾

        for (

          file <- filesHere

          if file.isFile

          if file.getName.endsWith(".scala")

        ) println(file)

    

        嵌套迭代(nested iteration)

        def grep(pattern: String) =

          for (

            file <- filesHere

            if file.getName.endsWith(".scala");

            line <- fileLines(file)

            if line.trim.matches(pattern)

          ) println(file + ": " + line.trim)

         可以在迭代的過程中,綁定中間值到一個變量

        上面的()可以換成{},這樣每一個語句后面的分號就可以省略

        def grepBindValue(pattern: String) =

          for {

            file <- filesHere

            if file.getName.endsWith(".scala")

            line <- fileLines(file)

            trimmed = line.trim

            if trimmed.matches(pattern)

          } println(file + ": " + trimmed)

    

        for ... yield ... 可以生成一個新的集合

        //返回的是Array,是因為filesHere的類型是Array

        def scalaFiles: Array[File] =

          for {

            file <- filesHere

            if file.getName.endsWith(".scala")

          } yield file

    

        val forLineLengths: List[Int] =

          for {

            file <- filesHere.toList

            if file.getName.endsWith(".scala")

            line <- fileLines(file)

            trimmed = line.trim

            if trimmed.matches(".*object.*")

          } yield trimmed.length


    4、異常處理

        scala中拋異常使用throw關(guān)鍵字,異常的捕獲用catch關(guān)鍵字。捕獲到異常后,可以計算并返回一個值

            val n = 5

            val half: Int =

              if (n % 2 == 0)

                n / 2

              else

                throw new RuntimeException("n must be even")

            println(half)

        

            try {

              //val f = new FileReader("input.txt") //這個會拋FileNotFoundException

              // Use and close file

              throw new IOException("test")

            } catch {

              case e @(_: FileNotFoundException | _: IOException) => {

                // Handle missing file

                println("All Exception = " + e.getMessage)        

              }

              case ex: Exception => {

                // Handle other I/O error

                println("IOException = " + ex.getMessage)

              }

            }

        

           val file = new FileReader("input.txt")

            try {

              // Use the file

            } finally {

              file.close()  // Be sure to close the file

            }

    5、break和continue

            非得使用break的話,我們可以使用scala.util.control中的break

                import scala.util.control.Breaks._

                import java.io._

                val in = new BufferedReader(new InputStreamReader(System.in))

                breakable {

                  while (true) {

                    println("? ")

                    if (in.readLine() == "") break

                  }

                }

 

二、函數(shù)

    1、本地函數(shù)。定義在函數(shù)內(nèi)部的函數(shù)為本地函數(shù),智能被此函數(shù)內(nèi)部調(diào)用。

      def processFile(filename: String, width: Int) = {

        //本地函數(shù)

        def processLine(line: String) = {

          if (line.length > width)

    

            println(filename + ": " + line.trim)

        }

    

        val source = Source.fromFile(filename)

        for (line <- source.getLines())

          processLine(line)

      }

    2、first class function  函數(shù)可以作為函數(shù)的參數(shù)進行傳遞

    3、函數(shù)的參數(shù)        

        不定長參數(shù)

        def echo(args: String*) =

            for (arg <- args) println(arg)

        

        使用參數(shù)名字來給參數(shù)設(shè)置值

          def speed(distance: Float, time: Float): Float =

            distance / time

         speed(distance = 100, time = 10)

        

         默認參數(shù)值

        def printTime(out: java.io.PrintStream = Console.out) =

            out.println("time = " + System.currentTimeMillis())

         printTime()

         printTime(Console.err)

    4、函數(shù)閉包

    函數(shù)捕獲一個自由變量而變成一個閉包。 每次調(diào)用函數(shù)的時候都產(chǎn)生一個新的閉包。

        var more = 1

        val addMore = (x: Int) => x + more //函數(shù)捕獲一個自由變量而變成一個閉包

        addMore(10)

    

    當自由變量的值改變的時候,閉包對自由變量的改變也是可以捕獲的到

        more = 9999

        addMore(10)

        

        val someNumbers = List(-11, -10, -5, 0, 5, 10)

        var sum = 0

        someNumbers.foreach(sum +=  _)

        println(sum)

    5、尾遞歸

        什么是尾遞歸呢?如果調(diào)用自身方法的動作是函數(shù)體的最后一個動作的話,那么這個遞歸就是一個尾遞歸

        遞歸:更函數(shù)式、簡潔以及沒有var,但是函數(shù)的調(diào)用可能會消耗點時間。while循環(huán)是命令式編程,可能會更高效的,因為沒有方法的調(diào)用。但是如果是尾遞歸的話,編譯器是會優(yōu)化的,其效率和while循環(huán)是一樣的

        可以使用注解tailrec來標志是否為尾遞歸

         @tailrec

         def boom(x: Int): Int =

              if (x == 0) throw new Exception("boom!")

              else boom(x - 1)

        

        尾遞歸的限制,以下遞歸,不會進行尾遞歸優(yōu)化,因為jvm的指令集使得尾遞歸的優(yōu)化受限

        遞歸調(diào)用的函數(shù)不是函數(shù)體所在的函數(shù)

            def isEven(x: Int): Boolean =

              if (x == 0) true else isOdd(x - 1)

            def isOdd(x: Int): Boolean =

              if (x == 0) false else isEven(x - 1)

        

三、高階函數(shù)及函數(shù)柯里化

    高階函數(shù)(high-order functions)就是用函數(shù)作為參數(shù)的函數(shù)。高階函數(shù)可以減少冗余代碼,高階函數(shù)可以使得客戶端代碼簡潔。

    def filesEnding(query: String) =

        for (file <- filesHere; if file.getName.endsWith(query))

          yield file

    

     def filesContaining(query: String) =

        for (file <- filesHere; if file.getName.contains(query))

          yield file

    

    改造后:

    private def filesMatching(matcher: String => Boolean) =

        for (file <- filesHere; if matcher(file.getName))

          yield file


    柯里化

    

    //柯里化函數(shù)(curring function)

    def curriedSum(x: Int)(y: Int) = x + y

    curriedSum(1)(2)

    //說明柯里化的過程

    def first(x: Int) = (y: Int) => x + y

    val second = first(1)

    second(2)


   示例:自定義循環(huán)控制語句   

      var i = 10

      while (i == 0) {

        i -= 1

        println(i)

      }

    

      until(i == 0) {

        i -= 1

        println(i)

      }

    

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

        if (!condition) {

          block

          until(condition)(block)

        }

      }

      

    by name  by value

    by name  執(zhí)行的時候才執(zhí)行,by value調(diào)用就執(zhí)行

    //by-name parameter

    def byNameAssert(predicate: => Boolean) =

      if (assertionsEnabled && !predicate)

        throw new AssertionError


    byNameAssert(5 > 3)


    //by-value parameter

    def boolAssert(predicate: Boolean) =

      if (assertionsEnabled && !predicate)

        throw new AssertionError

    boolAssert(5 > 3)


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI