zoukankan      html  css  js  c++  java
  • 【转】scala解惑: 偏函数与部分应用函数

    def test = {
        val x = 1
        x match {
          case x if x > 0 => println(s" x=$x if x > 0")
        }
      }
    
      def p1: PartialFunction[Int, Int] = {
        case x if x > 1 => 1
      }
    
      def test2 = {
        val sample = 1 to 10
        val isEven: PartialFunction[Int, String] = {
          case x if x % 2 == 0 => x + " is even"
        }
        
        var ifDefine = isEven.isDefinedAt(1)
        println(s"isEven.isDefinedAt(1)  $ifDefine")
        ifDefine = isEven.isDefinedAt(2)
        println(s"isEven.isDefinedAt(2)  $ifDefine")
    
        // the method collect can use isDefinedAt to select which members to collect
        val evenNumbers = sample collect isEven
        println(s"sample collect isEven  $evenNumbers")
    
        val isOdd: PartialFunction[Int, String] = {
          case x if x % 2 == 1 => x + " is odd"
        }
    
        // the method orElse allows chaining another partial function to handle
        // input outside the declared domain
        val numbers = sample map (isEven orElse isOdd)
    
      }
    
      def f(x: Int, y: Int, z: Int) = List(x, y, z).sum
      
      def f1 = f(100, _: Int, _: Int)
    

    原文链接 http://www.cnblogs.com/nixil/archive/2012/05/16/2503722.html

    scala解惑: 偏函数与部分应用函数

    scala中有PartialFunction的概念, 同时还要一个概念叫Partial Applied Function.

    前者译作偏函数, 后者译作"偏应用函数"或"部分应用函数", 一字之差, 差距很大.

    首先偏函数是个数学概念, 偏函数不是"函数"的一种, 而是一个跟函数平行的概念.

    定义可参考wiki http://zh.wikipedia.org/wiki/%E5%87%BD%E6%95%B0 , 它是指

    定义域X中可能存在某些值在值域Y中没有对应的值.

    scala可以通过模式匹配来定义偏函数, 下面这两种方式定义的函数, 都可以认为是偏函数, 因为他们都只对其定义域Int的部分值做了处理.

    那么像p1定义成PartialFunction的额外好处是, 你可以在调用前使用一个isDefinedAt方法, 来校验参数是否会得到处理.  

    或者在调用时使用一个orElse方法, 该方法接受另一个偏函数,用来定义当参数未被偏函数捕获时该怎么做.

    也就是能够进行显示的声明. 在实际代码中最好使用PartialFunction来声明你确实是要定义一个偏函数, 而不是漏掉了什么.

    复制代码
        def p1:PartialFunction[Int, Int] = {
            case x if x > 1 => 1
        }
        
        def p2 = (x:Int) => x match {
            case x if x > 1 => 1
        }
    复制代码

    部分应用函数, 是指一个函数有N个参数, 而我们为其提供少于N个参数, 那就得到了一个部分应用函数

    比如我先定义一个函数

    def sum(a:Int,b:Int,c:Int) = a + b + c; 

    那么就可以从这个函数衍生出一个部分应用函数是这样的:

    def p_sum = sum(1, _:Int, _:Int)

    于是就可以这样调用p_sum(2,3), 相当于调用sum(1,2,3) 得到的结果是6. 这里的两个_分别对应函数sum对应位置的参数. 所以你也可以定义成

    def p_sum = sum (_:Int, 1, _:Int) 

    这东西有啥用呢? 一个是当你在代码中需要多次调用一个函数, 而其中的某个参数又总是一样的时候, 使用这个可以使你少敲一些代码. 另一个呢? 

  • 相关阅读:
    108. Convert Sorted Array to Binary Search Tree
    How to check if one path is a child of another path?
    Why there is two completely different version of Reverse for List and IEnumerable?
    在Jenkins中集成Sonarqube
    如何查看sonarqube的版本 how to check the version of sonarqube
    Queue
    BFS广度优先 vs DFS深度优先 for Binary Tree
    Depth-first search and Breadth-first search 深度优先搜索和广度优先搜索
    102. Binary Tree Level Order Traversal 广度优先遍历
    How do I check if a type is a subtype OR the type of an object?
  • 原文地址:https://www.cnblogs.com/ihongyan/p/4818432.html
Copyright © 2011-2022 走看看