“功能性编程”背后的哲学是什么?当你真的想要创造副作用? [英] What is the philosophy behind "functional programming" when you actually want to create a side effect?

查看:78
本文介绍了“功能性编程”背后的哲学是什么?当你真的想要创造副作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道纯函数属于函数式编程范例,你创建一个没有任何副作用的函数,对于输入它总是返回相同的输出,如:

I know that pure functions are in the "functional programming" paradigm, you create a function that doesn't have any side effects and that for a input it always return the same output like:

function (a,b) {
    return a + b;
}

这是一个纯函数,因为对于输入我总是返回相同的输出我没有产生任何副作用。好吧,我明白了。

This is a pure function because for a input I always return the same output and I didn't create any side effects. Ok I got that.

但我怎样才能制作纯函数,当我真正想创造一个副作用时,我怎么能保持函数式编程范式呢? ,例如更改DOM中的文本内容,例如:

But how can I make "pure functions", how can I stay in the "functional programming" paradigm when I actually want to create a side effect, such as changing a text content in the DOM, like :

function changeContent () {
   let content = document.querySelector("#content");
   content.textContent = 'Hello World';
}

此功能有副作用,它没有输入返回a输出,它产生副作用,但这实际上是功能的重点。这还是功能性编程吗?在这种情况下如何保持函数式编程范式?

This function has a side effect, it is not getting a input a return a output, it is creating a side effect, but that is actually the point of the function. Is this still "functional programming"? How to stay in the "functional programming" paradigm in that case?

推荐答案


我如何留下在函数式编程范例中,当我真的想要创建副作用时?

How can I stay in the "functional programming" paradigm when I actually want to create a side effect?

你真的不能。纯函数不能有任何副作用。

You cannot really. A pure function must not have any side effect.

当然,这意味着我们实际上无法对作为纯函数执行任何操作的程序进行建模。有两种方法:

Of course, this means that we cannot actually model a program that does anything as a pure function. There are two ways out of this:


  • 牺牲纯度。只需编写一个带副作用的函数。彻底记录,以便每个使用该功能的人都知道这一点。对程序的必要部分有不同的理由。

  • Sacrifice purity. Just write a function with a side effect. Document it thoroughly so that everyone using the function know about this. Reason differently about the imperative parts of your program.

这是大多数编程语言的首选解决方案,因为它们不会强制实现纯度,而且经常可以获得

This is the go-to solution in most programming languages, as they don't enforce purity and you can often enough get away with it.

构建一个(纯)数据结构,明确描述您希望程序拥有的效果。然后有一个不纯的解释器执行它们来运行你的程序。 (我不会在这里详细说明这样的数据结构如何工作,有不同的方法,解释超出了问题的范围)。

Build a (pure) data structure that explicitly describes the effects you want your program to have. Then have an impure "interpreter" that executes them to "run" your program. (I won't go into details here how such a data structure could work, there are different approaches and an explanation would go beyond the scope of the question).

In Haskell强制执行所有函数的纯度,这个数据结构是 IO 类型,它基本上描述了命令式计算,并且它的解释器内置于运行时。

In Haskell, which enforces purity of all functions, this data structure is the IO type that basically describes an imperative computation and the interpreter for it is built into the runtime.

在JavaScript中,没有这样的解释器。你可以自己构建一个(或者使用为此目的构建的库),但最终你必须在程序中的某个地方调用它的 run 函数,它会将你带回#1。

In JavaScript, there is no such interpreter. You can build one yourself (or use a library built for that purpose), but eventually you have to call its run function somewhere in your program which takes you back to #1.

对于DOM具体来说:用于描述文档中的更改的数据结构相对容易构建。您甚至可以引入纯粹的功能优化,例如:会避免多次写入同一位置,或者无论如何都要删除。

For the DOM specifically: a data structure to describe alterations in the document would be relatively easy to build. You could even introduce purely functional optimisations that e.g. would avoid writing multiple times to the same location, or to locations that subsequently will be removed anyway.

然而,DOM是JavaScript异步性质的基本组成部分。描述与多事输入和输出的潜在并发交互很难。

However, the DOM is fundamental part of JavaScript's asynchronous nature. Describing potentially-concurrent interaction with eventful inputs and outputs is hard.

这篇关于“功能性编程”背后的哲学是什么?当你真的想要创造副作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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