Scala中的纯函数

A pure function is a function that does not have a dependency on i/o and other values that are out of the function's scope. And depending only on the values that are passed in parameters of the function.

纯函数是不依赖于I / O和该函数范围之外的其他值的函数。 并且仅取决于函数参数中传递的值。

Let's elaborate the function more,

让我们进一步详细说明功能,

A pure function is Scala depends on:

Scala的一个纯粹功能取决于:

  1. Parameters passed to it.

    传递给它的参数。

  2. The algorithm (operations) of the function.

    函数的算法(操作)。

A pure function is independent on:

纯函数独立于:

  1. Any external I/O like input peripherals, database, files, services, etc. Also, it cannot provide any output to any output.

    任何外部I / O,例如输入外围设备,数据库,文件,服务等。而且,它不能为任何输出提供任何输出。

  2. It cannot use any values that are outside the function’s scope. Also, no variables can be modified outside its scope.

    它不能使用函数范围之外的任何值。 同样,不能在其范围之外修改任何变量。

As defined by Alvin Alexander, pure function = Output depends on Input + No side effects

根据Alvin Alexander的定义, 纯函数=输出取决于输入+无副作用

Some examples of pure function are:

纯函数的一些示例是:

  1. isEmpty()

    是空的()

  2. sqrt()

    sqrt()

Now, let's create a pure function:

现在,让我们创建一个纯函数:

Example: Mileage Calculator

示例:里程计算器

object MyObject {
    def mileageCal(distance:Int, fuel:Int): Float = {
        val mileage = distance/fuel
        return (mileage);
    }
    def main(args: Array[String]) {
    println("The mileage of my bike is: " + mileageCal(342, 9))
    }
}

Output:

输出:

The mileage of my bike is: 38.0

In the above code, we have created a function named mileageCal() which accepts two integer values which are distance and fuel. The function will find mileage using the formula mileage = distance/fuel. And the function returns this value back to the calling function.

在上面的代码中,我们创建了一个名为MileageCal()的函数,该函数接受两个整数值,分别是distancefuel 。 该函数将使用公式miles = distance / fuel来查找里程。 然后该函数将该值返回给调用函数。

Scala中的不纯函数 (Impure Functions in Scala)

All the functions that are not pure are impure. This makes the definition of an impure function as "A function that is dependent on i/o or other values out of function's scope along with parameter values."

所有不纯的功能都不纯。 这使不纯函数的定义为“ 依赖于I / O或其他值以及函数值范围之外的函数的函数”。

Now, let's create an impure function:

现在,让我们创建一个不纯函数:

Example: Constant multiplier

示例:常数乘数

object MyObject {
    val constant = 12.4;
    def constantMul(value:Int): Double = {
        val result = value*constant; 
        return (result);
    }
    
    def main(args: Array[String]) {
        println("The result of constant multiplier is: " + constantMul(34))
    }
}

Output:

输出:

The result of constant multiplier is: 421.6

In the above code, we have created a constantMul() function which accepts a parameter value that is multiplied by a value defined in the object i.e. dependent on a value outside the scope of the function.

在上面的代码中,我们创建了一个constantMul()函数,该函数接受与对象中定义的值相乘的参数值,即该值取决于函数范围之外的值。

使用纯函数的好处 (Benefits of using a pure function)

Some benefits of using a pure function in Scala:

在Scala中使用纯函数的一些好处:

  1. Easily combined: Pure functions are I/O independent, hence they can be easily combined with other functions.

    易于组合:纯功能独立于I / O,因此可以轻松与其他功能组合。

  2. Easy to test and debug: Due to their independency on outer values, their test and debugging is easy as you don't need to look outside the function's scope.

    易于测试和调试:由于它们独立于外部值,因此它们的测试和调试很容易,因为您无需查看函数范围。

  3. They don't change input values and other values outside their scope.

    它们不会在其范围之外更改输入值和其他值。

翻译自: https://www.includehelp.com/scala/pure-function.aspx

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值