将功能性编程原则应用于Javascript是一个非常可行的选择吗? [英] Is applying functional programming principles to Javascript a seriously viable option?

查看:85
本文介绍了将功能性编程原则应用于Javascript是一个非常可行的选择吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自Haskell的背景。我习惯于用递归函数和典型的高阶函数(折叠,贴图,过滤器等)和函数组合来完成任务。我现在正在开发node.js,并且我非常想尝试编写自己的模块来实现这些功能,这样我就可以在我的代码中以对我来说合理的方式使用它们。



我的问题基本上是:是否设置了Javascript来处理这种类型的负担?我明白,上述递归函数可以很容易地重构为迭代函数,但是通常我发现自己在函数内调用了很多函数,并且我不知道Javascript是否可以很好地处理这种类型的事情。我知道像Underscore这样的东西存在并实现了一些FP原则,但我的问题基本上归结为:在Javascript中进行功能编程是否是一种好的做法?如果没有,为什么不呢?



我也很抱歉如果这个问题对于SO来说太软了,但我不想开始把我自己的工具集合起来if它只是一旦它变得太大就会破坏一切。

解决方案

在我看来,你的问题的简短答案是肯定的 - - 在Javascript中应用函数式编程原则是可行的! (我相信对于大多数其他语言来说也是这样 - 通常应用FP原则可以获得一些东西)。

这里是我在Javascript中构建的一个函数解析器组合器库的示例。 (和此处它正在实施中)。这是很重要的,因为:1)它允许我通过组合构建解析器,这意味着我可以独立构建和测试小解析器,然后将它们放在一起并确信行为将是相同的,并且2)它使回溯超级容易得到正确的(这是非常重要的,因为选择运算符在替代失败时回溯)。

所以这些都是FP原则(注意没有递归,折叠,地图和过滤器),我发现在构建我的库中非常有用:


  • 避免可变状态$ / $>
  • 纯函数(即输出仅依赖于输入)
  • 组合:通过粘贴简单的部分构建复杂的应用程序


    在Javascript中应用这些方法通常很容易和愉快,因为Javascript支持:


    • 一等函数

    • 匿名函数

    • 词法关闭
    • >


    但这里有一些需要注意的事项:


    • 缺乏流行的高效函数式数据结构库
    • 缺少tail-call优化(至少现在是这样)

    • 部分应用程序更多语法重于Haskell
    • 许多流行的库不是特别有用的功能

    • DOM不起作用(按设计)
    • $然而,你最后的评论 - 我不想开始把我自己的工具集放在一起,如果它一旦它变得太快就会破坏所有的工具组合大 - 是一个很好的。这对于每一种方法来说基本上都是一个问题,我不能告诉你,当事情变得太大时,Javascript中的FP是否比主流技术更成问题。但我可以告诉你,根据我的经验,Javascript中的FP可以帮助我防止事情变得太大。


      I come from a background in Haskell. I'm very used to getting things done with recursive functions and the typical higher-order functions (folds, maps, filters, etc) and composing functions together. I'm developing in node.js now, and I'm seriously tempted to write my own modules implementing these functions so I can use them in my code in a way that makes sense to me.

      My question is, basically: is Javascript set up to handle this type of burden? I understand that the aforementioned recursive functions can be easily refactored into iterative ones, but often times I find myself calling a lot of functions within functions, and I don't know if Javascript can handle this type of thing well. I know that things like Underscore exist and implement some FP principles, but my question basically boils down to: is it good practice to program functionally in Javascript? If not, why not?

      I also apologize if this question is a little too soft for SO, but I don't want to start putting together my own tool set if it's just going to break everything once it gets too large.

      解决方案

      In my opinion, the short answer to your question is yes -- applying functional programming principles is viable in Javascript! (I believe that this is also true for most other languages -- there's usually something to be gained from applying FP principles).

      Here's an example of a functional parser combinator library I built in Javascript. (And here it is in action). It was important to be functional because: 1) it allows me to build parsers by composition, which means I can build and test small parsers independently, then put them together and have confidence that the behavior will be the same, and 2) it makes backtracking super easy to get right (which is important because the choice operator backtracks when an alternative fails).

      So these are FP principles (note the absence of recursion, folds, maps, and filters from this list) that I found extremely useful in building my library:

      • avoiding mutable state
      • pure functions (i.e. output depends only on input)
      • composition: building complex apps by gluing together simple pieces

      It's usually quite easy and pleasant to apply these in Javascript because of Javascript's support for:

      • first-class functions
      • anonymous functions
      • lexical closures

      but here are some things to watch out for:

      • lack of popular library of efficient functional data structures
      • lack of tail-call optimization (at least at the moment)
      • partial application is more syntax-heavy than in Haskell
      • lots of popular libraries are not especially functional
      • the DOM is not functional (by design)

      However, your last comment -- "I don't want to start putting together my own tool set if it's just going to break everything once it gets too large" -- is a good one. This is basically a problem for every approach, and I can't tell you whether FP in Javascript will be more problematic than "mainstream" techniques when things get too large. But I can tell you that in my experience, FP in Javascript helps me to prevent things from getting too large.

      这篇关于将功能性编程原则应用于Javascript是一个非常可行的选择吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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