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的一个纯粹功能取决于:
Parameters passed to it.
传递给它的参数。
The algorithm (operations) of the function.
函数的算法(操作)。
A pure function is independent on:
纯函数独立于:
Any external I/O like input peripherals, database, files, services, etc. Also, it cannot provide any output to any output.
任何外部I / O,例如输入外围设备,数据库,文件,服务等。而且,它不能为任何输出提供任何输出。
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:
纯函数的一些示例是:
isEmpty()
是空的()
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()的函数,该函数接受两个整数值,分别是distance和fuel 。 该函数将使用公式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中使用纯函数的一些好处:
Easily combined: Pure functions are I/O independent, hence they can be easily combined with other functions.
易于组合:纯功能独立于I / O,因此可以轻松与其他功能组合。
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.
易于测试和调试:由于它们独立于外部值,因此它们的测试和调试很容易,因为您无需查看函数范围。
They don't change input values and other values outside their scope.
它们不会在其范围之外更改输入值和其他值。