功能规划:状态与重新分配 [英] Functional programming: state vs. reassignment

查看:145
本文介绍了功能规划:状态与重新分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助了解我当前的OOP状态概念与在Haskell或Clojure之类的函数式语言中所做的区别。

I need help getting my head around the difference between my current OOP notion of state, and the way it would be done in a functional language like Haskell or Clojure.

为了使用一个哈希的例子,让我们说,我们处理简化的银行帐户对象/结构/任何。在OOP语言中,我会有一些类来保存对BankAccount的引用,BankAccount将包含利率等实例变量,以及setInterestRate()等方法,它们改变对象的状态,通常不返回任何内容。在Clojure中,我有一个银行帐户结构(一个荣耀的hashmap)和特殊功能,需要银行帐户参数和其他信息,并返回一个新的结构。因此,不是改变原始对象的状态,我现在有一个新的返回与所需的修改。

To use a hackneyed example, let's say we're dealing with simplified bank account objects/structs/whatever. In an OOP language, I'd have some class holding a reference to a BankAccount, which would have instance variables for things like interest rate, and methods like setInterestRate() which change the object's state and generally return nothing. In say Clojure, I'd have a bank-account struct (a glorified hashmap), and special functions that take a bank-account parameter and other info, and return a new struct. So instead of changing the state of the original object, I now have a new one being returned with the desired modifications.

那么...我该怎么办呢?覆盖任何引用旧银行帐户的变量?如果是这样,那比国家变化的OOP方法有优势吗?最后,在这两种情况下,似乎有一个变量引用对象与必要的更改。像我一样,我只有一个模糊的概念,发生了什么。

So... what do I do with it? Overwrite whatever variable was referencing the old bank-account? If so, does that have advantages over the state-changing OOP approach? In the end, in both cases it seems one has a variable that references the object with the necessary changes. Retarded as I am, I have only a vague concept of what's going on.

我希望有意义,感谢您的帮助!

I hope that made sense, thanks for any help!

推荐答案

在纯粹的功能风格中,你永远不会覆盖任何变量。

In a pure functional style, you'll never overwrite any variable.

类比是物理学中的时空。如果你认为世界是3d的,那么对象没有固定的位置 - 它们随着时间移动。为了使物理世界上的数学,我们因此添加一个时间维度,并考虑在特定时间的各种属性的价值。在这样做的时候,我们将我们的研究对象变成了常量。类似地,在编程中,通过使用不可变值有一个概念上的简单性。在现实世界中具有身份的对象可以被建模为不可变值的序列(对象在增加的时间的状态),而不是作为改变的单个值。

An analogy would be to spacetime in physics. If you consider the world as 3d, then objects don't have fixed positions - they move over time. To bring math to bear on the physical world, we therefore add a time dimension and consider the values of various properties at particular times. In doing so, we've made the objects of our study into constants. Similarly, in programming, there is a conceptual simplicity to be had by working with immutable values. Objects with an identity in the real world can be modeled as a sequence of immutable values (the states of the object at increasing times) rather than as a single value that changes.

当然,如何将值序列与对象标识相关联的细节可能有点毛。 Haskell有 Monads ,可让您建模状态。 功能反应性编程是一个更加直接的尝试,在世界上用纯功能更新建模对象,我认为这是一个非常有前途的方向编程。

Of course the details of how to associate the sequence of values to an "object identity" can be a little hairy. Haskell has Monads that let you model state. Functional Reactive Programming is a more literal attempt at modeling objects in the world with pure functional updates, that I think is a very promising direction for programming.

我会注意到,Clojure,不像Haskell,不是纯粹的,你可以更新变量,你建议。如果你只是在高级别更新几个变量,你仍然可能享受许多功能编程的概念简单的好处。

I will note that Clojure, unlike Haskell, isn't pure, and you can update variables as you suggested. If you're only updating a few variables at a high level, you'll still probably enjoy many of the conceptual simplicity benefits of functional programming.

这篇关于功能规划:状态与重新分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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