函数编程和模拟对象 [英] Function Programming and Mock Objects

查看:77
本文介绍了函数编程和模拟对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在观看Clojure的网络直播。主持人在讨论Clojure的FP性质的过程中发表了一个评论,这个评论类似于(我希望我不会歪曲他)模拟对象嘲笑你。

当微软的Reactive Framework开始出现时,当我观看网络广播时,我也听到了类似的评论。它变得像模拟对象是为那些不知道数学的人)

现在我知道这两个评论都是笑话/舌头等等等等(并可能严重地转述),但基础它们显然是一些概念,我不明白,因为我没有真正转向FP范式。

因此,我会很感激,如果有人能解释FP是否实际上使得嘲弄多余,如果是的话。怎么样。

在纯FP中,你具有引用透明的函数,每次用相同的输入调用它们时都会计算相同的输出。因此,所有你需要的状态必须作为参数显式传递,并作为函数结果传递出去,没有任何有状态的对象以某种方式隐藏在你调用的函数后面。然而,这是你的模拟对象通常所做的:模拟一些外部的,隐藏的状态或者你的测试对象依赖的行为。



换句话说:
OO:你的对象结合了相关的状态和行为。
Pure FP:State是你在通过它们本身是无状态的函数之间传递的,并且只依赖于其他无状态的函数。


I was recently watching a webcast on Clojure. In it the presenter made a comment in the context of discussing the FP nature of Clojure which went something like (I hope I don't misrepresent him) "Mock objects are mocking you".

I also heard a similar comment a while back when I watched a webcast when Microsoft's Reactive Framework was starting to appear . It went something like "Mock objects are for those who don't know math")

Now I know that both comments are jokes/tongue-in-cheek etc etc (and probably badly paraphrased), but underlying them is obviously something conceptual which I don't understand as I haven't really made the shift to the FP paradigm.

So, I would be grateful if someone could explain whether FP does in fact render mocking redundant and if so how.

解决方案

In pure FP you have referentially transparent functions that compute the same output every time you call them with the same input. All the state you need must therefore be explicitly passed in as parameters and out as function results, there are no stateful objects that are in some way "hidden behind" the function you call. This, however, is, what your mock objects usually do: simulate some external, hidden state or behavior that your subject under test relies on.

In other words: OO: Your objects combine related state and behavior. Pure FP: State is something you pass between functions that by themselves are stateless and only rely on other stateless functions.

这篇关于函数编程和模拟对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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