本文介绍了无法捕获“R.applySpec"中的参数: 拉姆达.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上是想在 R.applySpec 中实现下面的代码.

I am basically trying to achieve the code below within the R.applySpec.

const fn = ({target, count}) => R.unnest (R.zipWith (R.repeat) (target, count))

const Data = {
  target : ["a", "b", "c"],
  count : [1, 2, 3],
}

const data1= {
  result : fn (Data)
}
console.log ( 'data1:', data1.result);// ["a","b","b","c","c","c"]

我无法弄清楚的是 fn 中的参数似乎在 R.applySpec

What I cannot figure out is that arguments in the fn seems to be uncaught within the R.applySpec

const data2_applySpec = R.applySpec({
  result : R.lift ( R.zipWith ( fn )) ( R.prop ('target'),  R.prop ('count'))
})

const data2 = data2_applySpec(Data)
console.log ('data2:', data2);//ERROR

如何更改 fn 以使其工作?我使用 Ramda.js.谢谢.

How can I alter the fn to make it work?I use Ramda.js.Thanks.

REPL

推荐答案

我认为你让这件事变得比需要的更难.

I think you're making this harder than it needs to be.

您已经将要在 applySpec 中使用的函数存储为 fn.

You already have the function you want to use inside applySpec stored as fn.

所以你可以写:

const fn2 = applySpec ({
  result: fn
})

或者,如果您对 fn 的唯一使用是在这个 applySpec 调用中,那么只需将其内联:

Or, if your only use of fn is inside this applySpec call, then just inline it:

const fn3 = applySpec ({
  result: ({target, count}) => unnest (zipWith (repeat) (target, count))
})

如果您迷恋无点代码,您可以使用您在之前的博文中讨论的技术::>

And if you have a fetish for point-free code, you can use the technique discussed in your earlier post:

const fn4 = applySpec ({
  result: compose (unnest, apply (zipWith (repeat)), props (['target', 'count']))
})

(或 Ori Drori 的类似版本.)

(or the similar version from Ori Drori.)

所有这些都显示在这个片段中.

All of these are shown in this snippet.

const fn1 = ({target, count}) => unnest (zipWith (repeat) (target, count))

const fn2 = applySpec ({
  result: fn1
})

const fn3 = applySpec ({
  result: ({target, count}) => unnest (zipWith (repeat) (target, count))
})

const fn4 = applySpec ({
  result: compose (unnest, apply (zipWith (repeat)), props (['target', 'count']))
})


const data = {target : ["a", "b", "c"], count : [1, 2, 3]}

console .log (fn1 (data))
console .log (fn2 (data))
console .log (fn3 (data))
console .log (fn4 (data))
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js"></script>
<script> const {unnest, zipWith, repeat, applySpec, compose, apply, props} = R </script>

这篇关于无法捕获“R.applySpec"中的参数: 拉姆达.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 14:02