在什么级别应用函数式编程方法

时间:2022-07-14 22:03:00

Should one apply functional programming practices at the design level, i.e. when we identify and design class hierarchy, or is it applicable only when it comes to writing function bodies?

是否应该在设计层面应用函数式编程实践,即在我们识别和设计类层次结构时,还是仅在编写函数体时才适用?

What I feel is that we do the design process applying normal OOPs techniques and write implementations using a functional approach.

我的感觉是我们使用普通的OOP技术进行设计过程,并使用功能方法编写实现。

Your thoughts, please!

请你的想法!

4 个解决方案

#1


Brian McNamara, of the F# Development team, has an interesting blog titled How does functional programming affect the structure of your code?.

F#开发团队的Brian McNamara有一个有趣的博客,名为“函数式编程如何影响代码结构?”。

I tend to agree with his assessment that functional programming tends to have the largest influence on actual code structure, at the level of implementation and individual routines, and less influence on overall architecture, at the level of modules and subsystems.

我倾向于同意他的评估,即在模块和子系统的层面上,函数式编程往往对实际代码结构,实现级别和单个例程的影响最大,对整体架构的影响较小。

That said, I also think that FP can influence how the problem is attacked, and can often lead to entirely different approaches, such as DSL's, async code, filter graphs, actors, etc., but these can all be considered "in the medium" solutions which don't effect the larger architecture, such as distributed, REST, client-server, etc.

也就是说,我也认为FP可以影响问题的攻击方式,并且通常可以导致完全不同的方法,例如DSL,异步代码,过滤器图,演员等,但这些都可以被认为是“在媒体中“不影响大型架构的解决方案,例如分布式,REST,客户端 - 服务器等。

#2


The design process should be OOP agnostic. Maybe we'll need objects and classes, maybe we won't. Maybe a large portion will use functional techniques. Maybe all it needs is a 5 minute perl script. It depends on what we're trying to accomplish.

设计过程应与OOP无关。也许我们需要对象和类,也许我们不会。也许很大一部分将使用功能技术。也许它只需要一个5分钟的perl脚本。这取决于我们想要实现的目标。

That said, an argument could be made that OOP may get in the way of a purely functional design. Other arguments could be made that it doesn't. Another argument could be made that you just need to use your best judgement and balance the two paradigms. Others will chime in and say the two approaches are orthogonal and can be combined without conflict.

也就是说,可以说OOP可能会妨碍纯粹的功能设计。可以提出其他论点,但事实并非如此。可以提出另一个论点,即您只需要使用最佳判断并平衡两种范式。其他人会说,这两种方法是正交的,可以合并而不会发生冲突。

But it's all abstract really. The answer is MU. Pose a real problem you have, and then we'll be able to tell you whether a functional or oop approach is appropriate, and at what level.

但它真的很抽象。答案是MU。提出一个真正的问题,然后我们将能够告诉你功能或oop方法是否合适,以及在什么级别。

#3


I am a bit biased towards the FP approach at design if you want to implement in FP.
But, Breton makes a good point that the selection is subjective.

如果你想在FP中实现,我对设计中的FP方法有点偏向。但是,布列塔尼提出了一个很好的观点,即选择是主观的。

References.

  1. Another * question: How can I use functional programming in the real world?.
  2. 另一个*问题:我如何在现实世界中使用函数式编程? JavaScript:使用函数式编程技术编写优雅的JavaScript F#primer:在.NET Framework中使用函数式编程技术讨论主题:为什么人们不使用函数式编程?链接:应用于实际任务的功能程序列表

#4


I agree that an OOP high-level decomposition with FP implementation techniques is a good strategy; I discuss this some in a blog:

我同意用FP实现技术进行OOP高级分解是一个很好的策略;我在博客中对此进行了讨论:

How does functional programming affect the structure of your code?

函数式编程如何影响代码的结构?

#1


Brian McNamara, of the F# Development team, has an interesting blog titled How does functional programming affect the structure of your code?.

F#开发团队的Brian McNamara有一个有趣的博客,名为“函数式编程如何影响代码结构?”。

I tend to agree with his assessment that functional programming tends to have the largest influence on actual code structure, at the level of implementation and individual routines, and less influence on overall architecture, at the level of modules and subsystems.

我倾向于同意他的评估,即在模块和子系统的层面上,函数式编程往往对实际代码结构,实现级别和单个例程的影响最大,对整体架构的影响较小。

That said, I also think that FP can influence how the problem is attacked, and can often lead to entirely different approaches, such as DSL's, async code, filter graphs, actors, etc., but these can all be considered "in the medium" solutions which don't effect the larger architecture, such as distributed, REST, client-server, etc.

也就是说,我也认为FP可以影响问题的攻击方式,并且通常可以导致完全不同的方法,例如DSL,异步代码,过滤器图,演员等,但这些都可以被认为是“在媒体中“不影响大型架构的解决方案,例如分布式,REST,客户端 - 服务器等。

#2


The design process should be OOP agnostic. Maybe we'll need objects and classes, maybe we won't. Maybe a large portion will use functional techniques. Maybe all it needs is a 5 minute perl script. It depends on what we're trying to accomplish.

设计过程应与OOP无关。也许我们需要对象和类,也许我们不会。也许很大一部分将使用功能技术。也许它只需要一个5分钟的perl脚本。这取决于我们想要实现的目标。

That said, an argument could be made that OOP may get in the way of a purely functional design. Other arguments could be made that it doesn't. Another argument could be made that you just need to use your best judgement and balance the two paradigms. Others will chime in and say the two approaches are orthogonal and can be combined without conflict.

也就是说,可以说OOP可能会妨碍纯粹的功能设计。可以提出其他论点,但事实并非如此。可以提出另一个论点,即您只需要使用最佳判断并平衡两种范式。其他人会说,这两种方法是正交的,可以合并而不会发生冲突。

But it's all abstract really. The answer is MU. Pose a real problem you have, and then we'll be able to tell you whether a functional or oop approach is appropriate, and at what level.

但它真的很抽象。答案是MU。提出一个真正的问题,然后我们将能够告诉你功能或oop方法是否合适,以及在什么级别。

#3


I am a bit biased towards the FP approach at design if you want to implement in FP.
But, Breton makes a good point that the selection is subjective.

如果你想在FP中实现,我对设计中的FP方法有点偏向。但是,布列塔尼提出了一个很好的观点,即选择是主观的。

References.

  1. Another * question: How can I use functional programming in the real world?.
  2. 另一个*问题:我如何在现实世界中使用函数式编程? JavaScript:使用函数式编程技术编写优雅的JavaScript F#primer:在.NET Framework中使用函数式编程技术讨论主题:为什么人们不使用函数式编程?链接:应用于实际任务的功能程序列表

#4


I agree that an OOP high-level decomposition with FP implementation techniques is a good strategy; I discuss this some in a blog:

我同意用FP实现技术进行OOP高级分解是一个很好的策略;我在博客中对此进行了讨论:

How does functional programming affect the structure of your code?

函数式编程如何影响代码的结构?