我的头不能包裹“抬起”在Ramda.js [英] Can't wrap my head around "lift" in Ramda.js

查看:118
本文介绍了我的头不能包裹“抬起”在Ramda.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

lift

liftN



下面是给出的示例:

<$ (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]

所以结果的第一个数很容易, a b c ,都是每个数组的第一个元素。第二个对我来说不是很容易理解。参数是每个数组的第二个值(2,2,未定义)还是第一个数组的第二个值和第二个和第三个数组的第一个值?



<即使无视这里发生的事情的顺序,我也不会看到它的价值。如果我在没有> lift 的情况下执行此操作,我会首先将数组 concat 作为字符串。这看起来像是像 flatMap 那样工作,但我似乎无法遵循它背后的逻辑。

解决方案

Bergi的回答非常好。但另一种考虑这个问题的方法是更具体一些。 Ramda确实需要在其文档中包含一个非列表示例,因为列表并没有真正捕获这个示例。



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

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

这个操作有三个数字。但是,如果你的容器拥有数字呢?也许我们有 也许 秒。我们不能简单地将它们加在一起:

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

(好吧,这是Javascript,它不会在这里引发错误,只是尝试连接事物它不应该......但它绝对不会做你想做的!)

如果我们可以将这个函数提升到容器的水平,它会使我们的生活更轻松Bergi指出的 lift3 在Ramda中用 liftN(3,fn)和一个光泽< lift(fn),它只是简单地使用提供的函数的arity。所以,我们可以这样做:

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

但是这个提升函数并不知道任何关于我们容器的具体内容,只是它们实现了 AP 。 Ramda为列表实现 ap ,类似于将函数应用于列表交叉产品中的元组,所以我们也可以这样做:

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

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


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

lift

liftN

Here's the given example:

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]

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?

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'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;

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!

(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!)

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()

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]

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天全站免登陆