这是一个纯函数吗? [英] Is this a pure function?

查看:67
本文介绍了这是一个纯函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大多数将纯函数定义为具有以下两个属性:

  1. 对于相同的参数,其返回值相同.
  2. 它的评估没有副作用.

这是与我有关的第一个条件.在大多数情况下,很容易判断.考虑以下JavaScript函数(如 this文章)

纯:

const add = (x, y) => x + y;

add(2, 4); // 6

不纯:

let x = 2;

const add = (y) => {
  return x += y;
};

add(4); // x === 6 (the first time)
add(4); // x === 10 (the second time)

很容易看出第二个函数将为后续调用提供不同的输出,从而违反了第一个条件.因此,这是不纯的.

这部分我明白了.


现在,对于我的问题,考虑以下函数,该函数会将给定的美元金额转换为欧元:

(编辑-在第一行中使用const.之前无意中使用过let.)

const exchangeRate =  fetchFromDatabase(); // evaluates to say 0.9 for today;

const dollarToEuro = (x) => {
  return x * exchangeRate;
};

dollarToEuro(100) //90 today

dollarToEuro(100) //something else tomorrow

假设我们从数据库获取汇率,并且汇率每天都在变化.

现在,无论我今天多少次调用此函数 ,它都会为输入100提供相同的输出.但是,明天可能会给我带来不同的输出.我不确定这是否违反第一个条件.

IOW,该函数本身不包含任何使输入突变的逻辑,但它依赖于将来可能更改的外部常量.在这种情况下,绝对可以每天更改.在其他情况下,可能会发生;可能不会.

我们可以将此类函数称为纯函数吗?如果答案是否定的,那么我们如何将其重构为一个?

dollarToEuro的返回值取决于不是参数的外部变量.因此,该功能不纯.

答案是否定的,那么我们如何才能将函数重构为纯函数?

一个选项是传递exchangeRate.这样,每次参数为(something, somethingElse)时,输出保证something * somethingElse:

const exchangeRate =  fetchFromDatabase(); // evaluates to say 0.9 for today;

const dollarToEuro = (x, exchangeRate) => {
  return x * exchangeRate;
};

请注意,对于函数式编程,应避免使用let-始终使用const以避免重新分配.

Most sources define a pure function as having the following two properties:

  1. Its return value is the same for the same arguments.
  2. Its evaluation has no side effects.

It is the first condition that concerns me. In most cases, it's easy to judge. Consider the following JavaScript functions (as shown in this article)

Pure:

const add = (x, y) => x + y;

add(2, 4); // 6

Impure:

let x = 2;

const add = (y) => {
  return x += y;
};

add(4); // x === 6 (the first time)
add(4); // x === 10 (the second time)

It's easy to see that the 2nd function will give different outputs for subsequent calls, thereby violating the first condition. And hence, it's impure.

This part I get.


Now, for my question, consider this function which converts a given amount in dollars to euros:

(EDIT - Using const in the first line. Used let earlier inadvertently.)

const exchangeRate =  fetchFromDatabase(); // evaluates to say 0.9 for today;

const dollarToEuro = (x) => {
  return x * exchangeRate;
};

dollarToEuro(100) //90 today

dollarToEuro(100) //something else tomorrow

Assume we fetch the exchange rate from a db and it changes every day.

Now, no matter how many times I call this function today, it will give me the same output for the input 100. However, it might give me a different output tomorrow. I'm not sure if this violates the first condition or not.

IOW, the function itself doesn't contain any logic to mutate the input, but it relies on an external constant that might change in the future. In this case, it's absolutely certain it will change daily. In other cases, it might happen; it might not.

Can we call such functions pure functions. If the answer is NO, how then can we refactor it to be one?

解决方案

The dollarToEuro's return value depends on an outside variable that is not an argument; therefore, the function is impure.

In the answer is NO, how then can we refactor the function to be pure?

One option is to pass in exchangeRate. This way, every time arguments are (something, somethingElse), the output is guaranteed to be something * somethingElse:

const exchangeRate =  fetchFromDatabase(); // evaluates to say 0.9 for today;

const dollarToEuro = (x, exchangeRate) => {
  return x * exchangeRate;
};

Note that for functional programming, you should avoid let - always use const to avoid reassignment.

这篇关于这是一个纯函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆