ramda.js中chain()和map()之间的区别

编程入门 行业动态 更新时间:2024-10-20 21:02:25
本文介绍了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 元素。同样,这是一个合法的链函数,以及合法 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()之间的区别

本文发布于:2023-11-26 00:22:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631920.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:区别   js   ramda   map   chain

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!