无法将我的头环绕在“抬起"上在 Ramda.js 中 [英] Can't wrap my head around "lift" in Ramda.js

查看:29
本文介绍了无法将我的头环绕在“抬起"上在 Ramda.js 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看 Ramda.js 的源代码,特别是lift"函数.

Looking at the source for Ramda.js, specifically at the "lift" function.

lift

liftN

这是给定的示例:

var madd3 = R.lift(R.curry((a, b, c) => a + b + c));

madd3([1,2,3], [1,2,3], [1]); //=> [3, 4, 5, 4, 5, 6, 5, 6, 7]

所以结果的第一个数字很简单,abc,都是每个数组的第一个元素.第二个对我来说并不容易理解.参数是每个数组的第二个值 (2, 2, undefined) 还是第一个数组的第二个值和第二个和第三个数组的第一个值?

So the first number of the result is easy, a, b, and c, are all the first elements of each array. The second one isn't as easy for me to understand. Are the arguments the second value of each array (2, 2, undefined) or is it the second value of the first array and the first values of the second and third array?

即使不考虑这里发生的事情的顺序,我也没有真正看到它的价值.如果我在没有 lift 的情况下执行它,我最终会得到数组 concat 作为字符串.这似乎有点像 flatMap 工作,但我似乎无法遵循它背后的逻辑.

Even disregarding the order of what's happening here, I don't really see the value. If I execute this without lifting it first I will end up with the arrays concatenated as strings. This appears to sort of be working like flatMap but I can't seem to follow the logic behind it.

推荐答案

Bergi 的回答很棒.但另一种思考方式是更具体一些.Ramda 确实需要在其文档中包含一个非列表示例,因为列表并没有真正捕捉到这一点.

Bergi's answer is great. But another way to think about this is to get a little more specific. Ramda really needs to include a non-list example in its documentation, as lists don't really capture this.

让我们用一个简单的函数:

Lets take a simple function:

var add3 = (a, b, c) => a + b + c;

这对三个数字进行操作.但是如果你有容器保存数字怎么办?也许我们有Maybes.我们不能简单地将它们加在一起:

This operates on three numbers. But what if you had containers holding numbers? Perhaps we have Maybes. We can't simply add them together:

const Just = Maybe.Just, Nothing = Maybe.Nothing;
add3(Just(10), Just(15), Just(17)); //=> ERROR!

(好吧,这是 Javascript,它实际上不会在这里抛出错误,只是尝试连接它不应该的东西......但它绝对不会做你想要的!)

(Ok, this is Javascript, it will not actually throw an error here, just try to concatenate thing it shouldn't... but it definitely doesn't do what you want!)

如果我们可以将该功能提升到容器级别,那将使我们的生活更轻松.Bergi 指出的 lift3 是在 Ramda 中使用 liftN(3, fn) 实现的,以及一个简单地使用的光泽 lift(fn)提供的函数的数量.所以,我们可以这样做:

If we could lift that function up to the level of containers, it would make our life easier. What Bergi pointed out as lift3 is implemented in Ramda with liftN(3, fn), and a gloss, lift(fn) that simply uses the arity of the function supplied. So, we can do:

const madd3 = R.lift(add3);
madd3(Just(10), Just(15), Just(17)); //=> Just(42)
madd3(Just(10), Nothing(), Just(17)); //=> Nothing()

但是这个提升的函数并不知道我们容器的任何特定信息,只知道它们实现了ap.Ramda 为列表实现 ap 的方式类似于将函数应用于列表的叉积中的元组,因此我们也可以这样做:

But this lifted function doesn't know anything specific about our containers, only that they implement ap. Ramda implements ap for lists in a way similar to applying the function to the tuples in the crossproduct of the lists, so we can also do this:

madd3([100, 200], [30, 40], [5, 6, 7]);
//=> [135, 136, 137, 145, 146, 147, 235, 236, 237, 245, 246, 247]

这就是我对 lift 的看法.它需要一个在某些值级别工作的函数,并将其提升为一个在这些值的容器级别工作的函数.

That is how I think about lift. It takes a function that works at the level of some values and lifts it up to a function that works at the level of containers of those values.

这篇关于无法将我的头环绕在“抬起"上在 Ramda.js 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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