【问题标题】:reversing the boolean return type from a function从函数反转布尔返回类型
【发布时间】:2013-04-05 21:23:55
【问题描述】:

我有两个接受函数类型的函数:Int => Boolean 函数类型

def myFunction1(f1: Int => Boolean) ...
def myFunction2(f2: Int => Boolean) ...

我想从 function1 调用 function2,但不是只用 f1 调用它,而是想用 f1 的倒数来调用它。因此,如果 f1 类似于

(x: Int) => x > 4

在运行时,即对于大于 4 的数字返回 true。如果数字大于四,我希望​​反向返回 false。是否可以在调用 myFunction2 之前反转 f1?

【问题讨论】:

    标签: scala functional-programming


    【解决方案1】:

    你可以这样做:

    def myFunction1(f1: Int => Boolean) =  myFunction2(!f1(_))
    

    【讨论】:

      【解决方案2】:
      def myFunction1(f1: Int => Boolean) =  myFunction2(f1 andThen (! _))
      

      【讨论】:

        猜你喜欢
        • 2013-03-11
        • 2015-01-18
        • 2013-11-29
        • 2020-02-26
        • 2017-06-07
        • 2011-07-22
        • 1970-01-01
        • 1970-01-01
        • 2018-06-30
        相关资源
        最近更新 更多