Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Ramda.js中镜头的无点合成_Javascript_Functional Programming_Ramda.js - Fatal编程技术网

Javascript Ramda.js中镜头的无点合成

Javascript Ramda.js中镜头的无点合成,javascript,functional-programming,ramda.js,Javascript,Functional Programming,Ramda.js,我正在尝试编写返回镜头的函数,以生成一个新镜头,并以无点的方式进行 这可能是关于函数组合的更一般的问题。镜片只是一个案例研究。我对透镜不感兴趣,但我想知道如何以无点方式组合这些函数的一般模式 const obj = {a: {x: 0}, b: {x: 42}}; // this won't work, but I want it to work const pointFreeComposedLens = R.compose(R.lensProp, R.lensProp('x')); R.vi

我正在尝试编写返回镜头的函数,以生成一个新镜头,并以无点的方式进行

这可能是关于函数组合的更一般的问题。镜片只是一个案例研究。我对透镜不感兴趣,但我想知道如何以无点方式组合这些函数的一般模式

const obj = {a: {x: 0}, b: {x: 42}};

// this won't work, but I want it to work
const pointFreeComposedLens = R.compose(R.lensProp, R.lensProp('x'));
R.view(pointFreeComposedLens('a'), obj); // returns 'undefined'

// this works
const pointyComposedLens = key => R.compose(R.lensProp(key), R.lensProp('x'));
R.view(pointyComposedLens('a'), obj); // returns '0'
组合函数的模式是什么,这样我就不需要在组合管道中为第一个函数重新编写参数了

举个令人震惊的例子:

const deepLens = (a, b, c) => R.lensPath([a, b, c]);

// This works, but is tedious & verbose
const extraDeep = (a, b, c, x) => R.compose(deepLens(a,b,c), R.lensProp(x));
const gammaDeep = (a, b, c, y) => R.compose(deepLens(a,b,c), R.lensProp(y));

// Doesn't work, but it would be nicer to write:
const extraDeep = x => R.compose(deepLens, R.lensProp(x));

// and call it like so:
R.view(extraDeep('a','b','c','x'), obj);

Rest参数将代码缩短为:

 const extraDeep = (...rest) => last => R.compose(deepLens(...rest), R.lensProp(last))(rest.pop());

但我不确定这是否真的很优雅。

如果您打算编写一个接受路径和对象的函数, 然后已经存在:

R.path(['a', 'b'], {a: {b: 10}}); //=> 10
如果您有兴趣删除某些函数中的某些参数,
deepLens
可以重写如下:

const deepLens = R.unapply(R.lensPath);
此无点版本还有一个额外的好处,即它不仅限于三个参数。它将使用任意数量的参数:

deepLens('a', 'b');           //=> R.lensPath(['a', 'b']);
deepLens('a', 'b', 'c');      //=> R.lensPath(['a', 'b', 'c']);
deepLens('a', 'b', 'c', 'd'); //=> R.lensPath(['a', 'b', 'c', 'd']);

我知道你只是把镜头作为一个例子,但这里有一种方法可以让你从它们身上得到我认为你想要的行为

const{lensPath,compose,lens,view}=R
常数deepLens=(a,b,c)=>lensPath([a,b,c]);
常量深度=(镜头,…args)=>合成(镜头,镜头(args))
常数cLens=deepLens('a','b','c')
const obj={a:{b:{c:{d:1,e:2,f:{g:3,h:4,i:{j:5,k:6}
log(view(cLens,obj))/=>{d:1,e:2,f:{g:3,h:4,i:{j:5,k:6}
log(视图(更深层(cLens,'f','g'),obj))/=>3
常数fLens=更深(cLens,'f')
log(视图(fLens,obj))/=>{g:3,h:4,i:{j:5,k:6}
常数jLens=更深(cLens,'f','i','j')
//或jLens=更深(fLens,'i','j')
console.log(视图(jLens,obj))/=>5

我认为镜头是开始学习构图的错误类型,因为它们代表了“高阶构图”,也就是说,它意味着镜头特定的构图(
查看
设置
覆盖
),它采用另一种构图编码你想要查找的路径。接受!基于你对为什么“这不起作用”的解释,我就是不能写无点作文。我现在明白了,实际上,我想对
组合中的第一个参数应用“额外”参数。我希望此:
compose(add,negate)(1,1)
与此等价:
(x=>compose(add(x),negate))(1)(1)
(返回0)。但是,太糟糕了,那是行不通的。好吧,你可以通过对结果函数进行curry处理来得到类似的结果。但这并不准确<代码>咖喱(组合(添加,否定))(3)(8)/=>5
。这是因为它非常接近于
a=>b=>add(否定(a))(b)
Ramda讨论过——并做了短暂的尝试——自动生成
compose
pipe
的结果,但由于问题中讨论的原因,它被放弃了,并且