哪些函数式语言技术可以在命令式语言中使用?

发布于 2024-07-14 12:41:16 字数 397 浏览 5 评论 0原文

哪些通常与函数式语言相关的技术或范例也可以有效地用于命令式语言?

例如:

  • 在没有尾部调用优化的语言中,递归可能会出现问题,将其使用限制在少数情况下,因此其用处有限
  • Map 和过滤器已经找到了进入非函数式语言的方法,即使它们具有函数式排序对他们来说,

我碰巧真的很喜欢不必担心函数式语言中的状态。 如果我特别固执,我可能会编写 C 程序而不修改变量,只将我的状态封装在传递给函数的变量和从函数返回的值中。

即使函数不是第一类值,我也可以将一个函数包装在 Java 中的一个对象中,然后将其传递到另一个方法中。 就像函数式编程一样,只是没那么有趣。

那么,对于函数式编程的老手来说,当你用命令式语言编程时,你成功应用了 FP 的哪些思想呢?

Which techniques or paradigms normally associated with functional languages can productively be used in imperative languages as well?

e.g.:

  • Recursion can be problematic in languages without tail-call optimization, limiting its use to a narrow set of cases, so that's of limited usefulness
  • Map and filter have found their way into non-functional languages, even though they have a functional sort of feel to them

I happen to really like not having to worry about state in functional languages. If I were particularly stubborn I might write C programs without modifying variables, only encapsulating my state in variables passed to functions and in values returned from functions.

Even though functions aren't first class values, I can wrap one in an object in Java say, and pass that into another method. Like Functional programming, just less fun.

So, for veterans of functional programming, when you program in imperative languages, what ideas from FP have you applied successfully?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧瑾黎汐 2024-07-21 12:41:16

几乎全部?

如果您了解函数式语言,则可以编写由函数式风格“通知”的命令式程序。 这将使您远离副作用,并转向在任何特定点阅读程序文本足以让您真正了解程序在该点的含义的程序。

回到《黎明之时》,我们曾经担心“耦合”和“内聚”。 学习 FP 将引导您编写具有最佳(最小)耦合和高内聚性的系统。

Pretty nearly all of them?

If you understand functional languages, you can write imperative programs that are "informed" by a functional style. That will lead you away from side effects, and toward programs in which reading the program text at any particular point is sufficient to let you really know what the meaning of the program is at that point.

Back at the Dawn of Time we used to worry about "coupling" and "cohesion". Learning an FP will lead you to write systems with optimal (minimal) coupling, and high cohesion.

山有枢 2024-07-21 12:41:16

以下是在非 FP 语言中妨碍 FP 的因素:

  • 如果该语言不支持 lambda/closures,并且没有任何语法糖可以轻松破解它,你已经死在水里了。 你不会在没有闭包的情况下调用map/filter。
  • 如果语言是静态类型的并且不支持泛型,那么你就死在水里了。 所有好的 FP 东西都使用通用性。
  • 如果该语言不支持尾递归,那么您就会受到阻碍。 您可以迭代地编写例如“map”的实现; 通常你的数据可能不会太大,递归就可以了。
  • 如果该语言不支持代数数据类型和模式匹配,您将会受到轻微的阻碍。 一旦你尝过它们却没有再吃,这真是太烦人了。
  • 如果该语言无法表达类型类,那么,哦,好吧...你会过去的,但是该死的,如果这不仅仅是有史以来最棒的功能,而且 Haskell 是唯一一种远程流行且拥有良好支持的语言。

Here are things that get in the way of doing FP in a non-FP language:

  • If the language doesn't support lambda/closures, and doesn't have any syntactic sugar to easily mostly hack it, you are dead in the water. You don't call map/filter without closures.
  • If the language is statically-typed and doesn't support generics, you are dead in the water. All the good FP stuff uses genericity.
  • If the language doesn't support tail-recursion, you are hindered. You can write implementations of e.g. 'map' iteratively; also often your data may not be too large and recursion will be ok.
  • If the language does not support algebraic data types and pattern-matching, you will be mildly hindered. It's just annoying not to have them once you've tasted them.
  • If the language cannot express type classes, well, oh well... you'll get by, but darn if that's not just the awesomest feature ever, but Haskell is the only remotely popular language with good support.
瞎闹 2024-07-21 12:41:16

没有一流的函数确实会阻碍编写函数式程序,但是您可以做一些不需要它们的事情。 第一个是避免可变状态 - 尝试让大多数或所有类返回表示修改状态的新对象,而不是在内部进行更改。 例如,如果您正在使用 add 操作编写链表,您可能希望从 add 返回新的链表,而不是修改对象。

虽然这可能会降低您的程序效率(由于创建和销毁的对象数量增加),但您将能够更轻松地调试程序,因为对象的状态和操作变得更加可预测,更不用说能够嵌套函数调用更深入,因为它们有状态输入和输出。

Not having first-class functions really puts a damper on writing functional programs, but there are a few things that you can do that don't require them. The first is to eschew mutable state - try to have most or all of your classes return new objects that represent the modified state instead of making the change internally. As an example, if you were writing a linked list with an add operation, you would want to return the new linked list from add as opposed to modifying the object.

While this may make your programs less efficient (due to the increased number of objects being created and destroyed) you will gain the ability to more easily debug the program because the state and operation of the objects becomes more predictable, not to mention the ability to nest function calls more deeply because they have state inputs and outputs.

浅笑轻吟梦一曲 2024-07-21 12:41:16

我已经成功地使用了很多高阶函数,尤其是传入的函数而不是返回的函数。 返回的类型可能有点乏味,但可以模拟。

各种应用数据结构和递归函数在命令式语言中都能很好地工作。

我最怀念的事情:

  • 几乎没有命令式语言保证优化每个尾部调用。

  • 据我所知,没有任何命令式语言支持通过模式匹配进行案例分析。

I've successfully used higher-order functions a lot, especially the kind that are passed in rather than the kind that are returned. The kind that are returned can be a bit tedious but can be simulated.

All sorts of applicative data structures and recursive functions work well in imperative languages.

The things I miss the most:

  • Almost no imperative languages guarantee to optimize every tail call.

  • I know of no imperative language that supports case analysis by pattern matching.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文