javascript 为什么民间故事和拉姆达如此不同?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33027305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
why are folktale and ramda so different?
提问by Aaron Shen
I'm learning javascript FP by reading DrBoolean's book.
我正在通过阅读 DrBoolean 的书来学习 javascript FP 。
I searched around for functional programming library. I found Ramda and Folktale. Both claim to be functional programming library.
我四处寻找函数式编程库。我找到了 Ramda 和 Folktale。两者都声称是函数式编程库。
But they are so different:
但它们是如此不同:
Ramdaseems to contain utility functions for dealing with list: map, reduce, filter and pure functions: curry, compose. It doesn't contain anything to deal with monad, functor.
Folktalehowever doesn't contain any utility for list or functions. It seems to implement the some algebraic structures in javascript like monad: Maybe, Task...
Ramda似乎包含处理列表的实用函数:map、reduce、filter 和纯函数:curry、compose。它不包含任何处理 monad、functor 的内容。
然而,Folktale不包含任何用于列表或函数的实用程序。它似乎在 javascript 中实现了一些代数结构,如 monad: Maybe, Task...
Actually I found more libraries, they all seem fall into the two categorys. underscore, lodash are very like Ramda. Fantasy-land, pointfree-fantasy are like folktale.
其实我发现了更多的图书馆,他们似乎都属于这两类。下划线,lodash 很像 Ramda。幻想之地,自由幻想就像民间故事。
Can these very different libraries both be called functional, and if so, what makes each one a functional library?
难道这很不相同的库都被称为功能,如果有,是什么让每一个功能库?
回答by Scott Sauyet
Functional Features
功能特点
There is no clear-cut boundary of what defines functional programming or a functional library. Some features of functional languages are built into Javascript:
函数式编程或函数库的定义没有明确的界限。Javascript 内置了函数式语言的一些特性:
- First-class, higher-order functions
- Lambdas/Anonymous functions, with closures
- 一等高阶函数
- Lambdas/匿名函数,带闭包
Others are possible to accomplish in Javascript with some care:
其他一些可以在 Javascript 中小心完成:
- Immutability
- Referential Transparency
- 不变性
- 参照透明度
Still others are part of ES6, and partially or fully available right now:
还有一些是 ES6 的一部分,现在部分或完全可用:
- Compact, even terse, functions
- Performant recursion through tail-call optimization
- 紧凑、简洁的功能
- 通过尾调用优化的高性能递归
And there are plenty of others which are really beyond the normal reach of Javascript:
还有很多其他的确实超出了 Javascript 的正常范围:
- Pattern matching
- Lazy evaluation
- Homoiconicity
- 模式匹配
- 懒惰评价
- 同音性
A library, then, can pick and choose what sorts of features it's trying to support and still reasonably be called "functional".
一个库,然后,可以挑选和选择它试图支持什么样的特性,并且仍然合理地被称为“功能性”。
Fantasy-land specification
梦幻乐园规格
Fantasy-landis a specification for a number of the standard types ported from mathematical Category Theory and Abstract Algebra to functional programming, types such as Monoid, Functor, and Monad. These types are fairly abstract, and extend possibly more familiar notions. Functors, for instance, are containers which can be map
ped over with a function, the way an array can be map
ped over using Array.prototype.map
.
Fantasy-land是许多标准类型的规范,从数学范畴论和抽象代数移植到函数式编程,类型如Monoid、Functor和Monad。这些类型相当抽象,并且扩展了可能更熟悉的概念。例如,map
函子是可以用函数遍历的容器,就像map
使用 遍历数组的方式一样Array.prototype.map
。
Folktale
民间故事
Folktaleis a collection of types implementing various parts of the Fantasy-land specification and a small collection of companion utility functions. These types are things like Maybe, Either, Task(very similar to what is elsewhere called a Future, and a more lawful cousin to a Promise), and Validation
Folktale是实现 Fantasy-land 规范各个部分的类型集合和一小部分配套实用程序函数。这些类型是诸如Maybe、either、Task(与其他地方称为 Future 的东西非常相似,并且是 Promise 的更合法的表亲)和Validation 之类的东西
Folktale is perhaps the best-known implementation of the Fantasy-land specification, and it is well-respected. But there is no such thing as a definitive or default implementation; fantasy-land only specifies abstract types, and an implementation of course must create such concrete types. Folktale's claim to being a functional library is clear: it provides data types found typically in functional programming languages, ones which make it substantially easier to program in a functional manner.
Folktale 可能是 Fantasy-land 规范最著名的实现,并且备受推崇。但是没有明确的或默认的实现;Fantasy-land 只指定抽象类型,当然一个实现必须创建这样的具体类型。Folktale 声称自己是一个函数式库,这一点很明确:它提供了函数式编程语言中常见的数据类型,这些数据类型使得以函数式方式编程变得更加容易。
This example, from the Folktale documentation(note: not in recent versions of the docs), shows how it might be used:
这个来自Folktale 文档的示例(注意:不在最新版本的文档中),展示了如何使用它:
// We load the library by "require"-ing it
var Maybe = require('data.maybe')
// Returns Maybe.Just(x) if some `x` passes the predicate test
// Otherwise returns Maybe.Nothing()
function find(predicate, xs) {
return xs.reduce(function(result, x) {
return result.orElse(function() {
return predicate(x)? Maybe.Just(x)
: /* otherwise */ Maybe.Nothing()
})
}, Maybe.Nothing())
}
var numbers = [1, 2, 3, 4, 5]
var anyGreaterThan2 = find(function(a) { return a > 2 }, numbers)
// => Maybe.Just(3)
var anyGreaterThan8 = find(function(a) { return a > 8 }, numbers)
// => Maybe.Nothing
Ramda
拉姆达
Ramda(disclaimer: I'm one of the authors) is a very different type of library. It does not provide new types for you.1Instead, it provides functions to make it easier to operate on existing types. It is built around the notions of composing smaller functions into larger ones, of working with immutable data, of avoiding side-effects.
Ramda(免责声明:我是作者之一)是一种非常不同类型的库。它不会为您提供新类型。1相反,它提供了更容易对现有类型进行操作的功能。它围绕将较小的函数组合成较大的函数、处理不可变数据、避免副作用的概念而构建。
Ramda operates especially on lists, but also on objects, and sometimes on Strings. It also delegates many of its calls in such a manner that it will interoperate with Folktale or other Fantasy-land implementations. For instance, Ramda's map
function, operates similarly to the one on Array.prototype
, so R.map(square, [1, 2, 3, 4]); //=> [1, 4, 9, 16]
. But because Folktale's Maybe
implements the Fantasy-land Functor
spec, which also specifies map, you can also use Ramda's map
with it:
Ramda 尤其适用于列表,但也适用于对象,有时也适用于字符串。它还以与 Folktale 或其他 Fantasy-land 实现互操作的方式委派了许多调用。例如,Ramda 的map
函数,其操作与 上的函数类似Array.prototype
,所以R.map(square, [1, 2, 3, 4]); //=> [1, 4, 9, 16]
。但是因为 FolktaleMaybe
实现了 Fantasy-landFunctor
规范,它也指定了地图,你也可以使用 Ramda 的map
:
R.map(square, Maybe.Just(5)); //=> Maybe.Just(25);
R.map(square, Maybe.Nothing); //=> Maybe.Nothing
Ramda's claims to being a functional library lie in making it easy to compose functions, never mutating your data, and presenting only pure functions. Typical usage of Ramda would be to build up more complex function by composing smaller ones, as seen in an article on the philosphy of Ramda
Ramda 声称自己是一个函数库,因为它可以轻松组合函数,永远不会改变您的数据,并且只呈现纯函数。Ramda 的典型用法是通过组合较小的函数来构建更复杂的函数,如有关Ramda 哲学的文章中所见
// :: [Comment] -> [Number]
var userRatingForComments = R.pipe(
R.pluck('username') // [Comment] -> [String]
R.map(R.propOf(users)), // [String] -> [User]
R.pluck('rating'), // [User] -> [Number]
);
Other Libraries
其他图书馆
Actually I found more libraries, they all seem fall into the two categorys. underscore, lodash are very like Ramda. Fantasy-land, pointfree-fantasy are like folktale.
其实我发现了更多的图书馆,他们似乎都属于这两类。下划线,lodash 很像 Ramda。幻想之地,自由幻想就像民间故事。
That's not really accurate. First of all, Fantasy-land is simply a specification that libraries can decide to implement for various types. Folktale is one of many implementations of that specification, probably the best-rounded one, certainly one of the most mature. Pointfree-fantasyand ramda-fantasyare others, and there are many more.
这并不准确。首先,Fantasy-land 只是图书馆可以决定为各种类型实现的规范。Folktale 是该规范的众多实现之一,可能是最全面的实现,当然也是最成熟的实现之一。 Pointfree-fantasy和ramda-fantasy是其他的,还有更多。
Underscoreand lodashare superficially like Ramda in that they are grab-bag libraries, providing a great number of functions with much less cohesion than something like Folktale. And even the specific functionality often overlaps with Ramda's. But at a deeper level, Ramda has very different concerns from those libraries. Ramda's closest cousins are probably libraries like FKit, Fnuc, and Wu.js.
下划线和lodash,表面上看好像Ramda因为它们抓斗袋库,提供的功能有很大数量少得多的凝聚力不是像民间故事。甚至特定的功能也经常与 Ramda 的重叠。但在更深层次上,Ramda 与那些库有着非常不同的关注点。Ramda 的近亲可能是像FKit、Fnuc和Wu.js这样的库。
Bilbyis in a category of it's own, providing both a number of tools such as the ones provided by Ramda and some types consistent with Fantasy-land. (The author of Bilby is the original author of Fantasy-land as well.)
Bilby属于它自己的一个类别,它提供了许多工具,例如 Ramda 提供的工具和一些与 Fantasy-land 一致的类型。(Bilby 的作者也是 Fantasy-land 的原作者。)
Your Call
您的来电
All of these libraries have right to be called functional, although they vary greatly in functional approach and degree of functional commitment.
所有这些库都有权被称为功能性的,尽管它们在功能方法和功能承诺程度方面差异很大。
Some of these libraries actually work well together. Ramda should work well with Folktale or other Fantasy-land implementations. Since their concerns barely overlap, they really don't conflict, but Ramda does just enough to make the interoperation relatively smooth. This is probably less true for some of the other combinations you could choose, but ES6's simpler function syntax may also take some of the pain out of integrating.
其中一些库实际上可以很好地协同工作。Ramda 应该可以很好地与 Folktale 或其他 Fantasy-land 实现配合使用。由于它们的关注点几乎没有重叠,因此它们确实不冲突,但 Ramda 所做的足以使互操作相对顺畅。对于您可以选择的其他一些组合,这可能不太正确,但是 ES6 更简单的函数语法也可以减轻集成的痛苦。
The choice of library, or even styleof library to use, is going to depend on your project and your preferences. There are lots of good options available, and the numbers are growing, and many of them are improving greatly. It's a good time to be doing functional programming in JS.
库的选择,甚至是使用的库风格,将取决于您的项目和您的偏好。有很多不错的选择可供选择,而且数量还在增长,其中许多正在大大改进。现在是在 JS 中进行函数式编程的好时机。
1Well, there is a side-project, ramda-fantasydoing something similar to what Folktale does, but it's not part of the core library.
1嗯,有一个副项目,ramda-fantasy做类似于 Folktale 所做的事情,但它不是核心库的一部分。