Javascript 使用Ramda执行子数组的功能方法
是否有一种更实用的方法来完成下面的工作,也许是使用RamdaJavascript 使用Ramda执行子数组的功能方法,javascript,functional-programming,ramda.js,Javascript,Functional Programming,Ramda.js,是否有一种更实用的方法来完成下面的工作,也许是使用Ramda var time = 100; sequenceInstruments.forEach(function(instrument){ if(instrument.on) { playInstrument(time, instrument.duration); } }) 通过仅以无点方式使用Ramda中的函数,您的示例将如下所示 const play = R.forEach(R.when(R.pr
var time = 100;
sequenceInstruments.forEach(function(instrument){
if(instrument.on)
{
playInstrument(time, instrument.duration);
}
})
通过仅以无点方式使用Ramda中的函数,您的示例将如下所示
const play = R.forEach(R.when(R.prop('on'),
R.compose(R.partial(playInstrument, [time]),
R.prop('duration'))))
play(sequenceInstruments)
然而,我经常认为拨回一点可能更好,使用匿名函数可能会使代码更可读,并更清楚地传达意图
const play = R.forEach(R.when(R.prop('on'), i => playInstrument(time, i.duration)))
play(sequenceInstruments)
通过仅以无点方式使用Ramda中的函数,您的示例将如下所示
const play = R.forEach(R.when(R.prop('on'),
R.compose(R.partial(playInstrument, [time]),
R.prop('duration'))))
play(sequenceInstruments)
然而,我经常认为拨回一点可能更好,使用匿名函数可能会使代码更可读,并更清楚地传达意图
const play = R.forEach(R.when(R.prop('on'), i => playInstrument(time, i.duration)))
play(sequenceInstruments)
虽然我同意Scott Christopher的观点,即全点解决方案比您可能提出的任何无点版本都更容易理解,但如果您对开发无点版本感兴趣,并且希望
time
成为最终函数的参数,Ramda提供了一个可能会有所帮助的函数。(还有一个相关的函数,可用于稍有不同的情况。)这取决于当前使用的Playingtrument
函数:
const play = R.useWith(R.forEach, [
playInstrument,
R.compose(R.pluck('duration'), R.filter(R.prop('on')))
]);
play(100, sequenceInstruments);
您可以在上看到这一点,而我同意Scott Christopher的观点,即如果您对开发无点版本感兴趣,并且希望将
时间
作为最终函数的参数,全点解决方案比您可能提出的任何无点版本都更容易理解,Ramda提供了一个可能有帮助的函数。(还有一个相关的函数,可用于稍有不同的情况。)这取决于当前使用的Playingtrument
函数:
const play = R.useWith(R.forEach, [
playInstrument,
R.compose(R.pluck('duration'), R.filter(R.prop('on')))
]);
play(100, sequenceInstruments);
您可以在上看到这一点。我同意@ftor:
过滤器将允许您以更线性的方式进行编写,从而生成完全可读的无点代码
const play = pipe(
filter(prop('on')), // take only the instruments that are 'on'
map(prop('duration')), // take the duration of each of those
forEach(playInstrument(100)) // play'm all
);
play(sequenceInstruments);
这是假设playInstruments
已经过时
使用lodash/fp
的速记,您甚至可以做到:
const play = pipe(
filter('on'),
map('duration'),
forEach(playInstrument(100))
);
我同意@ftor:filter
将允许您以更线性的方式进行编写,从而生成完全可读的无点代码
const play = pipe(
filter(prop('on')), // take only the instruments that are 'on'
map(prop('duration')), // take the duration of each of those
forEach(playInstrument(100)) // play'm all
);
play(sequenceInstruments);
这是假设playInstruments
已经过时
使用lodash/fp
的速记,您甚至可以做到:
const play = pipe(
filter('on'),
map('duration'),
forEach(playInstrument(100))
);
请注意,使用Array.prototype.filter
可以避免没有else
分支的if
语句。只需使用filter
组合map
。如果您想避免中间数组,只需使用它们的转换器实现。“执行子数组”?请注意,您可以避免使用If
语句而不使用else
分支array.prototype.filter
。只需使用filter
组合map
。如果你想避免中间阵列,只需使用它们的转换器实现。“执行子阵列”?加上简单的管道,减去可怕的过载“速记”。@ScottSauyet我理解你的观点。拉姆达有一种“纯粹主义”的功能方法,而有人可能会称洛达斯更“务实”。我不太容易害怕;)也许吧,但这也是jQuery多年来一直声称的。不知何故,它似乎在这么重的压力下下沉。无论如何,这是一个很好的解决方案!好的简单管道的优点,可怕的超负荷“速记”的缺点。@ScottSauyet我理解你的观点。拉姆达有一种“纯粹主义”的功能方法,而有人可能会称洛达斯更“务实”。我不太容易害怕;)也许吧,但这也是jQuery多年来一直声称的。不知何故,它似乎在这么重的压力下下沉。无论如何,这是一个很好的解决方案!