为什么民间传说和ramda如此不同? [英] why are folktale and ramda so different?

查看:132
本文介绍了为什么民间传说和ramda如此不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过阅读DrBoolean的来学习FP.

I'm learning javascript FP by reading DrBoolean's book.

我到处搜索了功能编程库.我找到了Ramda和Folktale.两者都声称是功能编程库.

I searched around for functional programming library. I found Ramda and Folktale. Both claim to be functional programming library.

但是它们是如此不同:

  • Ramda 似乎包含用于处理列表的实用函数:map,reduce,filter和pure函数:咖喱,组成.函子不包含任何处理monad的东西.

  • Ramda seems 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.

民间传说但是不包含任何用于列表或函数的实用程序.似乎在JavaScript中实现了像monad这样的代数结构:Maybe,Task ...

Folktale however doesn't contain any utility for list or functions. It seems to implement the some algebraic structures in javascript like monad: Maybe, Task...

实际上,我发现了更多的库,它们似乎都属于两类.下划线和lodash就像Ramda.幻想世界,无意义的幻想就像民间故事.

Actually I found more libraries, they all seem fall into the two categories. Underscore and lodash are like Ramda. Fantasy-land, pointfree-fantasy are like folktale.

这些完全不同的库都可以称为 functional 吗?如果这样,是什么使每个库都成为功能库?

Can these very different libraries both be called functional, and if so, what makes each one a functional library?

推荐答案

功能特点

定义函数式编程或函数库没有明确的界限. Java语言内置了功能语言的某些功能:

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:

  • 一流的高阶函数
  • Lambdas/匿名函数,带有闭包

其他人也可以小心地用Java语言完成:

Others are possible to accomplish in Javascript with some care:

  • 不可移植性
  • 参照透明

还有其他人是ES6的一部分,并且现在部分或完全可用:

Still others are part of ES6, and partially or fully available right now:

  • 紧凑甚至简洁的功能
  • 通过尾部调用优化实现高性能递归

还有很多其他的东西实际上超出了Javascript的正常范围:

And there are plenty of others which are really beyond the normal reach of Javascript:

  • 模式匹配
  • 懒惰评估
  • 同声性

然后,一个库可以选择要支持的功能类型,并且仍然可以合理地称为功能性".

A library, then, can pick and choose what sorts of features it's trying to support and still reasonably be called "functional".

幻想土地是从数学范畴论移植的许多标准类型的规范和函数式编程的抽象代数,例如 Monoid Monad .这些类型是相当抽象的,并且扩展了可能更熟悉的概念.例如,函子是可以使用函数map遍历容器的容器,可以使用Array.prototype.map遍历数组的方法.

Fantasy-land is 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 mapped over with a function, the way an array can be mapped over using Array.prototype.map.

民间传说是实现幻想世界规范各个部分的类型的集合以及一小部分同伴实用程序功能.这些类型类似于也许验证

Folktale is 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

《民间传说》也许是《幻想土地》规范最著名的实现,并且受到了广泛的尊重.但是没有确定的或默认的实现. Fantasy-land仅指定抽象类型,并且实现当然必须创建此类具体类型. Folktale声称是功能库的说法很明确:它提供了通常在功能编程语言中找到的数据类型,这些数据类型使以功能方式进行编程变得更加容易.

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.

此示例摘自民间传说文档(注释:不在最新版本的文档中),说明了如何使用它:

This example, from the Folktale documentation (note: not in recent versions of the docs), shows how it might be used:

// 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 (免责声明:我是作者之一)是一种非常不同的库.它不会为您提供新类型. 1 而是提供了一些功能,使您可以更轻松地对现有类型进行操作.它围绕以下概念构建:将较小的功能组合为较大的功能,使用不可变数据,避免副作用.

Ramda

Ramda (disclaimer: I'm one of the authors) is a very different type of library. It does not provide new types for you.1 Instead, 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尤其适用于列表,但也适用于对象,有时甚至适用于字符串.它还以与Folktale或其他Fantasy-land实现互操作的方式委派了许多调用.例如,Ramda的map函数的操作类似于Array.prototype上的函数,因此R.map(square, [1, 2, 3, 4]); //=> [1, 4, 9, 16].但是,由于Folktale的Maybe实现了同时指定地图的Fantasy-land Functor规范,因此您也可以将Ramda的map与它一起使用:

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:

R.map(square, Maybe.Just(5)); //=> Maybe.Just(25);
R.map(square, Maybe.Nothing); //=> Maybe.Nothing

Ramda声称是功能库的原因在于它使函数的编写变得容易,从不对数据进行变异,并且仅提供纯函数.如拉姆达哲学

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

// :: [Comment] -> [Number]  
var userRatingForComments = R.pipe(
    R.pluck('username')      // [Comment] -> [String]
    R.map(R.propOf(users)),  // [String] -> [User]
    R.pluck('rating'),       // [User] -> [Number]
);

其他图书馆

实际上,我发现了更多的库,它们似乎都属于两类.下划线,lodash非常像Ramda.幻想世界,无意义的幻想就像民间故事.

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.

那不是很准确.首先,Fantasy-land只是图书馆可以决定针对各种类型实现的规范. Folktale是该规范的许多实现之一,可能是最全面的实现,当然也是最成熟的实现之一. Pointfree-fantasy

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-fantasy and ramda-fantasy are others, and there are many more.

下划线 lodash 从表面上看就像Ramda,因为它们是抓包库,提供的功能很多,而内聚性却比Folktale之类的要小得多.甚至特定的功能通常也与Ramda的功能重叠.但是从更深层次上讲,Ramda与这些库的关注点完全不同. Ramda最接近的表亲可能是 FKit Wu.js .

Underscore and lodash are 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.

Bilby 属于自己的类别,提供了很多工具,例如由Ramda和某些类型与Fantasy-land一致. (《比尔比》的作者也是《幻想世界》的原作者.)

Bilby is 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.)

所有这些库都有权被称为功能库,尽管它们在功能方法和功能承诺程度上差异很大.

All of these libraries have right to be called functional, although they vary greatly in functional approach and degree of functional commitment.

其中一些库实际上可以很好地协同工作. Ramda应该与Folktale或其他Fantasy-land实现良好配合.由于它们的关注点几乎没有重叠,因此它们确实没有冲突,但是Ramda所做的足以使互操作相对平稳.对于您可能选择的其他某些组合来说,这可能不太正确,但是ES6的简单函数语法也可能减轻了集成的麻烦.

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.

要使用的库,甚至是库的样式,将取决于您的项目和偏好.有很多不错的选择,而且数量还在不断增加,其中许多正在大大改善.现在是在JS中进行函数式编程的好时机.

The choice of library, or even style of 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.

1 嗯,有一个附带项目, ramda-fantasy 所做的事情与Folktale相似,但它不是核心库的一部分.

1Well, there is a side-project, ramda-fantasy doing something similar to what Folktale does, but it's not part of the core library.

这篇关于为什么民间传说和ramda如此不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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