ramda.js中chain()和map()之间的区别 [英] difference between chain() and map() in ramda.js

查看:244
本文介绍了ramda.js中chain()和map()之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

chain()(来自 ramda 包)和地图之间有什么区别? ()在Javascript中?

What is the difference between chain() (from ramda package) and map() in Javascript?

在这两个函数中,程序员输入一个对象和一些lambda /函数并获得一定的计算结果。谢谢。

In both functions the programmer inputs an object and some lambda/function and gets a certain calculation of it. Thanks.

推荐答案

抽象类型



chain map 每个都在抽象类型上运行。 map 适用于任何 Functor 即可。这是符合某些法律的 map 函数的任何项目。 chain Chain <上运行/ a> 元素。同样,这是一个合法的函数,以及合法 apply map functions。

Abstract Types

chain and map each operate on an abstract type. map operates on any Functor. This is any item with a map function that obeys certain laws. chain operates on a Chain element. Similarly, this is something with a lawful chain function, as well as having lawful apply and map functions.

Ramda提供 map chain 可用于履行这些合同的类型的函数。它还为某些内置类型( map 的函数,数组和对象以及链的函数和数组。)

Ramda provides map and chain functions that will work with types fulfilling these contracts. It also supplies implementation for certain built-in types (functions, arrays, and objects for map and functions and arrays for chain.)

要了解它们的不同之处,比较它们的签名就足够简单了:

To see how they differ, it's simple enough to compare their signatures:

// map   :: Functor f => (a → b)   → f a → f b
// chain :: Chain   m => (a → m b) → m a → m b

您可以这样考虑:提供的功能 map 获取A类型的项目并返回B类型的项目。 map 接受该函数和持有A类型的容器并返回一个持有B类的容器。相比之下,提供给 chain 的函数采用A类的项目并返回一个容器,其类型为B. code> chain 接受该函数和一个持有A类的容器,返回一个持有B类的容器。

You can think about it like this: the function supplied to map takes an item of type A and returns one of type B. map accepts that function and a container holding type A and returns a container holding type B. The function supplied to chain by contrast takes an item of type A and returns a container holding type B. chain accepts that function and a container holding type A, returning a container holding type B.

你可以考虑一下好像 chain 打开一个级别的容器,而不是 map

You can think about it as though chain unwraps one level of containers compared to map.

例如,假设我们有一个函数 factors ,它返回一个因子整数( factors(14)// => [1,2,7,14] ,例如。)以下是 map chain 可用于数字列表:

For example, let's say we had a function factors, which returns the factors of an integer (factors(14) //=> [1, 2, 7, 14], for instance.) Here is how map and chain would work on a list of numbers:

map(factors, [12, 15])   //=> [[1, 2, 3, 4, 6, 12], [1, 3, 5, 15]]
chain(factors, [12, 15]) //=> [1, 2, 3, 4, 6, 12, 1, 3, 5, 15]

或者,如果我们有 Maybe 类型用于简化使用子类型只需的空值处理来表示值和 Nothing 表示计算中的某些null。我们可能会编写一个安全的平方根函数,例如

Or if we had a Maybe type used to simplify null-handling with the subtypes Just to signify a value and Nothing to signify some null in the computation. We might write a safe square root function such as

const sqrt = (n) => n > 0 ? Just(Math.sqrt(n)) : Nothing()

然后我们看到 map chain

map(sqrt, Just(25)) //=> Just(Just(5))
chain(sqrt, Just(25)) //=> Just(5)

map(sqrt, Just(-25)) //=> Just(Nothing)
chain(sqrt, Just(-25)) //=> Nothing

最后,对于函数,出于 另一个SO答案

And finally, for functions, for reasons described in another SO answer,

map(f, g)   //~> x => f(g(x));
chain(f, g) //~> x => f(g(x))(x); 



摘要



你可以从他们看到签名, map chain 之间存在某种关系,但它们是不同的函数,用于非常不同的目的。 chain 与有时被称为 flatMap 的内容相关,因为它会平坦化(创建一种结果)通过 map

Summary

You can see from their signatures, that there is some relationship between map and chain, but they are distinct functions, used for very different purposes. chain is related to what is sometimes called flatMap, as it flattens out (one level) of the sort of result created by map.

但考虑它们的最佳方式是查看签名和与之相关的法律这些功能。

But the best way to think about them is to look at the signatures and the laws tied to these functions.

这篇关于ramda.js中chain()和map()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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